星星博客 »  > 

SQL数据库代码

1.创建数据库test1    create database test1;
2.删除数据库test1     drop database test1;
3.展示有哪些数据库    show database;
4.切换到用test1数据库  use test1;
5.建表
--学生表
CREATE TABLE `Student`(
`s_id` VARCHAR(20),
`s_name` VARCHAR(20) NOT NULL DEFAULT '',
`s_birth` VARCHAR(20) NOT NULL DEFAULT '',
`s_sex` VARCHAR(10) NOT NULL DEFAULT '',
PRIMARY KEY(`s_id`)
);
--课程表
CREATE TABLE `Course`(
`c_id` VARCHAR(20),
`c_name` VARCHAR(20) NOT NULL DEFAULT '',
`t_id` VARCHAR(20) NOT NULL,
PRIMARY KEY(`c_id`)
);
--教师表
CREATE TABLE `Teacher`(
`t_id` VARCHAR(20),
`t_name` VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(`t_id`)
);
--成绩表
CREATE TABLE `Score`(
`s_id` VARCHAR(20),
`c_id` VARCHAR(20),
`s_score` INT(3),
PRIMARY KEY(`s_id`,`c_id`)
);
5.插入数据
--插入学生表测试数据
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
--课程表测试数据
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');

--教师表测试数据
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

--成绩表测试数据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);
或者可以写上字段插入数据
insert into [员工$] (编号,姓名,年龄,报读专业) values (‘B007’,’陈功’,37,’函数’)
更改表数据
Update [员工$] set 姓名=’刘姐’ where 姓名=’刘三姐’        将刘三姐改成刘姐
Update [员工$] set 学费=学费-100 where 年龄>=30     年龄大于30的人学费减少200
6.查询出data表中:中文名,职业,城市三个字段的记录
Select 中文名,职业,城市 from [data$]
7.提取所有字段 select * from [data$]
8.读取A1到E4区域表格的所有字段内容
Select * from [data$A1:E4]
9.提取不重复的记录
Select distinct 中文 from [data$]
Select distinct * from [data$]
10.查询出类型是笔记本的销售记录和单价大于6000的记录,销售日期大于2018/1/9的销售数据
Select * from [data$] where 类型=”笔记本”
Select * from [data$] where 单价>6000
Select * from [data$] where 销售日期>=#2018/1/9#
Select * from [data$] where 北京+上海+南京>20
11.字段求和运算
Select *,北京+上海+南京 as 销量 from [data$]
12.And,ornull()使用
Select * from [data$] where 北京 is not null or 上海 is not null and 成都 is null
13.in的使用
Select * from [data$] where 品牌 in(“IBM”,”联想”) and类型 in(“台式”,”笔记本”)
14.between and的使用
Select * from [data$] where 销售日期 between #2020/1/1# and #2020/12/31#
16.like模糊查询的使用
Select * from [data$] where 姓名 like “李%Select * from [data$] where 姓名 not like “李%Select * from [data$] where 姓名  like%梅”
Select * from [data$] where 姓名  like%%Select * from [data$] where 姓名  like “李_”
Select * from [data$] where 英文名  like[a-cw-z]%Select * from [data$] where 出生年月  like198[0-2]%Select * from [data$] where 姓名  like[张李陈王]%Select * from [data$] where 身份证号  like%[!13579]
15.连字符&
Select * from [data$] where 月份&品名=1月份和尚头”
16.嵌套查询
Select * from [data$] where 姓名 like (Select * from [基础讲解$J5:J6])
Select * from (Select *,北京+上海+成都 from [data2$]) where expr1000 between 20 and 30
17.排序,取前10Select * from [data$]  order by 总分 asc                           升序
Select * from [data$]  order by 总分 desc                          降序  
Select top 10 * from [data$]  order by 总分 desc
18.分组
Select 品名,型号 from [data$] group by 品名,型号
Select distinct 品名,型号 from [data$]
19.重命名as
Select t1.品牌 from [data$] as t1
20.多表合并
所有的都合并,重复的也有在其中
Select * from [1月份$] union all Select * from [2月份$] union all Select * from [3月份$]
去除重复记录的合并
Select * from [1月份$] union  Select * from [2月份$] union  Select * from [3月份$]
21.多表链接
Select * from [产品资料$] as t1,[销量表$] as t2 where t1.编号=t2.编号
Select t1.工号,姓名,城市,工资 from [员工表$] as t1,[工资表$] as t2 where t1.工号=t2.工号
Select t3.工号,姓名,工资,分红比例 from [员工表$] as t1,[工资表$] as t2,[分红表$] as t3 where t1.工号=t2.工号 and t2.工号=t3.工号
Select 姓名,业绩,提成点数,业绩*提成点数 as 提成金额 from [业绩表$] t1,[提成表$] t2 where t1.业绩 between t2.临界起点 and t2.临界终点
内连接
Select t1.姓名,t1.总分 as 期中总分,t2.总分 as 期末总分 from [期中表$] t1 inner join [期末表$] t2 on t1.姓名=t2.姓名
外连接
Select t1.姓名,t1.工资,t2.奖金 from [工资$] t1 left outer join [奖金$] t2 on t1.姓名=t2.姓名
Select t1.姓名,t1.工资,t2.奖金 from [工资$] t1 right outer join [奖金$] t2 on t1.姓名=t2.姓名
22.IIF语句
Select *,iif(month(销售日期)>6,”下半年”,”上半年”) as 2012from [电脑销售表$]
23.Datediff,dateadd语句,求出员工工龄
Select 员工姓名,入职日期,datediff(“yyyy”,入职日期,date()) as 工龄 from [data$]
Select dateadd(“m”,1,now())-now()       本月余下的天数
24.switch选择函数
Select *,switch(成绩>=90,”优秀”,成绩>=80,”良”,成绩>=60,”中”,成绩<60,”差”) as 等级 from [成绩表$]
25.有特殊符号的字段名 个/箱 和INT取整函数
Select *,int(数量/[/]) as 箱数 from [data$]
26.left函数,right函数
Select 员工编号,left(姓名,1) as 姓氏 from [data$]
Select 编号,riht(000&编号,4) as 修正编号 from [data$]
27.choose函数
Select *,choose(分数/10,”差”,”差”,”差”,”差”,”差”,”中”,”中”,”良”,”优”,”优”) as 等级 from [成绩表$]
28.MID函数
Select *,mid(“差差差差差中中良优优”,分数/10,1) as 等级 from [成绩表$]
Select 身份证号,iif(mid(身份证号,15,3) mod 2,”男”,”女”) as 性别 from [data$]
29.instr查找函数
Select instr(2,123456789y”,”y”)123456789y的第二个字符开始查找y
Select instr(地址,”市”) from [data$]
30.replace替换函数
Select sum(replace(工作时间,”小时”,””)) as 总小时数 from [data$]   把工作时间里的”小时”替换为空文本
31.trim去除两边空格函数
Select trim(right(repkace(编号,-,”   ”))) as 尺码 from [data$]
32.string重复函数
Select string(6,”好”)
33.format格式函数看笔记内容太多
Select *,format(销售金额-成本金额,”√盈利;×亏损;-平稳”) from [data$]
34.条件分组having
Select 农作物,sum(产量) as 总产量 from [data$] group by 农作物 having sum(产量)>1000
35.transform数据透视表函数
Transform sum(销售额) select 销售地区 from [data$] group by 销售地区 pivot 产品名称        列标签:产品名称,行标签:销售地区,值是sum(销售额)

相关文章