-- 4.使用sql語句查詢出表中的所有內(nèi)容
SELECT * FROM student
-- 5.使用sql語句查詢出表中所有同學(xué)的id,name,score
SELECT id,sname,score FROM student
-- 6.更改useremail字段的數(shù)據(jù)類型為varchar(50)
alter table student modify column useremail VARCHAR(50)
-- 7.向表中添加一個(gè)字段酱讶,字段名稱為“pingjia”,字段類型為varchar(20)
ALTER TABLE student ADD pingjia VARCHAR(20)
-- 8.更改姓名是張三的同學(xué)的分?jǐn)?shù)為88
UPDATE student SET score=88 WHERE sname="張三"
-- 9.如果80分為及格線易桃,查詢出所有及格的同學(xué)的詳細(xì)信息
SELECT * from student WHERE score>80
-- 10.把姓名是“小紅”的同學(xué)的分?jǐn)?shù)在原來的基礎(chǔ)上+40
UPDATE student SET score=score+40 WHERE sname="小紅"
-- 11.使用關(guān)鍵字in,查詢id值是1或5或7的同學(xué)的基本信息
SELECT * FROM student WHERE id in(1,5,7)
-- 12.查詢id值在5至8的所有同學(xué)的基本信息
SELECT * FROM student where id>=5 AND id<=8
-- 13.查詢姓名是小紅并且分?jǐn)?shù)大于60的同學(xué)的基本信息
SELECT * FROM student WHERE sname="小紅" AND score>60
-- 14.查詢姓名是小紅或者分?jǐn)?shù)大于90的同學(xué)的基本信息
SELECT * FROM student WHERE sname="小紅" OR score>90
-- 15.查詢score字段值是NULL的同學(xué)的基本信息
SELECT * FROM student WHERE score is NULL
-- 16.查詢score字段值不是NULL的同學(xué)的id和name
SELECT id,sname FROM student WHERE score is NOT NULL
----------------------------------------------------------------------------------------------------------------
-- 創(chuàng)建課程(lesson)
-- id(整形主鍵)name(課程名稱)
CREATE TABLE lesson(
id INT PRIMARY KEY auto_increment,
`name` VARCHAR(20)
)
-- 創(chuàng)建一個(gè)班級(jí)(cls)曙搬,包含字段
-- id(整形主鍵)
-- name(班級(jí)名稱)
-- number(班級(jí)人數(shù))
-- lesson_id(課程名稱id)
CREATE TABLE cls(
id INT PRIMARY KEY auto_increment,
`name` varchar(20),
number int,
lesson_id int
)
-- 在lesson表中插入兩數(shù)據(jù)
INSERT into lesson(name) VALUES('python高級(jí)'),('數(shù)據(jù)庫vue')
-- 在班級(jí)表中插入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高級(jí)' 的所有班級(jí)贸伐、人員數(shù)量less
SELECT c.name,c.number FROM cls c,lesson l WHERE c.lesson_id=l.id AND l.name="python高級(jí)"
-- 查詢每個(gè)班級(jí)中 人數(shù)最多抡草、最少
SELECT max(number),min(number) FROM cls c,lesson l WHERE c.lesson_id=l.id
-- 查詢所有人員數(shù)量大于平均人數(shù)的班級(jí),并且按班級(jí)人數(shù)降序排序
SELECT c.name FROM cls c WHERE number>(SELECT avg(number) FROM cls)ORDER BY number desc
-- 修改cls表中'1902A'班級(jí)數(shù)量為44
UPDATE cls SET number=44 WHERE name="1902A"
------------------------------------------------------------------------------------------------------------------------------------
-- 2. 在test數(shù)據(jù)庫中創(chuàng)建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,"張三","男","高級(jí)工程師","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,設(shè)置類型為varchar(50),設(shè)置唯一約束
ALTER TABLE emp ADD hobby VARCHAR(50) not null
-- 4. 使用desc語句查看表結(jié)構(gòu)
DESC emp
-- 5. 向表中添加記錄,字段對(duì)應(yīng)值分別為(1005提鸟,林青霞军援,女,架構(gòu)師称勋,1969-12-12胸哥,8000,NULL赡鲜,100空厌,閱讀)
INSERT INTO emp VALUES(1005,"林青霞","女","架構(gòu)師","1969-12-12",8000,NULL,100,"閱讀")
-- 6. 修改sname字段的類型為varchar(20)
ALTER TABLE emp MODIFY COLUMN sname varchar(20)
-- 7. 查詢表中sid字段的值從是1002或1003或1005員工的所有記錄(使用關(guān)鍵字in)
SELECT * FROM emp WHERE sid=1002 OR sid=1003 or sid=1005
-- 8. 修改表中job值是高級(jí)工程師員工的job為“架構(gòu)師”
UPDATE emp SET job="架構(gòu)師" WHERE job="高級(jí)工程師"
-- 9. 刪除表中sid是1003并且job是王五的員工的記錄
DELETE FROM emp WHERE sid=1003 AND job="王五"
-- 10. 修改表中姓名是1004員工的salary在原來的基礎(chǔ)上-300(
UPDATE emp SET salary=salary-300 WHERE sid="1004"
---------------------------------------------------------------------------------------------------------------------------
-- 自定義類Book,表名為book银酬,表包含以下字段:
--? id? ? (主鍵嘲更,自增長)
-- bookname (書名,不能為空)
-- price? (價(jià)格揩瞪,不能為空)
-- 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分)查詢所有圖書的信息赋朦,并按價(jià)格降序顯示。
SELECT * FROM book ORDER BY price DESC
-- 4) (10分)查詢所有作家出版社的圖書信息,并按價(jià)格降序顯示
SELECT *FROM book WHERE publish="作家出版社" ORDER BY price DESC
-- 5) (10分)查詢出所有劉和平的圖書信息 北发,并輸出
SELECT *FROM book WHERE author="劉和平"
-- 6) (10分)刪除ID是2的記錄,如果沒有相關(guān)記錄則提示
DELETE FROM book WHERE id=2
-- 7) (10分)將所有價(jià)格不足10元的圖書調(diào)到10元喷屋,并查看信息
UPDATE book SET price=price+10 WHERE price<10
-- 8) (10分)查看所有圖書的價(jià)格情況琳拨,并升序顯示
SELECT bookname,price FROM book ORDER BY price asc
-- 9) (10分)查看所有價(jià)格低于20元的圖收信息
SELECT * FROM book WHERE price<20
-- 10) (10分)所有圖書的價(jià)格上調(diào)20%,并查看信息
UPDATE book SET price=price*1.2
SELECT * FROM book