sql query 学习代码-1

现在学习SQL的小伙伴可以参照我总结的代码敲一遍,这样基本就可以入门了。

切换数据库
--切换数据库
use DBTEST;
创建表
--创建表基本语法
--create table 表名
--(
--	字段名一 数据类型
--	字段名二 数据类型
--	字段名三 数据类型
--)
--建表(部门,职级,员工)
判断表是否存在,如已存在删除该表
if exists(select * from sys.objects where name='Department' and type='U')
	drop table Department
创建一个表,设置主键,设置字符长度,并设置不允许为空(默认允许为空),设置文本框
create table[rank]
(
	--部门编号,primary key:主键 identity:自动增长,初始值为1,增长的步长为1
	RankId int primary key identity(1,1),
	--部门名称,
	RankName nvarchar(50) not null,
	--部门描述
	RankRemark text
)
添加列
--(1)添加列
--alter table 表名 add 新列名 数据类型
--给员工表添加一列邮箱
alter table People add PeopleMail varchar(200)
修改列
--(3)修改列
--alter table 表名 alter column 列名 数据类型
--修改地址varchar(300)为varchar(200)
alter table People alter column PeopleAddress varchar(200);
删除约束
--alter table 表名 drop constraint 约束名
--删除月薪的约束
alter table People drop constraint CK__People__PeopleSa__6383C8BA;
添加约束(check约束)
--alter table 表名 add constraint 约束名 check(表达式);
--添加工作约束,工资必须在1000-1000000之间
alter table People add constraint CK__People__PeopleSa__6383C8BA
check(PeopleSalary>=1000 and PeopleSalary<=1000000);
--添加约束(主键)
--  alter table 表名 add constraint 约束名 primary key(列名)
--添加约束(唯一)
--alter table 表名 add constraint 约束名 uunique(列名)
--添加约束(默认值)
--alter table 表名 add constraint 约束名 default 默认值 for 列名
--外键约束
--alter table 表名 add constraint 约束名 foregin key(列名) references 关联表名(列名(主键))
插入数据(单行数据)
insert into Department(DepartmentName,DepartmentRemark)
values('市场部','......')

insert into Department(DepartmentName,DepartmentRemark)
values('软件部','......')
insert into Department(DepartmentName,DepartmentRemark)
values('企划部','......')
--简写
insert into Department values('硬件部','......')
插入数据(多行)
--一次性插入多行数据
insert into Department(DepartmentName,DepartmentRemark)
select '测试部','......' union
select '实施部','......' union
select '产品部','......' 
更新数据
--update 表名 set 字段1=值1,字段2=值2 where 条件
--工资调整,每个人加薪1000元
update People set PeopleSalary=PeopleSalary+1000;
update People set PeopleSalary=PeopleSalary+500
where PeopleId=7;
查询所有表
select * from Department;
select * from People;
删除
--删除市场部(部门编号3)中工资大于2万的人
delete from People where  PeopleSalary>10000;
删除特点
--关于删除
--drop table People --删除表对象
--truncate table People --删除数据(清空数据),表对象即表结构依然存在
--truncate清除所有数据不能有条件 delete可以删除所有数据也可以带条件删除
--自动编号:
--假设表中自动编号为1,2,3,4,5
--使用truncate清空数据之后添加数据,编号依然为1,2,3,4,5
--使用delete删除数据,删除的编号不存在
--如果使用delete删除了所有数据之后添加数据,编号为6,7,8,9,10
查询
--查询指定列(姓名,性别,生日,月薪,电话) 
select PeopleName,PeopleSex,PeopleBirth,PeopleSalary,PeoplePhone from People
--查询指定列(姓名,生日,月薪,电话)(显示中文列名)
select PeopleName 姓名,PeopleSex 性别,PeopleBirth 生日,PeopleSalary 月薪,PeoplePhone 电话
from People
--查询出员工所在城市(不需要重复数据显示)
select distinct( PeopleAddress) from People;
--假设准备加工资(上调20%),查询出加工资后的员工数据
select PeopleName,PeopleSex,PeopleSalary*1.2 加薪后的工资 from People;
--查询月薪大于等于10000员工,或者月薪大于等于80000的女员工
select * from People where PeopleSalary>=10000 or (PeopleSalary>=8000 and PeopleSex='女');
select * from People where PeopleSalary between 10000 and 20000;
--查询出地址在武汉或者北京的员工信息
select * from People where PeopleAddress='武汉'or PeopleAddress='北京';
select * from People where PeopleAddress in('武汉','北京');
排序
--排序
--查询所有员工信息,根据工资排序,降序
select * from People order by PeopleSalary desc
--升序
select * from People order by PeopleSalary asc
--查询所有员工信息,根据名字长度排序(降序)
select * from People order by len(PeopleName) desc
--查询工资最高的10%的员工信息
select top 10 percent * from People order by PeopleSalary desc

这些是第一部分,基本查询语句已经解决了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值