Insert, update, delete data

Data insertion

INSERT is used to insert or add to the database table rows, the insertion can be divided into several methods

  • Insert complete row
  • A portion of the insert row
  • Insert multiple rows
  • Insert the results of some queries

Insert complete row

mysql> INSERT INTO customers VALUES(NULL, 'zhangsan', 'shandong', NULL, NULL, NULL, NULL, '15053631234', '[email protected]');
Query OK, 1 row affected (0.01 sec)

Examples of this customer to insert a new customers table stored in the data in each column of the table given in VALUES statement, must provide a value for each column

Although this syntax is very simple, but it is not safe, you should try to avoid using
highly dependent on the specific order information in this case, if you do, sometimes go wrong

mysql> INSERT INTO customers(
    -> cust_name,
    -> cust_address,
    -> cust_contact,
    -> cust_email
    -> )
    -> VALUES(
    -> 'lisi',
    -> 'shandong',
    -> '15053631235',
    -> '[email protected]');
Query OK, 1 row affected (0.01 sec)
  • This example is completed the exact same job the previous INSERT statement
  • Because the column names provided, VALUES must match the column names specified in the order designated, not necessarily listed in order according to each actual table now,
  • Even the advantage of structural change table, this INSERT statement can still correct implementation

Regardless of the INSERT syntax, must be given the correct number CALUES of
the specified column names and values of this syntax, you can also omit the column. This means you can only provide values for certain columns to other columns do not provide value

Omit a column must meet the conditions

  • The column is defined to allow null values,
  • The default value is given in the table definition (if the value is not given, the default value)

Insert multiple rows

mysql> INSERT INTO customers( cust_name, cust_address, cust_contact, cust_email ) VALUES( 'lisi', 'shandong', '15053631235', '[email protected]'),('wangwu', 'shandong', '15053631236', '[email protected]');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

Wherein a single set of INSERT statement multiple values, each value with one pair of parentheses, separated by commas

Update and delete data

Update Column

In order to update the data in the table (modified), use the UPDATE statement can be used in two ways UPDATE

  • Update specific rows in the table
  • Update all rows in the table

UPDATE statement is very easy to use, basic UPDATE statement consists of three parts

  • To change the table
  • Column names and their new values
  • OK to update the filter condition row

Cust_id 1 customer now has a new e-mail address, so his record needs to be updated,

mysql> UPDATE customers 
    -> SET cust_email='[email protected]'
    -> WHERE cust_id=1;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1  Changed: 1  Warnings: 0
  • UPDATE statement always begins with the name of the table to be updated.
  • Need to update the table name is customers,
  • SET sub statement is used to assign a new value to the column to be updated
  • The WHERE statement is used to determine which row to update. If there is no WHERE clause, will update the e-mail address to all the lines, this is not what we want to see

Update multiple columns

mysql> UPDATE customers  SET cust_name='zhangsan1',cust_email='[email protected]' WHERE cust_id=1;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1  Changed: 1  Warnings: 0

When updating a plurality of columns, only a single SET command, each "column value =" comma-separated prior to use, after the last one without comma

delete data

To delete data from a table, use the DELETE statement can be used in two ways

  • Remove specific rows from a table
  • Delete all the rows from a table

As mentioned above, UPDATE very easy to use, easier to use and DELETE
following statement removes a row from the customers table

mysql> DELETE FROM customers
    -> WHERE cust_id=3;
Query OK, 1 row affected (0.03 sec)

This statement is easy to understand .DELETE FROM called for deleting data from the table name, filtering WHERE clause to delete rows,
in this example, only remove the client id of 3, if you omit the WHERE clause, he will remove the table each customer

Guess you like

Origin www.cnblogs.com/inmeditation/p/11617722.html