-- 4.使用sql语句查询出表中的所有内容
SELECT * FROM student
-- 5.使用sql语句查询出表中所有同学的id,name,score
SELECT id,sname,score FROM student
-- 6.更改useremail字段的数据类型为varchar(50)
alter table student modify column useremail VARCHAR(50)
-- 7.向表中添加一个字段,字段名称为“pingjia”,字段类型为varchar(20)
ALTER TABLE student ADD pingjia VARCHAR(20)
-- 8.更改姓名是张三的同学的分数为88
UPDATE student SET score=88 WHERE sname="张三"
-- 9.如果80分为及格线,查询出所有及格的同学的详细信息
SELECT * from student WHERE score>80
-- 10.把姓名是“小红”的同学的分数在原来的基础上+40
UPDATE student SET score=score+40 WHERE sname="小红"
-- 11.使用关键字in,查询id值是1或5或7的同学的基本信息
SELECT * FROM student WHERE id in(1,5,7)
-- 12.查询id值在5至8的所有同学的基本信息
SELECT * FROM student where id>=5 AND id<=8
-- 13.查询姓名是小红并且分数大于60的同学的基本信息
SELECT * FROM student WHERE sname="小红" AND score>60
-- 14.查询姓名是小红或者分数大于90的同学的基本信息
SELECT * FROM student WHERE sname="小红" OR score>90
-- 15.查询score字段值是NULL的同学的基本信息
SELECT * FROM student WHERE score is NULL
-- 16.查询score字段值不是NULL的同学的id和name
SELECT id,sname FROM student WHERE score is NOT NULL
----------------------------------------------------------------------------------------------------------------
-- 创建课程(lesson)
-- id(整形主键)name(课程名称)
CREATE TABLE lesson(
id INT PRIMARY KEY auto_increment,
`name` VARCHAR(20)
)
-- 创建一个班级(cls),包含字段
-- id(整形主键)
-- name(班级名称)
-- number(班级人数)
-- lesson_id(课程名称id)
CREATE TABLE cls(
id INT PRIMARY KEY auto_increment,
`name` varchar(20),
number int,
lesson_id int
)
-- 在lesson表中插入两数据
INSERT into lesson(name) VALUES('python高级'),('数据库vue')
-- 在班级表中插入6条记录,
INSERT into cls(name, number, lesson_id) VALUES
('1905A',22,1),
('1906A',44,1),
('1904A',33,1),
('1901A',18,2),
('1902A',48,2),
('1903A',56,2)
-- 查询类型为 'python高级' 的所有班级、人员数量less
SELECT c.name,c.number FROM cls c,lesson l WHERE c.lesson_id=l.id AND l.name="python高级"
-- 查询每个班级中 人数最多、最少
SELECT max(number),min(number) FROM cls c,lesson l WHERE c.lesson_id=l.id
-- 查询所有人员数量大于平均人数的班级,并且按班级人数降序排序
SELECT c.name FROM cls c WHERE number>(SELECT avg(number) FROM cls)ORDER BY number desc
-- 修改cls表中'1902A'班级数量为44
UPDATE cls SET number=44 WHERE name="1902A"
------------------------------------------------------------------------------------------------------------------------------------
-- 2. 在test数据库中创建yuangong表,表中添加如下记录,表
CREATE TABLE yuangong(
sid INT PRIMARY KEY auto_increment,
sname varchar(20) not NULL,
sex VARCHAR(20) DEFAULT '男',
job VARCHAR(20) not NULL,
birthday date,
salary int,
comm int,
withhold int)
INSERT INTO yuangong(sid,sname,sex,job,birthday,salary,comm,withhold)
VALUES(1001,"张三","男","高级工程师","1975-1-1",2200,1100,200),
(1002,"李四","女","助工","1985-1-1",1200,200,100),
(1003,"王五","男","工程师","1978-11-11",1900,700,200),
(1004,"赵六","男","工程师","1979-1-1",1950,700,150)
-- 2. 修改表名为”emp”
ALTER TABLE yuangong rename emp
-- 3. 向表中添加字段Hobby,设置类型为varchar(50),设置唯一约束
ALTER TABLE emp ADD hobby VARCHAR(50) not null
-- 4. 使用desc语句查看表结构
DESC emp
-- 5. 向表中添加记录,字段对应值分别为(1005,林青霞,女,架构师,1969-12-12,8000,NULL,100,阅读)
INSERT INTO emp VALUES(1005,"林青霞","女","架构师","1969-12-12",8000,NULL,100,"阅读")
-- 6. 修改sname字段的类型为varchar(20)
ALTER TABLE emp MODIFY COLUMN sname varchar(20)
-- 7. 查询表中sid字段的值从是1002或1003或1005员工的所有记录(使用关键字in)
SELECT * FROM emp WHERE sid=1002 OR sid=1003 or sid=1005
-- 8. 修改表中job值是高级工程师员工的job为“架构师”
UPDATE emp SET job="架构师" WHERE job="高级工程师"
-- 9. 删除表中sid是1003并且job是王五的员工的记录
DELETE FROM emp WHERE sid=1003 AND job="王五"
-- 10. 修改表中姓名是1004员工的salary在原来的基础上-300(
UPDATE emp SET salary=salary-300 WHERE sid="1004"
---------------------------------------------------------------------------------------------------------------------------
-- 自定义类Book,表名为book,表包含以下字段:
-- id (主键,自增长)
-- bookname (书名,不能为空)
-- price (价格,不能为空)
-- author (作者)
-- publish (出版社)
CREATE TABLE book(
id int PRIMARY KEY auto_increment,
bookname VARCHAR(20) not NULL,
price INT NOT null,
author varchar(20),
publish VARCHAR(20)
)
INSERT INTO book(bookname,price,author,publish)
VALUES("北平无故事","25","刘和平","作家出版社"),
("人间失格","16","太宰治著","作家出版社 "),
("高兴","14","贾平凹","人民出版社"),
("源氏物语","57","刘和平","人民出版社"),
("卡夫卡文集","9","卡夫卡","邮电出版社"),
("大家 ","12","王蒙","邮电出版社"),
("拉片子","37","杨健","清华出版社"),
("古代散文","5","归有光","安徽出版社"),
("百花散文","6","孙虹选","百花文艺出版社"),
("方令孺散文集","5","方令孺","安徽文艺")
-- 3) (10分)查询所有图书的信息,并按价格降序显示。
SELECT * FROM book ORDER BY price DESC
-- 4) (10分)查询所有作家出版社的图书信息,并按价格降序显示
SELECT *FROM book WHERE publish="作家出版社" ORDER BY price DESC
-- 5) (10分)查询出所有刘和平的图书信息 ,并输出
SELECT *FROM book WHERE author="刘和平"
-- 6) (10分)删除ID是2的记录,如果没有相关记录则提示
DELETE FROM book WHERE id=2
-- 7) (10分)将所有价格不足10元的图书调到10元,并查看信息
UPDATE book SET price=price+10 WHERE price<10
-- 8) (10分)查看所有图书的价格情况,并升序显示
SELECT bookname,price FROM book ORDER BY price asc
-- 9) (10分)查看所有价格低于20元的图收信息
SELECT * FROM book WHERE price<20
-- 10) (10分)所有图书的价格上调20%,并查看信息
UPDATE book SET price=price*1.2
SELECT * FROM book