MYSQL primary study notes 2: data manipulation DML! (video serial number: primary _24,25,36)

Knowledge point five: data manipulation DML (24,25,36)

Insert data:

 

1  --Test insert record INSERT 
2  CREATE  TABLE  IF  NOT  EXISTS user13(
 3 id TINYINT UNSIGNED AUTO_INCREMENT KEY ,
 4 username VARCHAR ( 20 ) NOT  NULL  UNIQUE ,
 5 password CHAR ( 32 ) NOT  NULL ,
 6 email VARCHAR ( 50 ) NOT  NULL  DEFAULT  ' [email protected] ' ,
 7 age TINYINT UNSIGNED DEFAULT 18
8 );
default data

 

  Without specifying a specific field name:

    INSERT [INTO] tbl_name VALUES|VALUE(值…)

1  -- Insert data without specifying a specific field name 
2  INSERT  INTO user13 VALUES ( 1 , ' KING ' , ' KING ' , ' [email protected] ' , 20 );
 3  INSERT  INTO user13 VALUE( 2 , ' QUEEN ' , ' QUEEN ' , ' [email protected] ' , 30 );
Insert data without specifying a specific field name test

  List specified fields:

    INSERT [INTO] tbl_name(field name 1,...) VALUES|VALUE(value 1,...)

1  --List the specified fields: 
2  INSERT  INTO user13(username,password) VALUE( ' A ' , ' A ' );
 3  INSERT  INTO user13(password,username) VALUE( ' BBB ' , ' B ' );
List specified field tests

  Insert multiple records at the same time:

    INSERT [INTO] tbl_name[(field name...)] VALUES(value 1...),(value 2...)

1  -- Insert multiple records at the same time: 
2  INSERT  INTO user13 VALUE( 5 , ' C ' , ' CCC ' , ' [email protected] ' , 35 ),
 3 ( 6 , ' D ' , ' DDD ' , ' DDD @qq.com ' , 31 );
Insert multiple records at the same time

  Insert records via SET form:

    INSERT [INTO] tbl_name SET fieldname=value,…

1  --Insert records in the form of INSERT SET: 
2  INSERT  INTO user13 SET id = 11 ,username = ' test ' ,password = ' test ' ,email = ' [email protected] ' ,age = 48 ;
 3  INSERT  INTO user13 SET username = ' test1 ' ,password = ' test1 ' ;
INSERT SET test

  Insert the results of the query into a table:

    INSERT [INTO] tbl_name[(field name,…)] SELECT field name tbl_name [WHERE condition]

1  -- Insert the results of the query into the table: 
2  CREATE  TABLE  IF  NOT  EXISTS user14(
 3 id TINYINT UNSIGNED AUTO_INCREMENT KEY ,
 4 username VARCHAR ( 20 ) NOT  NULL  UNIQUE 
5  );
 6  INSERT user14 SELECT id,username from user13;
Insert the results of the query into another table test

update data:

        UPDATE tbl_name SET field name=value,… [WHERE condition] [ORDER BY field name] [LIMIT condition]

1  -- Update the age of all users in the user table to 15 
2  UPDATE user13 SET age = 15 ;
 3  
4  -- Change the password, email, and age of the first record 
5  UPDATE user13 SET password = ' king123 ' , email = ' [email protected] ' ,age = 99  WHERE id = 1 ;
 6  -- age-6 with id greater than three 
7  UPDATE user13 SET age = age - 6  WHEREid > 3 ;
 8  -- set the username to "A", age to the default value of 
9  UPDATE user13 SET age = DEFAULT  WHERE username = ' A ' ;
Data update test

delete data:

    DELETE FROM tbl_name [WHERE condition][ORDER BY field name][LIMIT condition]

1  -- delete records in user14 table: 
2  DELETE  FROM user14;
 3  -- delete user 4 with id 1 in user13 table 
4  DELETE  FROM user13 WHERE id = 1 ;
delete data test

    Clear the datasheet completely:

        TRUNCATE [TABLE] tnl_name

Notes

TRUNCATE  TABLE is functionally identical to the DELETE statement without the WHERE clause : both delete all rows in the table. But TRUNCATE TABLE is faster than DELETE and uses less system and transaction log resources. 

The DELETE statement deletes one row at a time and records an entry in the transaction log for each row deleted. TRUNCATE  TABLE deletes data by freeing the data pages used to store the table data, and only records the release of pages in the transaction log.

TRUNCATE  TABLE deletes all rows in a table, but leaves the table structure and its columns, constraints, indexes, etc. unchanged. The count value used for new row identification is reset to the seed for this column. If you want to preserve the identity count value, use DELETE instead . If you want to drop the table definition and its data, use the DROP  TABLE statement.

For tables referenced by FOREIGN  KEY constraints, TRUNCATE  TABLE cannot be used, instead a DELETE statement without a WHERE clause should be used. Since TRUNCATE TABLE is not logged, it cannot activate triggers. 

TRUNCATE  TABLE cannot be used on tables participating in indexed views.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325344025&siteId=291194637