DML语言
数据操作语言:
插入:insert
修改:update
删除:delete
一、插入语句
要求:
1.插入的值的类型要与列的类型一致或兼容
2.不可以为null必须插入值,可以为null的列1.null 2.插入列表中不写
3.插入列表,字段顺序可以电环
4.插入列表的字段数和插入值的个数必须一致
5.插入列表可以省略,默认所有列,而且列的顺序和表中的列的顺序一致
方式一:
语法:
insert into 插入表名(插入列名,...)
values(插入值1,....);
INSERT INTO beauty(id,NAME,sex,`borndate`,`phone`,`photo`,`boyfriend_id`)
VALUES(13,'唐艺昕','女','1990-4-23','1898888888',NULL,2);
INSERT INTO beauty
VALUES(13,'唐艺昕','女','1990-4-23','1898888888',NULL,2);
方式二:
语法:
insert into 插入表名
set 插入列名1=插入值1,插入列名2=插入值2
INSERT INTO beauty
SET id=19,NAME='刘涛',phone='99999';
两种插入方式比较:
1.方式一支持插入多行
INSERT INTO beauty
VALUES(13,'唐艺昕1','女','1990-4-23','1898888888',NULL,2),
(23,'唐艺昕2','女','1990-4-23','1898888888',NULL,2),
(25,'唐艺昕3','女','1990-4-23','1898888888',NULL,2),
...................;
2.方式一支持子查询
INSERT INTO beauty(id,NAME,phone)
SELECT id,boyname,'123456'
FROM boys WHERE id<3;
二、修改语句
1.修改单表的记录
语法:
update 表名
set 列1=新值1,列2=新值2,....
where 筛选条件;
SELECT *
FROM beauty;
SELECT *
FROM boys;
UPDATE beauty
SET phone ='13898885'
WHERE NAME LIKE '唐%';
UPDATE boys b
SET b.`boyName`='张飞',b.`userCP`=10
WHERE b.`id`=2;
2.修改多表的记录
语法:
sql99语法:
update 表1 别名
left|right|inner join 表2 别名
on 连接条件
set 列1=新值1,列2=新值2,....
where 筛选条件;
SELECT *
FROM beauty;
UPDATE boys bo
INNER JOIN beauty b
ON bo.`id`=b.`boyfriend_id`
SET b.`phone`='114'
WHERE bo.`boyName`='张无忌';
UPDATE boys bo
RIGHT JOIN beauty b
ON bo.`id`=b.`boyfriend_id`
SET b.`boyfriend_id`=2
WHERE bo.`id`IS NULL;
三、删除语句
方式一:delete
语法:
1.单标的删除※
delete from 表名
[where 筛选条件]
[limit 条目数];
2.多表删除
delete 表1的别名,表2的别名
from 表1 别名
left|right|inner join 表2 别名
on 连接条件
where 筛选条件;
delete from 表名;
DELETE FROM beauty
WHERE phone LIKE '%9';
SELECT * FROM beauty;
DELETE b
FROM beauty b
JOIN boys bo
ON b.`boyfriend_id`=bo.`id`
WHERE bo.`boyName`='张无忌';
DELETE b,bo
FROM beauty b
JOIN boys bo
ON b.`boyfriend_id`=bo.`id`
WHERE bo.`boyName`='黄晓明';
方式二:truncate
语法:
truncate table 表名;
不允许加where,清空整个表的数据
*****truncate和delete的区别:
**假如要删除的表中有自增长列,
如果用delete删除后,在插入数据,自增长列的值从断点(删除前表中长列的最大点)开始,
而truncate删除后,在插入数据,自增长列的值从1开始.
**truncate删除后没有返回值,delete删除后返回删除的结果
**truncate删除不能回滚,delete删除可以回滚
USE myemployees;
CREATE TABLE my_employees(
Id INT(10),
First_name VARCHAR(10),
Last_name VARCHAR(10),
Userid VARCHAR(10),
Salary DOUBLE(10,2)
);
CREATE TABLE users(
id INT,
userid VARCHAR(10),
department_id INT
);
DESC my_employees;
ID FIRST_NAME LAST_NAME USERID SALARY
1 patel Ralph Rpatel 895
2 Dancs Betty Bdancs 860
3 Biri Ben Bbiri 1100
4 Newman Chad Cnewman 750
5 Ropeburn Audrey Aropebur 1550
INSERT INTO my_employees
VALUES(1,'patel','Ralph','Rpatel',895),
(2,'Dancs','Betty','Bdancs',860),
(3,'Biri','Ben','Bbiri',1100),
(4,'Newman','Chad','Cnewman',750),
(5,'Ropeburn','Audrey','Aropebur',1550);
DELETE FROM my_employees;
INSERT INTO my_employees
SELECT 1,'patel','Ralph','Rpatel',895 UNION
SELECT 2,'Dancs','Betty','Bdancs',860 UNION
SELECT 3,'Biri','Ben','Bbiri',1100 UNION
SELECT 4,'Newman','Chad','Cnewman',750 UNION
SELECT 5,'Ropeburn','Audrey','Aropebur',1550;
1 Rpatel 10
2 Bdancs 10
3 Bbiri 20
4 Cnewman 30
5 Aropebur 40
INSERT INTO users
VALUES(1,'Rpatel',10),
(2,'Bdancs',10),
(3,'Bbiri',20);
UPDATE my_employees SET last_name='drelxer' WHERE id = 3;
UPDATE my_employees SET salary=1000 WHERE salary<900;
DELETE u,e
FROM users u
JOIN my_employees e ON u.`userid`=e.`Userid`
WHERE u.`userid`='Bbiri';
DELETE FROM my_employees;
DELETE FROM users;
SELECT * FROM my_employees;
SELECT * FROM users;
TRUNCATE TABLE my_employees;