SQL 语句

  1. 创建、删除数据库
create database dbname
on primary --主文件组
(
	name = 'dbname_data', --逻辑文件名
	filename = 'd:\project\dbname_data.mdf',
	size = 5,
	maxsize =100,
	filegrowth = 10%  --自增长
)
log on --日志文件
(
	name = 'dbname_log',
	filename = 'd:\project\dbname_log.ldf',
	size =5,
	filegrowth =0  --未启用增长
)

//一般先检查有没有要创建的数据库
 if exists(select * from sysdatabases where name='dbname')
 drop database dbname

  1. 创建、删除表
use dbname --指向数据库
create table tname
(
	列名  类型  [identity] [primary key] [not null]  //自增,主键,不为空
	eg: id   int   identity(1,1) not null, //1开始,自增1
		name varchar(20) 
)

 if exists(select * from sysobjects where name='tname')
 drop table tname
  1. 约束
alter table tname
	add constraint 约束名  约束类型(列名)

主键(primary key)  PK_NAME
唯一(unique key)	  UQ_NAME
默认(default key)  DF_NAME
检查(check key)	  CK_NAME
外键(foreign key)  FK_NAME

eg: 
alter table tname
	add constraint pk_id primary key(id),
		constraint ck_pwd check (len(pwd)>=6), --len 长度
		constraint ck_sex check (sex=0 or sex=1),--性别0或1
		constraint df_sex default(0) for sex, --默认0
		constraint fk_id foreign key(uid) references tname(id), --引用tname表id进行外键链接


向已经存在数据的表中添加约束
alter table tname with nocheck    --对表中已存在的数据不做检查
	add constraint 约束名 约束类型

删除约束
alter table tname
	drop constraint 约束名
  1. 运算符
算术运算符
select 3+4 as 加的结果 --as 重置列名
select 5/2 as 除的结果 --左右为整数结果为整数,一方为小数结果为小数

赋值运算符
declare @age int --定义int类型的变量int
set @age = 18 --18赋值给age
select @age -- 打印age变量

比较运算符 >,<...
declare @x int ,@y int
set @x=2,@y=3
if @x>=@y
	select 'x大'
else
	select 'y大'

逻辑运算符 not > and > or

连接运算符  + 
左右都是数值型就是算术运算符
左右都是字符型就是连接运算符
左右不一致需要类型转换


优先级
() > 算术 > 比较 > 逻辑 > 连接 > 赋值
  1. 表中插入数据
insert into 列名 values('')
insert into 表名(列名1,列名2...) values()
有默认值的列valuesdefault表示

插入多条数据
insert into 表名(列名1,列名2...)
select '内容1',内容2....
union all
select '内容1',内容2.....

将一个表的数据插入到另一个表中
insert into1(1,列2...)
select2_列1,表2_列2
from2

将一个表的数据插入到新表中
select 源列名
into 新表名
from 源表名
  1. 更新、删除数据
update 表名 set 列名=更新值
	[where 更新条件]

delete 表名
	[where 删除条件]
  1. 查询
select 列名
from 表名
[where  条件]
[order by] --排序

between and --两个区间的值,包含起始和结束值
select * from tname where id between 2 and 10 --id号2-10之间的值

select * from tname where address in('保定','北京') --列举值范围内查询

where name like '肖%' --肖开头的多个字符
				'_' --一个字符
				[] -- 一个集合的范围[0-9]
				[^] --取反
where name = '张三'
where name != / <> '张三' --显示不是张三的
as  --添加别名

在查询中使用常量列
select id,name, '保定' as 地址 
	from tname
//会在查询出的表最后添加一项地址列,不影响数据库

查询返回限制的行数,使用top关键字
select top 5 id  from tname  --显示5行数据
select top 20 percent id from tname --显示20%的数据

select id,money from tname
order by money desc  --以money降序排列  asc升序,默认
  1. 字符串函数
charindex  --指定字符串的位置,索引从1开始
len --长度
upper --大写
ltrim --清除左边空格
rtrim --	清除右边空格
left  --左侧截取
right --右侧截取
replace -- 替换
stuff --删除指定长度的字符,并在该位置插入一个新的字符串


eg: 
select charindex('la','hhla') --返回3
select charindex('la','hhla'4) --返回0,从下标4开始,找不到
//可以结合left等截取字符串  left(email,charindex('@',email)-1)  获取邮箱@前字符

select replace('我喜欢java','java','sql') --将Java替换成sql,可为空就是删除

  1. 日期函数
getdate() --获得当前系统时间
dateadd(--select dateadd(MM,1,getdate()) 当前月份加1  
dateiff(start,end) --两个指定日期的时间差
  1. 数学函数
rand() --随机数 0-1
ceiling(n) --天花板 向大取整
round(n,m) --四舍五入,保留m位小数
abs(n) --取绝对值

convert(类型,’数值‘)  --数据类型转换

  1. 聚合函数
sum() 
avg()
max()
min()
count()

select count(id) as '总人数' from tname 
  1. 分组查询
select 聚合函数,分组的列 from[where 条件] group by 分组的列

select count(*) as '总人数' ,sex as '性别' from tname
	group by sex

分组后筛选用having,分组前用where

  1. 内连接与外连接
内连接:
两个表有主外键关系,两者交集
	select1_列1,2_列2,...
	from1,2     //from  表1  inner join 表2 on 表1.列 = 表2.列
	where1=2

外连接:
两个表有主从之分
有 left join (两者交集和左表)right joinfull join
	select1_列1,2_列2,...
	from1 left join2
	on1.id =2.id
  1. union 合并查询
	select ...	from1
	union
	select ...	from2
	
合并查询要求合并的表列数相同	
	要求列的类型相同或相兼容 可用convert转换类型
	列的名称由第一个查询决定
	行的个数由表中行的总数决定,有重复行将省略	

  1. 子查询
	select ... from1
	where1 =[>,<] (select ... from)

子查询必须放在一个()中,通常只能做where的条件,先执行子查询再执行父查询
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱音乐的哒哒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值