SQL structured query language - the language of DML

DML: Data Manipulation Language data manipulation language, used to realize the increasing of the table insert, delete delete, update operation to change
a, add a record insert
1. Add record is added row.

This field specifies the value must be added when the recording after the designated table field modifier NO NULL. There is no default field values must be specified.
Syntax: INSERT tbl_name [(col1, ...)] VALUES (val1, ...), (val21, ...)
Chinese: INSERT table column names [...] VALUES (record 1), (record 2), ... specify the field name to be assigned, then () sequentially assigned with values, if you can write all the fields assigned to field name is omitted.
Assignment Note: 1, the string must be enclosed in quotation marks. 2, does not allow empty fields must be assigned if the value determined is not off, the flag information may be added all records will be added to uphold the late flag information. 3, the primary key column values can not be repeated. 4, with no default value must be specified in the field.

Example 2. insert operation
method: Manually specified data.

  1. INSERT INTO vmlab values ​​(2, 'Hong qigong', 60, 'M', 3,4); assigned to all words
  2. INSERT INTO vmlab (id, name, age, classID) values ​​(3, 'Huang Yaoshi', 56,3); adding a specified field assignment behavior.
  3. INSERERT INTO vmlab (id, name, age, classID) values ​​(6, 'Huang Yaoshi', 56,3), (4, 'Ou yangfeng', 60,4), (5, 'Duan zhixing', 65,2 ); add multiple acts specified field assignment.

Method two: set the direct assignment

INSERT INTO vmlab set id = 7, name = 'Guo Jing', age = 23; field is used directly assigned to the specified set.

Method three: bulk insert extract data from other tables

  1. Insert into vmlab (id, name, Age, Gender) select tid, name, age, gender from teachers; teachers extracted from the designated field of all rows, VMLAB inserted into the table, this method requires: requires two operating tables sequence corresponding field, the same field data type.
  2. Insert into vmlab (id, name, Age, Gender) select tid, name, age, gender from other.teachers; teachers extracted from the designated field in the database table all the other rows, VMLAB inserted into the table, this method requires: requires the order of operations corresponding fields of two tables, the same field data type.
  3. Example: From a check table data table and insert another implementation:
    INSERT INTO T1 T2 SELECT * from;

Two, delete delete records:

  1. Syntax: delete from table where the matching conditions (typically corresponding to the primary key field value records);
  2. Meaning: delete data from a table based on matching conditions where a successful match.
  3. Note: delete must match the conditions defined by where, otherwise it will empty the entire data table.
  4. 实例:
    a. DELETE FROM vmlab where id >10
    b. DELETE FROM hellodb.vmlab where id >10
  5. Empty table: TRUNCATE TABLE students;

Three, update alterations record

  1. Syntax: UPDATE table SET field name = "value" WHERE match condition (typically primary key field value corresponding record);
  2. Meaning: Update the value of a matching table WHERE conditions specified SET assignment from an assignment expression.
  3. Note:
    UPDATE must be qualified to modify the scope of use WHERE, otherwise it will change the whole table.
  4. Example:
    the UPDATE VMLAB the SET NAME = 'Guo Jing' WHERE ID = 2; VMLAB the second row of the table records the name field value to Guo Jing

Fourth, best practices

  1. To avoid misuse, then strong mysql -U recommended when connecting the database --safe->
  2. updates security update options, or add safe-updates Enforce security update option [client] configuration items in the configuration file in /etc/my.cnf. Update option to modify the enabled security requirements must be defined a primary key table, and then perform the column values ​​in the primary key update based Update History defined range. When you enable security update will appear the following prompt if the table has no primary key to perform update
    SQL structured query language - the language of DML
  3. After defining the primary key, the security update mode, the recording is performed UPDATE modification, the conditions where the line must be defined based on the primary key, the other fields can not be defined as a condition where, as will prompt:
    SQL structured query language - the language of DML

Guess you like

Origin blog.51cto.com/154773488/2455094