sql语句

2.在test数据库中创建student表,表结构如下图所示(id设置为主键自增长)

CREATE TABLE student(

sid INT PRIMARY KEY AUTO_INCREMENT,

sname VARCHAR(20),

score INT,

address VARCHAR(50),

useremail VARCHAR(20)

);

//向student表中添加记录如下图所示

INSERT INTO student VALUES(1,'张三',98,'北京','[email protected]'),

(2,'李四',88,'上海','[email protected]'),

(3,'王五',78,'广州','[email protected]'),

(4,'赵六',68,'深圳','[email protected]'),

(5,'孙七',58,'杭州','[email protected]'),

(6,'小红',48,'北京','[email protected]'),

(7,'小黑',99,'上海','[email protected]'),

(8,'小绿',100,'杭州','[email protected]'),

(9,'小粉',60,'杭州','[email protected]'),

(10,'小紫',70,'黑龙江','[email protected]');

//4.使用SQL语句查询出表中的所有内容

SELECT * FROM student

//5.使用SQL语句查询出表中所有同学的id,NAME,score

SELECT sid,sname,score FROM student

//6.更改useremail字段的数据类型为VARCHAR(50)

ALTER TABLE student MODIFY COLUMN useremail VARCHAR(50)

//7.向表中添加一个字段,字段名称为“pingjia”,字段类型为VARCHAR(20)

ALTER TABLE student ADD COLUMN 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 sid IN(1,5,7);

//12.查询id值在5至8的所有同学的基本信息

SELECT * FROM student sid WHERE sid>=5 AND sid<=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='NULL'

//16.查询score字段值不是NULL的同学的id和NAME

SELECT sid,sname FROM student WHERE score IS NOT NULL






1.创建db_test数据库(10分)

CREATE DATABASE db_test

1.选择db_test数据库(5分)

USE db_tsst

在test数据库中创建yuangong表,表中添加如下记录,表结构参考下方

CREATE TABLE yuangong(

sid INT(10) PRIMARY KEY AUTO_INCREMENT,

sname VARCHAR(50),

sex ENUM('男','女') NOT NULL DEFAULT'男',

job VARCHAR(50),

birthday DATE,

salary INT,

come INT,

withhold INT

);

INSERT INTO yuangong VALUES(1001,'张三','男','高级工程师','1975-1-1',2200,1100,200),

(1002,'李四','女','助工','1985-1-1',1200,1100,100),

(1003,'王五','男','工程师','1978-11-11',1900,110,200),

(1004,'赵六','男','工程师','1979-1-1',19600,1100,150);

2.修改表名为”emp”(10分)

ALTER TABLE yuangong RENAME TO emp;

向表中添加字段Hobby,设置类型为VARCHAR(50),设置唯一约束

ALTER TABLE emp ADD hobby VARCHAR(50)UNIQUE

4.使用DESC语句查看表结构(5分)

DESC emp

向表中添加记录,字段对应值分别为(1005,林青霞,女,架构师,1969-12-12,8000,NULL,100,阅读)(5分)

INSERT INTO emp VALUES(1005,'林青霞','女','架构师','1969-12-12',8000,NULL,100,'阅读')

6.修改sname字段的类型为VARCHAR(20)(5分)

ALTER TABLE emp MODIFY sname VARCHAR(20)

7.查询表中sid字段的值从是1002或1003或1005员工的所有记录(使用关键字IN)(5分)

SELECT * FROM emp WHERE sid IN(1002,1003,1005);

8.修改表中job值是高级工程师员工的job为“架构师”(5分)

UPDATE emp SET job='构架师'WHERE job LIKE'高级工程师'

9.删除表中sid是1003并且job是王五的员工的记录(10分)

DELETE FROM emp WHERE sid=1003 AND sname='王五'

10.修改表中姓名是1004员工的salary在原来的基础上-300(10分)

UPDATE emp SET salary=salary-300 WHERE (sid=1004)






创建一个学校数据库(jiyun)

CREATE DATABASE jiyun CHARACTER SET utf8 COLLATE utf8_general_ci

创建课程(lesson)

CREATE TABLE lesson(

  id  INT PRIMARY KEY AUTO_INCREMENT,

  NAME VARCHAR(10)

)

创建一个班级(cls),包含字段

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')

1、'python高级'

2、'数据库vue'

在班级表中插入6条记录,

INSERT INTO cls(NAME,number,lesson_id)

  VALUES('1905A',22,1),

('1905A',44,1),

('1905A',33,1),

('1905A',18,2),

('1905A',48,2),

('1905A',56,2)

查询类型为 'python高级' 的所有班级、人员数量

SELECT * FROM cls WHERE lesson_id IN(SELECT id FROM lesson WHERE NAME='python高级')

查询每个班级中 人数最多、最少

SELECT MAX(number) AS '人数最多',MIN(number) AS'人数最少' FROM cls

查询所有人员数量大于平均人数的班级,并且按班级人数降序排序

SELECT * FROM cls WHERE number < (SELECT AVG(number) FROM cls) ORDER BY number DESC

修改cls表中'1902A'班级数量为44

UPDATE cls SET number='44' WHERE NAME='1902A'





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

你可能感兴趣的:(sql语句)