INSERT INTO tbl_name (column_list) VALUES (value_list) 在插入数据前,首先创建一张表:
mysql> CREATE TABLE person -> ( -> id INT UNSIGNED NOT NULL AUTO_INCREMENT, -> name CHAR(40) NOT NULL DEFAULT '', -> age INT NOT NULL DEFAULT 0, -> info CHAR(50) NULL, -> PRIMARY KEY (id) -> ); Query OK, 0 rows affected (0.03 sec) 在person表中,插入一条新记录,id值为1,name值为Green,age值为21,info值为Lawyer
mysql> INSERT INTO person (id ,name, age , info) -> VALUES (1,'Green', 21, 'Lawyer'); Query OK, 1 row affected (0.02 sec)
mysql> SELECT * FROM person; +----+-------+-----+--------+ | id | name | age | info | +----+-------+-----+--------+ | 1 | Green | 21 | Lawyer | +----+-------+-----+--------+ 1 row in set (0.00 sec) 在person表中,插入一条新记录,id值为2,name值为Suse,age值为22,info值为dancer
mysql> INSERT INTO person (age ,name, id , info) -> VALUES (22, 'Suse', 2, 'dancer'); Query OK, 1 row affected (0.02 sec)
mysql> SELECT * FROM person; +----+-------+-----+--------+ | id | name | age | info | +----+-------+-----+--------+ | 1 | Green | 21 | Lawyer | | 2 | Suse | 22 | dancer | +----+-------+-----+--------+ 2 rows in set (0.00 sec) 为表的指定字段插入数据
在person表中,插入一条新记录,name值为Willam,age值为20,info值为sports man
mysql> INSERT INTO person (name, age,info) -> VALUES('Willam', 20, 'sports man'); Query OK, 1 row affected (0.02 sec)
mysql> SELECT * FROM person; +----+--------+-----+------------+ | id | name | age | info | +----+--------+-----+------------+ | 1 | Green | 21 | Lawyer | | 2 | Suse | 22 | dancer | | 3 | Willam | 20 | sports man | +----+--------+-----+------------+ 3 rows in set (0.00 sec) 在person表中,插入一条新记录,name值为laura,age值为25
mysql> INSERT INTO person (name, age ) VALUES ('Laura', 25); Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM person; +----+--------+-----+------------+ | id | name | age | info | +----+--------+-----+------------+ | 1 | Green | 21 | Lawyer | | 2 | Suse | 22 | dancer | | 3 | Willam | 20 | sports man | | 4 | Laura | 25 | NULL | +----+--------+-----+------------+ 4 rows in set (0.00 sec) 可以发现id字段在插入数据后,没有赋值时自动增加,在这里id字段为表的主键,不能为空,紫铜会自动为字段插入自增的序列值。
INSERT INTO tbl_name1 (column_list1) SELECT (column_list2) FROM table_name2 WHERE (condition) 从person_old表中查询所有的记录,并将其插入到person表中
首先,创建一个名为person_old的数据表,其表结构与person结构相同 mysql> CREATE TABLE person_old -> ( -> id INT UNSIGNED NOT NULL AUTO_INCREMENT, -> name CHAR(40) NOT NULL DEFAULT '', -> age INT NOT NULL DEFAULT 0, -> info CHAR(50) NULL, -> PRIMARY KEY (id) -> ); Query OK, 0 rows affected (0.11 sec)
UPDATE table_name SET col_name1=value1,col_name2=value2,..., WHERE where_condition 在person表中,更新id值为11的记录,将age字段值改为15,将name字段值改为LiMing
mysql> UPDATE person SET age = 15, name='LiMing' WHERE id = 11; Query OK, 1 row affected (0.02 sec) Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM person WHERE id=11; +----+--------+-----+---------+ | id | name | age | info | +----+--------+-----+---------+ | 11 | LiMing | 15 | student | +----+--------+-----+---------+ 1 row in set (0.00 sec) 在person表中,更新age值为19~22的记录,将info字段值都改为student
mysql> UPDATE person SET info='student' WHERE age BETWEEN 19 AND 22; Query OK, 6 rows affected (0.02 sec) Rows matched: 6 Changed: 6 Warnings: 0
mysql> SELECT * FROM person WHERE age BETWEEN 19 AND 22; +----+---------+-----+---------+ | id | name | age | info | +----+---------+-----+---------+ | 1 | Green | 21 | student | | 2 | Suse | 22 | student | | 3 | Willam | 20 | student | | 6 | Dale | 22 | student | | 9 | Harry | 21 | student | | 10 | Harriet | 19 | student | +----+---------+-----+---------+ 6 rows in set (0.00 sec) 3、删除数据
执行删除操作前,使用SELECT语句查看当前id=11的记录 mysql> SELECT * FROM person WHERE id=11; +----+--------+-----+---------+ | id | name | age | info | +----+--------+-----+---------+ | 11 | LiMing | 15 | student | +----+--------+-----+---------+ 1 row in set (0.00 sec)
使用DELETE语句删除该记录 mysql> DELETE FROM person WHERE id = 11; Query OK, 1 row affected (0.02 sec)
语句执行完毕,查看执行结果: mysql> SELECT * FROM person WHERE id=11; Empty set (0.00 sec) 在person表中,使用DELETE语句同时删除多条记录,删除age字段在19-22的记录
执行删除操作前,使用SELECT语句查看当前的数据 mysql> SELECT * FROM person WHERE age BETWEEN 19 AND 22; +----+---------+-----+---------+ | id | name | age | info | +----+---------+-----+---------+ | 1 | Green | 21 | student | | 2 | Suse | 22 | student | | 3 | Willam | 20 | student | | 6 | Dale | 22 | student | | 9 | Harry | 21 | student | | 10 | Harriet | 19 | student | +----+---------+-----+---------+ 6 rows in set (0.00 sec)
DELETE删除这些记录 mysql> DELETE FROM person WHERE age BETWEEN 19 AND 22; Query OK, 6 rows affected (0.01 sec)
查看执行结果 mysql> SELECT * FROM person WHERE age BETWEEN 19 AND 22; Empty set (0.00 sec) 删除person表中所有记录,SQL语句如下
执行删除操作前,使用SELECT语句查看当前的数据: mysql> SELECT * FROM person; +----+---------+-----+-----------+ | id | name | age | info | +----+---------+-----+-----------+ | 4 | Laura | 25 | NULL | | 5 | Evans | 27 | secretary | | 7 | Edison | 28 | singer | | 12 | Beckham | 31 | police | +----+---------+-----+-----------+ 4 rows in set (0.00 sec)