1.增加记录操作
主要用到INSERT语句。
例如,向表中插入:
mysql> INSERT INTO course VALUES(7,'bio');
Query OK, 1 row affected (0.11 sec)
mysql> select * from course;
+-----------+-------------+
| course_id | course_name |
+-----------+-------------+
| 1 | ch |
| 2 | math |
| 3 | english |
| 4 | phy |
| 7 | bio |
+-----------+-------------+
5 rows in set (0.00 sec)
也可以在表名后面指定要插入的列的名称,如下:
mysql> INSERT INTO course(course_id,course_name) VALUES(8,'xyz');
Query OK, 1 row affected (0.08 sec)
mysql> select * from course;
+-----------+-------------+
| course_id | course_name |
+-----------+-------------+
| 1 | ch |
| 2 | math |
| 3 | english |
| 4 | phy |
| 7 | bio |
| 8 | xyz |
+-----------+-------------+
6 rows in set (0.00 sec)
2.删除操作
使用DELETE 完成。具体语法如下:
mysql> DELETE FROM course WHERE course_id=8;
Query OK, 1 row affected (0.08 sec)
mysql> select * from course;
+-----------+-------------+
| course_id | course_name |
+-----------+-------------+
| 1 | ch |
| 2 | math |
| 3 | english |
| 4 | phy |
| 7 | bio |
+-----------+-------------+
5 rows in set (0.00 sec)
3.修改(更新)操作
使用UPDATE 语句实现。
如下,存在表:
mysql> select * from course;
+-----------+-------------+
| course_id | course_name |
+-----------+-------------+
| 1 | ch |
| 2 | math |
| 3 | english |
| 4 | phy |
| 7 | bio |
+-----------+-------------+
5 rows in set (0.00 sec)
将id为1的修改为chinese,如下:
mysql> UPDATE course SET course_name = 'chinese' WHERE course_id=1;
Query OK, 1 row affected (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from course;
+-----------+-------------+
| course_id | course_name |
+-----------+-------------+
| 1 | chinese |
| 2 | math |
| 3 | english |
| 4 | phy |
| 7 | bio |
+-----------+-------------+
5 rows in set (0.00 sec)