首先我们创建一个测试的用户表
1234567 | 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 '123123123@qq.com',age TINYINT UNSIGNED DEFAULT 18); |
1 | INSERT [INTO] tbl_name VALUES|VALUE(ֵ...) |
向表中插入一条数据,并查看:
12345678910 | mysql> INSERT INTO user2 VALUE(1,"sing","root","sing@163.com",18);Query OK, 1 row affected (0.01 sec)mysql> SELECT * FROM user2;+| id | username | password | email | age |+| 1 | sing | root | sing@163.com | 18 |+1 row in set (0.00 sec) |
1 | INSERT [INTO] tbl_name(字段名称1,...) VALUES|VALUE(值1,...) |
插入两天记录,且只给用户名和密码有值,其余的将会使用默认值,并查看:
123456789101112131415 | 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 | sing@163.com | 18 || 2 | A | aaa | 123123123@qq.com | 18 || 3 | B | bbb | 123123123@qq.com | 18 |+----+----------+----------+------------------+------+3 rows in set (0.01 sec) |
1 | INSERT [INTO] tbl_name[(字段名称...)] VALUES(值...),(值...)... |
我们直接查出三条数据:
123456789101112131415161718 | mysql> INSERT user2 VALUES(4,'C','ccc','ccc@qq.com',23), -> (5,'D','ddd','ddd@qq.com',13), -> (6,'E','eee','eee@qq.com',63);Query OK, 3 rows affected (0.00 sec)Records: 3 Duplicates: 0 Warnings: 0mysql> SELECT * FROM user2;+----+----------+----------+------------------+------+| id | username | password | email | age |+----+----------+----------+------------------+------+| 1 | sing | root | sing@163.com | 18 || 2 | A | aaa | 123123123@qq.com | 18 || 3 | B | bbb | 123123123@qq.com | 18 || 4 | C | ccc | ccc@qq.com | 23 || 5 | D | ddd | ddd@qq.com | 13 || 6 | E | eee | eee@qq.com | 63 |+----+----------+----------+------------------+------+6 rows in set (0.00 sec) |
1 | INSERT [INTO] tbl_name SET 字段名称=值,... |
测试一下:
12345678910111213141516 | mysql> INSERT user2 SET id=7,username='F',password='fff';Query OK, 1 row affected (0.00 sec)mysql> SELECT * FROM user2;+----+----------+----------+------------------+------+| id | username | password | email | age |+----+----------+----------+------------------+------+| 1 | sing | root | sing@163.com | 18 || 2 | A | aaa | 123123123@qq.com | 18 || 3 | B | bbb | 123123123@qq.com | 18 || 4 | C | ccc | ccc@qq.com | 23 || 5 | D | ddd | ddd@qq.com | 13 || 6 | E | eee | eee@qq.com | 63 || 7 | F | fff | 123123123@qq.com | 18 |+----+----------+----------+------------------+------+7 rows in set (0.00 sec) |
1 | INSERT [INTO] tbl_name[(字段名称,...)] SELECT 字段名称 FROM tbl_name [WHERE 条件] |
我们新创建一个表user3
,查询user2
中的id
和username
插入到user3
表中:
1234567891011121314151617181920212223 | 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: 0mysql> 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
,会报错数据不匹配,我们可以将字段列出来操作:
12345 | mysql> INSERT user3 SELECT id,username FROM user2;ERROR 1136 (21S01): Column count doesn't match value count at row 1mysql> INSERT user3(id,username) SELECT id,username FROM user2;Query OK, 7 rows affected (0.00 sec)Records: 7 Duplicates: 0 Warnings: 0 |
1 | UPDATE tbl_name SET 字段名称=值,... [WHERE 条件][ORDER BY 字段名称][LIMIT 限制条数] |
如果不加WHERE
条件时,会把数据库中所以信息都更新,例如将user2
中所有字段都改为33岁:
1234567891011121314151617 | mysql> UPDATE user2 SET age=33;Query OK, 7 rows affected (0.01 sec)Rows matched: 7 Changed: 7 Warnings: 0mysql> select * from user2;+----+----------+----------+------------------+------+| id | username | password | email | age |+----+----------+----------+------------------+------+| 1 | sing | root | sing@163.com | 33 || 2 | A | aaa | 123123123@qq.com | 33 || 3 | B | bbb | 123123123@qq.com | 33 || 4 | C | ccc | ccc@qq.com | 33 || 5 | D | ddd | ddd@qq.com | 33 || 6 | E | eee | eee@qq.com | 33 || 7 | F | fff | 123123123@qq.com | 33 |+----+----------+----------+------------------+------+7 rows in set (0.00 sec) |
更新id
为1的密码、年龄:
1234567891011121314151617 | mysql> UPDATE user2 SET password='sing',age=23 WHERE id=1;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> SELECT * FROM user2;+----+----------+----------+------------------+------+| id | username | password | email | age |+----+----------+----------+------------------+------+| 1 | sing | sing | sing@163.com | 23 || 2 | A | aaa | 123123123@qq.com | 33 || 3 | B | bbb | 123123123@qq.com | 33 || 4 | C | ccc | ccc@qq.com | 33 || 5 | D | ddd | ddd@qq.com | 33 || 6 | E | eee | eee@qq.com | 33 || 7 | F | fff | 123123123@qq.com | 33 |+----+----------+----------+------------------+------+7 rows in set (0.00 sec) |
1 | DELETE FROM tbl_name [WHERE 条件][ORDER BY 字段名称][LIMIT 限制条数] |
同样的如果不加条件的时候会删除所有的数据,测试删除user2
中id为7的数据:
123456789101112131415 | mysql> DELETE FROM user2 WHERE id=7;Query OK, 1 row affected (0.00 sec)mysql> SELECT * FROM user2;+----+----------+----------+------------------+------+| id | username | password | email | age |+----+----------+----------+------------------+------+| 1 | sing | sing | sing@163.com | 23 || 2 | A | aaa | 123123123@qq.com | 33 || 3 | B | bbb | 123123123@qq.com | 33 || 4 | C | ccc | ccc@qq.com | 33 || 5 | D | ddd | ddd@qq.com | 33 || 6 | E | eee | eee@qq.com | 33 |+----+----------+----------+------------------+------+6 rows in set (0.00 sec) |
彻底清空数据表:
1 | TRUNCATE [TABLE] tbl_name |
这个就不测试啦~
原文:大专栏MySql_10 表中数据的增、删、改
来源:博客园
作者:老特特傻
链接:https://www.cnblogs.com/chinatrump/p/11588826.html