B.CRUD操作
数据库4个基本操作:CRUD也就是Create、Read、Update、Delete
对应的4个SQL语句:INSERT、SELECT、UPDATE、DELECT
1.INSERT(增)
为t_message数据表插入一笔新数据
INSERT INTO t_message VALUES(1,'caterpillar','caterpillar@mail.com','This is a new message');
如果想用不同的插入顺序
INSERT INTO t_message(name,msg)VALUES('caterpillar','this is a new message')
添加多笔数据
INSERT INTO t_message VALUES
(1,'bush','bush@mail.com','a new message1'),
(2,'justin','justin@mail.com','a new message2'),
(3,'momor','momor@mail.com','a new message3');
2.SELECT(查)
将数据表所有数据查询出来
SELECT * FROM t_message;
使用指定条件查询
SELECT * FROM t_message WHERE name='justin';
两个以上条件可以使用AND或者OR来加以组合
SELECT * FROM t_message WHERE name='justin' AND 'justin@mail.com';
同时取得两个字段的数据
SELECT name,msg FROM t_message;
根据name字段的值排序(默认是顺序)
SELECT * FROM t_message ORDER BY name;
反序排列,加入DESC
SELECT * FROM t_message ORDER BY name DESC;
3.UPDATE(改)
更新数据表中已登录的数据
UPDATE t_message SET name='caterpillar' WHERE id=1;
如果没有使用WHERE限定更新条件,将对所有数据生效
UPDATE也可以用时更新多个字段
UPDATE t_message
SET name='caterpillar',email='caterpillar@mail.com'
WHERE id=1;
4.DELETE(删)
删除数据表中的数据
DELETE FROM t_message WHERE id=1;
如果不使用WHERE指定条件,则会删除数据表中所有的数据
DELETE FROM t_message;
