数据库
1.创建库
create database databook
2 自定义类Book
use databook
create table book(
id INT PRIMARY KEY auto_increment,
bookname VARCHAR(30),
price int,
author VARCHAR(30),
publish VARCHAR(30)
)
/*
2)向表里添加10条数据
*/
INSERT INTO book(bookname,price,author,publish) VALUES
('北平无故事',25,'刘和平','作家出版社'),
('人间失格',16,'太宰治著','作家出版社'),
('高兴',14,'贾平凹','人民出版社'),
('源氏物语',57,'刘和平','人民出版社'),
('卡夫卡文集',9,'卡夫卡','邮电出版社'),
('大家',12,'王蒙','邮电出版社'),
('拉片子',37,'杨健','清华出版社'),
('古代散文',5,'归有光','安徽出版社'),
('百花散文',6,'孙虹选','百花文艺出版社'),
('方令孺散文集',5,'方令孺','安徽文艺')
-- 3查询所有图书的信息,并按价格降序显示
SELECT * FROM book where price order by price asc
-- 4)查询所有作家出版社的图书信息,并按价格降序显示
SELECT author FROM book where price order by price desc
-- 5查询出所有刘和平的图书信息 ,并输出
SELECT * FROM book where author = '刘和平'
-- 6)删除ID是2的记录,如果没有相关记录则提示
DELETE from book where id = 2
SELECT * from book
-- 7)将所有价格不足10元的图书调到10元,并查看信息
UPDATE book set price = 10 where price <10
-- 8查看所有图书的价格情况,并升序显示
SELECT price from book where price order by price asc
-- 9查看所有价格低于20元的图收信息
select publish from book where price <=20
-- A--------------------------------------
-- 1.创建test数据库
create database datatest
use datatest
-- 2.在test数据库中创建student表id设置为主键自增长
create table student(
id int primary key auto_increment,
name varchar(20),
score int,
address varchar(50),
usermail varchar(20)
)
-- 3.向student表中添加记录
insert into student(id,name,score,address,usermail)
values(1,'张三',98,'北京','111111111@qq.com'),
(2,'李四',88,'上海','111111112@qq.com'),
(3,'王五',78,'广州','111111113@qq.com'),
(4,'赵六',68,'深圳','111111114@qq.com'),
(5,'孙七',58,'杭州','111111115@qq.com'),
(6,'小红',48,'北京','111111116@qq.com'),
(7,'小黑',99,'上海','111111117@qq.com'),
(8,'小绿',100,'杭州','111111118@qq.com'),
(9,'小粉',60,'杭州','111111119@qq.com'),
(10,'小紫',70,'黑龙江','111111110@qq.com')
-- 4.使用sql语句查询出表中的所有内容
select * from student
-- 5.使用sql语句查询出表中所有同学的id,name,score
select id,name,score from student
-- 6.更改useremail字段的数据类型为varchar(50)
alter table student modify usermail varchar(50)
-- 7.向表中添加一个字段,字段名称为“pingjia”,字段类型为varchar(20)
alter table student add pingjia varchar(20)
-- 8.更改姓名是张三的同学的分数为88
update student set score='88' where name=‘张三’
-- 9.如果80分为及格线,查询出所有及格的同学的详细信息
select * from student where score>=80
-- 10.把姓名是“小红”的同学的分数在原来的基础上+40
update student set score=score+40 where name='小红'
-- 11.使用关键字in,查询id值是1或5或7的同学的基本信息
select * from student where id in(1,5,7)
-- 12.查询id值在5至8的所有同学的基本信息
select * from student id where id>=5 and id<=8
-- 13.查询姓名是小红并且分数大于60的同学的基本信息
select * from student where name='小红' and score>60
-- 14.查询姓名是小红或者分数大于90的同学的基本信息
select * from student where name='小红' or score>90
-- 15.查询score字段值是NULL的同学的基本信息
select * from student where score='null'
-- 16.查询score字段值不是NULL的同学的id和name
select id,name from student where score is not null
-- 1.创建db_test数据库
CREATE database db_test
-- 进入数据库
use db_test
-- 2.在test数据库中创建yuangong表,表中添加如下记录
CREATE TABLE yuangong(
sid INT PRIMARY KEY AUTO_INCREMENT,
sname VARCHAR(20) NOT NULL,
sex VARCHAR(4) DEFAULT '男',
job VARCHAR(20)NOT NULL,
birthday DATE,
salary DECIMAL,
comm DECIMAL,
widthhold INT
)
INSERT INTO yuangong (sid,sname,sex,job,birthday,salary,comm,widthhold)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',1960,700,150)
-- 4 修改表名为emp
alter TABLE yuangong RENAME emp
-- 5.向表中添加字段Hobby,设置类型为varchar(50),设置唯一约束
ALTER TABLE emp add hobby VARCHAR(50) unique
-- 6 使用desc语句查看表结构
show create table emp
-- 7 向表中添加记录,字段对应值分别为(1005,林青霞,女,架构师,1969-12-12,8000,NULL,100,阅读)
INSERT INTO emp VALUES(1005,'林青霞','女','架构师','1969-12-12',8000,NULL,100,'阅读')
-- 8 修改sname字段的类型为varchar(20)
ALTER TABLE emp MODIFY sname VARCHAR(20)
-- 9 查询表中sid字段的值从是1002或1003或1005员工的所有记录(使用关键字in)
SELECT * from emp where sid = 1002 OR sid = 1003 OR sid = 1005
-- 10 修改表中job值是高级工程师员工的job为“架构师”
UPDATE emp SET job='架构师' WHERE job='高级程师'
-- 11 删除表中sid是1003并且job是王五的员工的记录
DELETE FROM emp where sid = 1003 and job = '王五'
-- 12 修改表中姓名是1004员工的salary在原来的基础上-300
UPDATE emp set salary = salary - 300 WHERE sid = 1004
SELECT * FROM emp