资讯详情

100道MySQL练习题,帮你熟悉sql语法【前50道】

基础练习


1.启动命令行MySql:

net start MySQL80

2.关闭命令行MySql:

net stop MySQL80

3.使用操作连接MySql数据库:

mysql -u用户名 -p用户名

在命令行中使用命令查看所有数据库:

show databases; 

在命令行中使用命令进入数据库:

use 数据库名称; 

使用命令在命令行中查看指定数据库中的所有表:

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 bigint primary key auto_increment,     name varchar(30),     age int,     email varchar(30) ); 

在这里插入图片描述

10.删除使用命令t_student表:

drop table t_student; 

使用命令创建一个名称order表,列:id(主键和自增),name、age:

create table `order`(     id bigint primary key auto_increment,     name varchar(30),     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
('李四', 19, '20000@qq.com'),
('赵六', 18, '40000@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@';

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 as 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;

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>=300 and salePrice<=400;

16、查询id、商品名称 分类为2和4的所有商品:

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、货品名称,批发价在300-400之间的货品,使用区间查询:

select id,productName,salePrice from product where salePrice between 300 and 400;

19、查询id、货品名称,批发价不在300-400之间的货品;使用区间查询:

select id,productName,salePrice from product where salePrice 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、查询分类编号为空的所有商品信息:

select id,productName,dir_id 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__';

标签: m125dc2labb继电器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台