1.添加数据
在添加新记录之前需要先创建一个数据库itcast,创建数据库的SQL语句如下所示:
mysql> CREATE DATABASE itcast;Query OK, 1 row affected
选择使用数据库itcast,SQL语句如下:
mysql> USE itcast;
Database changed
在数据库中创建一个表student1_tb用于存储学生信息,创建student_tb表的SQL语句如下所示:
mysql> CREATE TABLE student1_tb
-> (
-> id INT(20) PRIMARY KEY,
-> name VARCHAR(20) NOT NULL,
-> sex VARCHAR(10) UNIQUE,
-> grade FLOAT
-> );
Query OK, 0 rows affected
查看student1_tb中的内容
mysql> DESCRIBE student1_tb;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(20) | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| sex | varchar(10) | YES | UNI | NULL | |
| grade | float | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set
使用INSERT语句向student_tb表中插入一条数据,SQL语句如下所示:
mysql> INSERT INTO student1_tb(id,name,sex,grade) VALUES(1,"zhangsan","nan",98.5);
Query OK, 1 row affected
当上述SQL语句执行成功后,会在表student1_tb中添加一条数据。为了验证数据是否添加成功,使用SELECT语句查看student_tb表中的数据,查询结果如下:
mysql> SELECT * FROM student1_tb;
+----+------+-----+-------+
| id | name | sex | grade |
+----+------+-----+-------+
| 1 | zhangsan | nan | 98.5 |
+----+------+-----+-------+
1 row in set
继续为student1_tb添加数据;
mysql> INSERT INTO student1_tb(id,name,sex,grade) VALUES(2,"lisi","nan",95);
Query OK, 1 row affected
mysql> INSERT INTO student1_tb(id,name,sex,grade) VALUES(3,"wangwu","nv",61.5);
Query OK, 1 row affected
验证是否添加成功;
mysql> SELECT * FROM student1_tb;
+----+------+-----+-------+
| id | name | sex | grade |
+----+------+-----+-------+
| 1 | zhangsan | nan | 98.5 |
| 2 | lisi | nan | 95 |
| 3 | wangwu | nv | 61.5 |
+----+------+-----+-------+
3 rows in set
2.更新数据
UPDATE更新部分数据
mysql> UPDATE student1_tb set name="哦",grade=95 WHERE id=7;
Query OK, 1 row affected
Rows matched: 1 Changed: 1 Warnings: 0
更新student1_tb表中id字段值小于4的记录,将这些记录的grade字段值都更新为100。
mysql> UPDATE student1_tb set grade=100 WHERE id<4;
Query OK, 2 rows affected
Rows matched: 3 Changed: 2 Warnings: 0
3 rows in set
更新student1_tb表中id字段值小于4的记录,将grade字段值都更新为80。
mysql> UPDATE student1_tb set grade=80;
Query OK, 7 rows affected
Rows matched: 7 Changed: 7 Warnings: 0
三、删除数据
在student1_tb表中,删除id字段值为6的记录:
mysql> DELETE FROM student1_tb WHERE id=6;
Query OK, 1 row affected
在student1_tb表中,删除id字段值大于3的记录:
mysql> DELETE FROM student1_tb WHERE id>3;
Query OK, 3 rows affected
删除全部数据
mysql> DELETE FROM student1_tb;
Query OK, 3 rows affected
再次通过查询语句查看student1_tb表中的记录,执行结果如下所示:
mysql> SELECT * FROM student1_tb;
Empty set
原文地址:https://www.cnblogs.com/cmja/p/11827289.html