First, we create a test user table
1 2 3 4 5 6 7
CREATE TABLE IF NOT EXISTS `user2` (id TINYINT UNSIGNED KEY AUTO_INCREMENT,username VARCHAR (20 ) NOT NULL UNIQUE , password CHAR (32 ) NOT NULL ,email VARCHAR (50 ) NOT NULL DEFAULT '[email protected] ' , age TINYINT UNSIGNED DEFAULT 18 );
1, insert data
1, without specifying a field name
1
INSERT [INTO ] tbl_name VALUES |VALUE (值...)
Insert a row into the table, and see:
1 2 3 4 5 6 7 8 9 10
mysql> INSERT INTO user2 VALUE(1,"sing","root","[email protected] ",18); Query OK, 1 row affected (0.01 sec) mysql> SELECT * FROM user2; + | id | username | password | email | age | + | 1 | sing | root | [email protected] | 18 | + 1 row in set (0.00 sec)
2, lists the specified field
1
INSERT [ INTO ] tbl_name (field name 1 , ...) VALUES | of VALUE (value 1 , ...)
Record insert two days, and only the user name and password have value, and the rest will use the default values, and see:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
mysql> INSERT user2(username,password) VALUES('A','aaa'); Query OK, 1 row affected (0.00 sec) mysql> INSERT user2(username,password) VALUES('B','bbb'); Query OK, 1 row affected (0.01 sec) mysql> SELECT * FROM user2; + | id | username | password | email | age | + | 1 | sing | root | [email protected] | 18 | | 2 | A | aaa | [email protected] | 18 | | 3 | B | bbb | [email protected] | 18 | + 3 rows in set (0.01 sec)
3, while inserting a plurality of records
1
The INSERT [ the INTO ] tbl_name [(Field Name ...)] the VALUES (value ...), (value ...) ...
We found three direct data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
mysql> INSERT user2 VALUES(4,'C','ccc','[email protected] ',23), -> (5,'D','ddd','[email protected] ',13), -> (6,'E','eee','[email protected] ',63); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM user2; + | id | username | password | email | age | + | 1 | sing | root | [email protected] | 18 | | 2 | A | aaa | [email protected] | 18 | | 3 | B | bbb | [email protected] | 18 | | 4 | C | ccc | [email protected] | 23 | | 5 | D | ddd | [email protected] | 13 | | 6 | E | eee | [email protected] | 63 | + 6 rows in set (0.00 sec)
4, by inserting a recording form SET
1
INSERT [INTO ] tbl_name SET 字段名称=值,...
测试一下:
5、将查询结果插入到表中
1
INSERT [INTO ] tbl_name[(字段名称,...)] SELECT 字段名称 FROM tbl_name [WHERE 条件]
我们新创建一个表user3
,查询user2
中的id
和username
插入到user3
表中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
mysql> CREATE TABLE IF NOT EXISTS user3( -> id TINYINT UNSIGNED AUTO_INCREMENT KEY, -> username VARCHAR(20) NOT NULL UNIQUE -> ); Query OK, 0 rows affected (0.02 sec) mysql> INSERT user3 SELECT id,username FROM user2; Query OK, 7 rows affected (0.01 sec) Records: 7 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM user3; + | id | username | + | 2 | A | | 3 | B | | 4 | C | | 5 | D | | 6 | E | | 7 | F | | 1 | sing | + 7 rows in set (0.00 sec)
我们可以看到我们的user2
表中有多个字段,如果我们用上面的方法直接将查询结果插入到user3
,会报错数据不匹配,我们可以将字段列出来操作:
1 2 3 4 5
mysql> INSERT user3 SELECT id,username FROM user2; ERROR 1136 (21S01): Column count doesn't match value count at row 1 mysql> INSERT user3(id,username) SELECT id,username FROM user2; Query OK, 7 rows affected (0.00 sec) Records: 7 Duplicates: 0 Warnings: 0
2、更新数据
1
UPDATE tbl_name SET 字段名称=值,... [WHERE 条件][ORDER BY 字段名称][LIMIT 限制条数]
如果不加WHERE
条件时,会把数据库中所以信息都更新,例如将user2
中所有字段都改为33岁:
更新id
为1的密码、年龄:
3、删除数据
1
DELETE FROM tbl_name [WHERE 条件][ORDER BY 字段名称][LIMIT 限制条数]
同样的如果不加条件的时候会删除所有的数据,测试删除user2
中id为7的数据:
彻底清空数据表:
1
TRUNCATE [TABLE ] tbl_name
This is not to test it ~
Original: Large column in the data table MySql_10 add, delete, change