小白菜学Mysql 笔记(二)--DML管理语言

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 表名
	set1=新值1,2=新值2,....
	where 筛选条件;#找到想修改的行,可多行
	
	
	
	SELECT *
	FROM beauty;

	SELECT *
	FROM boys;


	#修改beauty表中姓唐的女神的电话为13898885

	UPDATE beauty
	SET phone ='13898885'
	WHERE NAME LIKE '唐%';

	#修改boys表中id为2的名称为张飞,魅力值为10

	UPDATE boys b
	SET b.`boyName`='张飞',b.`userCP`=10
	WHERE b.`id`=2;	
	
	
	
2.修改多表的记录
	语法:
	sql99语法:
	update1 别名
	left|right|inner join2 别名
	on 连接条件
	set1=新值1,2=新值2,....
	where 筛选条件;#找到想修改的行,可多行 

	SELECT *
	FROM beauty;

	#修改张无忌的女朋友的手机号为114

	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.多表删除
	delete1的别名,2的别名
	from1 别名
	left|right|inner join2 别名
	on 连接条件
	where 筛选条件;
	
		#方式一:delete
		#1.单表的删除
		
		#删除整表
		delete from 表名;
		
		#删除手机号9结尾的女神信息

		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,清空整个表的数据
	
	*****truncatedelete的区别:
	**假如要删除的表中有自增长列,
	如果用delete删除后,在插入数据,自增长列的值从断点(删除前表中长列的最大点)开始,truncate删除后,在插入数据,自增长列的值从1开始.
	
	**truncate删除后没有返回值,delete删除后返回删除的结果
	
	**truncate删除不能回滚,delete删除可以回滚
	

#1.	运行以下脚本创建表my_employees

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

);
#2.	显示表my_employees的结构
DESC my_employees;

#3.	向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;

				
#4.	 向users表中插入数据
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);




#5.将3号员工的last_name修改为“drelxer”
UPDATE my_employees SET last_name='drelxer' WHERE id = 3;



#6.将所有工资少于900的员工的工资修改为1000
UPDATE my_employees SET salary=1000 WHERE salary<900;

#7.将userid 为Bbiri的user表和my_employees表的记录全部删除

DELETE u,e
FROM users u
JOIN my_employees e ON u.`userid`=e.`Userid`
WHERE u.`userid`='Bbiri';

#8.删除所有数据

DELETE FROM my_employees;
DELETE FROM users;
#9.检查所作的修正

SELECT * FROM my_employees;
SELECT * FROM users;

#10.清空表my_employees
TRUNCATE TABLE my_employees;

你可能感兴趣的:(sql)