目录
- 查看数据表中的记录
- 如何往数据表中添加数据记录
- 写法一
- 写法二
- 如何删除数据
- 如何修改数据
查看数据表中的记录
SELECt * from pet;
如果为空:
Empty set (0.03 sec)
如何往数据表中添加数据记录
写法一
INSERT INTO pet VALUES ('Bob','John','hamster','f','2000-01-01',NULL);
# Query OK, 1 row affected (0.02 sec)
再次查询结果:
+------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+------+-------+---------+------+------------+-------+
| Bob | John | hamster | f | 2000-01-01 | NULL |
+------+-------+---------+------+------------+-------+
1 row in set (0.06 sec)
写法二
INSERT INTO pet(name,owner) VALUES ('Bob','John');
代表在name和owner字段上面插入的一条,其他皆为NULL/默认值的数据
(注意:NULL:代表的是空,表示该字段还没有数据.,一定要和’NULL’区分)
如何删除数据
先多添加几个数据
INSERT INTO pet VALUES('pp1','cc1','dog1','1','1998-1-2',null);
INSERT INTO pet VALUES('pp2','cc2','dog2','2','1998-2-2',null);
INSERT INTO pet VALUES('pp3','cc3','dog3','1','1998-3-2',null);
INSERT INTO pet VALUES('pp4','cc4','dog4','2','1998-4-2',null);
此时数据表中的记录:
+------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+------+-------+---------+------+------------+-------+
| Bob | John | hamster | f | 2000-01-01 | NULL |
| pp1 | cc1 | dog1 | 1 | 1998-01-02 | NULL |
| pp2 | cc2 | dog2 | 2 | 1998-02-02 | NULL |
| pp3 | cc3 | dog3 | 1 | 1998-03-02 | NULL |
| pp4 | cc4 | dog4 | 2 | 1998-04-02 | NULL |
+------+-------+---------+------+------------+-------+
5 rows in set (0.01 sec)
删除pp1这一行的数据:
DELETe FROM pet where name='pp1';
# name='pp1'换成owner='cc1'等等效果相同
此时数据表中的记录,pp1所在的第二行已被删除:
+------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+------+-------+---------+------+------------+-------+
| Bob | John | hamster | f | 2000-01-01 | NULL |
| pp2 | cc2 | dog2 | 2 | 1998-02-02 | NULL |
| pp3 | cc3 | dog3 | 1 | 1998-03-02 | NULL |
| pp4 | cc4 | dog4 | 2 | 1998-04-02 | NULL |
+------+-------+---------+------+------------+-------+
如何修改数据
UPDATE tableName SET 字段1=值1,字段2=值2 ... WHERe 条件;
例如将pp2修改为pp22
UPDATE pet SET name='pp22' WHERe owner='cc2';
此时数据表中的记录:
+------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+------+-------+---------+------+------------+-------+
| Bob | John | hamster | f | 2000-01-01 | NULL |
| pp22 | cc2 | dog2 | 2 | 1998-02-02 | NULL |
| pp3 | cc3 | dog3 | 1 | 1998-03-02 | NULL |
| pp4 | cc4 | dog4 | 2 | 1998-04-02 | NULL |
+------+-------+---------+------+------------+-------+
4 rows in set (0.00 sec)