mysql 练习100道

Posted z1427094386

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 练习100道相关的知识,希望对你有一定的参考价值。

1、在命令行中启动mysql:
	net start mysql( 服务名称 )  如果拒绝访问,使用管理员身份运行cmd 

2、在命令行中关闭MySql:
	net stop mysql

3、使用运行连接MySql数据库:
	mysql -uroot -p密码

4、使用命令在命令行中查看所有的数据库:
	show databases ;

5、使用命令在命令行中进入某个数据库:
	use db ;

6、使用命令在命令行中查看指定数据库中的所有表:
	show tables ;

7、使用命令在命令行中创建一个名为demo1的数据库:
	create database demo1 ;

8、使用命令删除名为demo1的数据库:
	drop database demo1 ;

9、使用命令创建一个学生信息表(t_student)
列为(id、name、age、email),id为主键且自增:
	create table t_student (
		id int primary key auto_increment ,
		name varchar(32),
		age int ,
		email varchar(255)
	);

10、使用命令删除t_student表:
	drop table t_student ;

11、使用命令创建一个名为order表,
列:id(主键且自增)、name、age: order 属于关键字需要 `order`
	create table `order` (
		id int primary key auto_increment ,
		name varchar(32),
		age int
	);

12、使用命令删除order表:
	drop table `order` ;


二、增加:
建表语句:
CREATE TABLE t_student(
	id BIGINT PRIMARY KEY AUTO_INCREMENT,
	name VARCHAR(30),
	age INT,
	email VARCHAR(30)
);

1、使用语句插入一条学生信息到t_student表(张三、18、10000@qq.com);
insert into t_student (name ,age ,email ) values ('张三', 18 ,'10000@qq.com');

2、使用语句插入部分学生信息到t_student表(周七、22):
insert into t_student (name , age) values ('周七' , 22 );

3、使用语句插入多条学生信息到t_student表(赵六、18、40000@qq.com)(李四、19、20000@qq.com)(王五、20、30000@qq.com):
	insert into t_student (name, age ,email ) values ('赵六',18,'40000@qq.com'),('李四',19,'20000@qq.com'),('王五',20,'30000@qq.com');

4、插入查询出来的学生姓名:
	insert into t_student (name)  select name from t_student ; 	


三、删除和更改:
1、删除id为6的学生:
	delete from t_student where id = 6 ;

2、修改id为7的学生的姓名为赵匡胤:
	update t_student set name = "赵匡胤" where id = 7 ;

3、修改id为3的学生,名字为张三丰,年龄140,邮箱为zhangsanf@:
	update t_student set name = "张三丰" , age = 140 , email = "zhangsanf@:" where id = 3 ;

4、删除所有的学生信息:
	delete from t_student ;

四、查询:
建表语句:
CREATE TABLE `product` (
	`id` bigint(20) NOT NULL AUTO_INCREMENT,
	`productName` varchar(50) DEFAULT NULL,
	`dir_id` bigint(20) DEFAULT NULL,
	`salePrice` double(10,2) DEFAULT NULL,
	`supplier` varchar(50) DEFAULT NULL,
	`brand` varchar(50) DEFAULT NULL,
	`cutoff` double(2,2) DEFAULT NULL,
	`costPrice` double(10,2) DEFAULT NULL,
	PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8;

插入信息:
INSERT INTO `product` VALUES ('1', '罗技M90', '3', '90.00', '罗技', '罗技', '0.50', '35.00');
INSERT INTO `product` VALUES ('2', '罗技M100', '3', '49.00', '罗技', '罗技', '0.90', '33.00');
INSERT INTO `product` VALUES ('3', '罗技M115', '3', '99.00', '罗技', '罗技', '0.60', '38.00');
INSERT INTO `product` VALUES ('4', '罗技M125', '3', '80.00', '罗技', '罗技', '0.90', '39.00');
INSERT INTO `product` VALUES ('5', '罗技木星轨迹球', '3', '182.00', '罗技', '罗技', '0.80', '80.00');
INSERT INTO `product` VALUES ('6', '罗技火星轨迹球', '3', '349.00', '罗技', '罗技', '0.87', '290.00');
INSERT INTO `product` VALUES ('7', '罗技G9X', '3', '680.00', '罗技', '罗技', '0.70', '470.00');
INSERT INTO `product` VALUES ('8', '罗技M215', '2', '89.00', '罗技', '罗技', '0.79', '30.00');
INSERT INTO `product` VALUES ('9', '罗技M305', '2', '119.00', '罗技', '罗技', '0.82', '48.00');
INSERT INTO `product` VALUES ('10', '罗技M310', '2', '135.00', '罗技', '罗技', '0.92', '69.80');
INSERT INTO `product` VALUES ('11', '罗技M505', '2', '148.00', '罗技', '罗技', '0.92', '72.00');
INSERT INTO `product` VALUES ('12', '罗技M555', '2', '275.00', '罗技', '罗技', '0.88', '140.00');
INSERT INTO `product` VALUES ('13', '罗技M905', '2', '458.00', '罗技', '罗技', '0.88', '270.00');
INSERT INTO `product` VALUES ('14', '罗技MX1100', '2', '550.00', '罗技', '罗技', '0.76', '300.00');
INSERT INTO `product` VALUES ('15', '罗技M950', '2', '678.00', '罗技', '罗技', '0.78', '320.00');
INSERT INTO `product` VALUES ('16', '罗技MX Air', '2', '1299.00', '罗技', '罗技', '0.72', '400.00');
INSERT INTO `product` VALUES ('17', '罗技G1', '4', '155.00', '罗技', '罗技', '0.80', '49.00');
INSERT INTO `product` VALUES ('18', '罗技G3', '4', '229.00', '罗技', '罗技', '0.77', '96.00');
INSERT INTO `product` VALUES ('19', '罗技G500', '4', '399.00', '罗技', '罗技', '0.88', '130.00');
INSERT INTO `product` VALUES ('20', '罗技G700', '4', '699.00', '罗技', '罗技', '0.79', '278.00');
INSERT INTO `product` VALUES ('21', '光盘', null, '10.00', '罗技', '罗技', '0.80', '2.00');

1、查询所有货品信息:
	select * from product ;

2、查询所有货品的id、productName、salePrice:
	select id , productName , salePrice from product ;

3、查询商品的分类编号:
	select dir_id  from product ;

4、查询所有商品的id、名称、批发价(salePrice*cutoff):
	select id ,productName , salePrice *cutoff '批发价' from product ;

5、查询所有货品的id、名称、和各进50个的的成本价(costPrice):
	select id , productName , costPrice*50 from product ;

6、查询所有货品的id、名称、各进50个,并且每个运费1元的成本:
	select id , productName , (costPrice +1 )*50 from product ;

7、查询所有货品的id、名称、各进50个,并且每个运费1元的成本(别名allPrice):
	select id , productName , (costPrice +1 )*50  allPrice  from product ;

8、查询商品的分类编号(去除重复):
	select distinct dir_id from product ;

9、查询货品零售价salePrice=119的所有商品信息:
	select * from product where salePrice = 119 ;

10、查询货品名为罗技G9X的所有货品信息:
	select * from product where productName = "罗技G9X" ;

11、查询货品名不为罗技G9X的所有商品信息:
	select * from product where productName != "罗技G9X" ;

12、查询分类编号不等于2的所有货品信息:
	select * from product where dir_id != 2  or dir_id = null ;

13、查询货品名称、零售价(salePrice)小于等于200的货品:
	select productName , salePrice from product where salePrice <=200 ;

14、查询id、货品名称、批发价(salePrice*cutoff)大于350的货品:
	select id , productName , salePrice*cutoff from product where salePrice*cutoff > 350 ;

15、查询id、商品名称 零售价(salePrice)在300-400之间的货品:
	select id , productName , salePrice from product where salePrice between 300 and 400 ;

16、查询id、商品名称 分类为2和4的所有商品:( 不使用in  and  or )
	select id , productName , dir_id from product where dir_id  = 2 or dir_id = 4 ;
	
17、查询id、商品名称、分类编号、salePrice、costPrice的货品零售价大于等于250或者成本大于等于200的商品:
	select id , productName , dir_id , salePrice , costPrice from product where salePrice >= 250 or costPrice >= 200 ;

18、查询id、货品名称,批发价(salePrice*cutoff)在300-400之间的货品,使用区间查询:
	select id , productName ,salePrice*cutoff from product where salePrice*cutoff between 300 and 400 ; 

19、查询id、货品名称,批发价不在300-400之间的货品;使用区间查询:
	select id , productName ,salePrice*cutoff from product where salePrice*cutoff not between 300 and 400 ;

20、查询id、货品名称,分类编号为2,4的所有商品,   使用IN :
	select id , productName , dir_id from product where dir_id in (2,4);

21、查询id、货品名称,分类编号不为2,4的所有商品,使用IN:
	select id ,productName ,dir_id from product where dir_id not in (2,4) ;

22、查询分类编号为空的所有商品信息:判读是否null 需要用is   is null  ;  是不空  is not null 
	select *  from product where dir_id is null ;

23、查询id、货品名称,货品名称匹配'罗技M9_'的商品信息:模糊查询
	select id , productName from product where productName like '罗技M9_';

24、查询id、货品名称、分类编号,零售价大于等于80并且货品名称匹配'%罗技M1_ _':
	select id , productName , dir_id , salePrice from product where salePrice >= 80 and productName like '%罗技M1__';

五、高级查询
1、查询id、货品名称、分类编号、零售价,并且零售价(salePrice)按降序排列:
	select id , productName , dir_id , salePrice from product order by salePrice desc ;

2、查询id、货品名称、分类编号、零售价,并且零售价按升序排列:
	select id , productName , dir_id , salePrice from product order by salePrice  ;

3、查询id、货品名称、分类编号、零售价,先按分类编号升序排列,再零售价按降序排列:
	select id , productName , dir_id ,salePrice from product order by dir_id asc , salePrice desc ;

4、查询M系列的id、货品名称、并按照批发价(salePrice*cutoff)升序排列,批发价使用别名:
	select id , productName ,salePrice*cutoff as '批发价' from product order by salePrice asc  ;

5、查询id、货品名称、分类编号,按分类编号为2的商品按照批发价(salePrice*cutoff)升序排列:
	select id ,productName , dir_id ,salePrice*cutoff pf from product where dir_id = 2 order by pf ;

6、分页查询所有商品信息,每页五条,第一页:
	select * from product limit 0,5 ;

7、分页查询所有商品信息,每页五条,第三页:
	select * from product limit 10,5 ;

8、分页查询所有商品信息,每页五条,第五页:
	select * from product limit 20,5;

9、查询所有商品的平均零售价(salePrice):
	select avg (salePrice) from product ;

10、查询商品总的条数:
	select count(id) from product ;

11、查询分类编号为2的商品总数:
	select count(id) from product where dir_id =2 ;

12、查询商品的最低零售价(salePrice),最高零售价(salePrice),以及所有商品零售价的总和:
	select min(salePrice) , max(salePrice ), sum(salePrice ) from product ;

13、按照零售价(salePrice)升序排列,并设置每页显示5条数据:
	select * from product order by salePrice limit 0,5 ;

CREATE TABLE productdir  (
  id bigint(20) PRIMARY KEY AUTO_INCREMENT,
  dirName varchar(20),
  parent_id bigint(20)
);
-- 添加数据
INSERT INTO productdir VALUES (1, '鼠标', NULL);
INSERT INTO productdir VALUES (2, '无线鼠标', 1);
INSERT INTO productdir VALUES (3, '有线鼠标', 1);
INSERT INTO productdir VALUES (4, '游戏鼠标', 1);

14、查询id、货品名称(productName),以及货品所属分类名称(dirName):
	select product.id , productName ,dirName from product  join productdir on product.dir_id = productdir.id ;

15、查询商品零售价(salePrice)大于200的无线鼠标的所有信息:
	select * from  product join productdir on product.dir_id = productdir.id and salePrice > 200 and dirName = "无线鼠标" ;


CREATE TABLE  productstock  (
   id bigint(20) PRIMARY KEY AUTO_INCREMENT,
   product_id bigint(20),
   storeNum bigint(20),
   lastIncomeDate datetime,
   lastOutcomeDate datetime,
   warningNum bigint(20)
);

INSERT INTO productstock VALUES (1, 1, 182, '2020-04-08 09:22:21', '2020-04-09 09:22:26', 20);
INSERT INTO productstock VALUES (2, 2, 27, '2020-04-10 09:22:46', '2020-04-11 09:22:40', 20);
INSERT INTO productstock VALUES (3, 3, 89, '2020-03-03 09:22:57', '2020-03-12 09:23:05', 20);
INSERT INTO productstock VALUES (4, 5, 19, '2020-04-09 09:23:30', '2020-04-17 09:23:25', 20);
INSERT INTO productstock VALUES (5, 6, 3, '2020-04-07 09:25:05', '2020-04-09 09:25:11', 5);
INSERT INTO productstock VALUES (6, 7, 2, '2020-04-08 09:25:28', '2020-04-10 09:25:24', 3);
INSERT INTO productstock VALUES (7, 8, 120, '2020-04-09 09:25:44', '2020-04-10 09:25:47', 20);
INSERT INTO productstock VALUES (8, 9, 58, '2020-04-07 09:26:03', '2020-04-09 09:25:59', 20);
INSERT INTO productstock VALUES (9, 11, 28, '2020-04-01 09:26:21', '2020-04-15 09:26:26', 20);
INSERT INTO productstock VALUES (10, 12, 8, '2020-04-02 09:26:38', '2020-04-14 09:26:35', 5);
INSERT INTO productstock VALUES (11, 13, 3, '2020-04-03 09:26:53', '2020-04-13 09:26:57', 5);
INSERT INTO productstock VALUES (12, 14, 6, '2020-04-04 09:27:11', '2020-04-12 09:27:07', 5);
INSERT INTO productstock VALUES (13, 15, 2, '2020-04-05 09:27:26', '2020-04-11 09:27:39', 5);
INSERT INTO productstock VALUES (14, 16, 3, '2020-04-06 09:28:04', '2020-04-10 09:28:00', 3);
INSERT INTO productstock VALUES (15, 17, 49, '2020-04-07 09:28:20', '2020-04-09 09:28:25', 20);
INSERT INTO productstock VALUES (16, 18, 14, '2020-04-07 09:28:49', '2020-04-14 09:28:37', 10);
INSERT INTO productstock VALUES (17, 20, 7, '2020-04-06 09:29:09', '2020-04-13 09:29:13', 5);

16、查询商品名称(productName)、每个货品对应的分类名称(dirName)以及对应的库存量(storeNum):
	select productName , dirName , storeNum from product ,productdir ,productstock where product.dir_id= productdir.id and productstock.id = product.id ;


17、如果库存商品销售完成,按照利润(salePrice-costPrice)从高到底查询货品名称(productName),零售价(salePrice),货品分类名称(dirName):
select productName , salePrice , dirName  , (salePrice - costPrice)*storeNum from product  
	join productdir on product.dir_id = productdir.id 
	join productstock on product.id = productstock.product_id 
	order by   (salePrice - costPrice)*storeNum desc ; 


CREATE TABLE emp(
	empno iNT,
	ename VARCHAR(50),
	job VARCHAR(50),
	mgr INT,
	hiredate DATE,
	sal DECIMAL(7,2),
	comm DECIMAL(7,2),
	deptno INT
) ;
INSERT INTO emp VALUES(7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20);
INSERT INTO emp VALUES(7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30);
INSERT INTO emp VALUES(7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30);
INSERT INTO emp VALUES(7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20);
INSERT INTO emp VALUES(7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30);
INSERT INTO emp VALUES(7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30);
INSERT INTO emp VALUES(7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10);
INSERT INTO emp VALUES(7788,'SCOTT','ANALYST',7566,'1987-04-19',3000,NULL,20);
INSERT INTO emp VALUES(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10);
INSERT INTO emp VALUES(7844,'TURNER','SALESMAN',7698,'1981-09-08',1500,0,30);
INSERT INTO emp VALUES(7876,'ADAMS','CLERK',7788,'1987-05-23',1100,NULL,20);
INSERT INTO emp VALUES(7900,'JAMES','CLERK',7698,'1981-12-03',950,NULL,30);
INSERT INTO emp VALUES(7902,'FORD','ANALYST',7566,'1981-12-03',3000,NULL,20);
INSERT INTO emp VALUES(7934,'MILLER','CLERK',7782,'1982-01-23',1300,NULL,10);

18、查询所有的员工信息:
	select * from emp ; 

19、查询每个员工的编号(empno)、姓名(ename)、职位(job):
	select empno , ename ,job from emp ; 


CREATE TABLE dept  (
	DEPTNO bigint(20) PRIMARY KEY ,
	DNAME varchar(30),
	LOC varchar(30)   
);

INSERT INTO dept VALUES (10, 'ACCOUNTING', 'NEWTORK');
INSERT INTO dept VALUES (20, 'RESEARCH', 'DALLAS');
INSERT INTO dept VALUES (30, 'SALES', 'CHICAGO');
INSERT INTO dept VALUES (40, 'OPERATIONS', 'BOSTON');
20、查询所有部门信息:
	select * from dept ; 

21、查询员工的部门编号(deptno)并去除重复:
	select distinct deptno from emp ;

22、查询所有员工的姓名以及对应的年薪:
	select ename , sal*12 from emp ;

23、查询所有员工的姓名以及对应的年薪(使用别名):
	select ename , sal*12 '年薪' from emp ;

24、查询每月都有500元的餐补和200元的交通补贴并且年底多发一个月的工资的年薪

以上是关于mysql 练习100道的主要内容,如果未能解决你的问题,请参考以下文章

Python 简单练习题100道,

MySQL基础篇 | 经典三十四道练习题

Python-100道练习题答案

Mysql 练习题10道(1-10题)

14道Python基础练习题(附答案)

写完Numpy100道基础练习题后的错误总结和语法总结