SQL 语句整理

创建据库:create database 数据库名称
修改库名:exec sp_renamedb 原库名,新库名
删除库名:drop database 库名 丨删除多个数据库删除库名:drop database 库名,库名2
——————————————————————————
//详细创建
create database 数据库名称
on
(
name=数据库名称_date,//主数据库文件
filename=‘d:\temp\数据库名称_date.mdf’, //主数据库文件
size=6, //数据库文件初始化大小
maxsize=12, //最大可以多大
filegrowth=10% //超过后一次增长10%
)
log on
(
name=数据库名称_log,
filename=‘d:\数据库名称_log.idf,’
size=1, //数据库文件初始化大小
maxsize=8, //最大可以多大
filegrowth=10%//超过后一次增长10%
)
——————————————————————————
//创建表详情
use 数据库名 //打开指定数据库
create table 表名//创建的表名
(
A int,
B varchar (50), //长度50文本
C int,
D money
)
use 数据库名 //打开指定数据库
create table 表名//创建的表名
(
A int,identity(1,1) primary key, //定义唯一ID递增方式,并且定义主键
B varchar (50),长度50文本
C int,
D money
)
——————————————————————————
//浏览查询
select * from dbo.表名 //从表中查询 所有字段 并显示
select 字段1,字段2 from dbo.表名 //从表中查询 所有这2个字段的。
select top(100) 字段1,字段2 from 表名//显示前100条数据。
——————————————————————————
//条件查询
use 数据库
select from 表名 where 字段 <4000 //查询小于4000的数据
select
form 表名 where 字段a>100 or 字段B<=1000 //2条件满足一条即可
selectform 表名 where not 字段A>100 //查询不大于100的值
select
form 表名 where 字段A!>100 //查询不大于100的值
select *form 表名 where (字段A=1,字段B=2) or 字段3=4
select *from 表名 where 字段 in((字段1,字段2,字段3)//与or 效果一样 满足任意条件就显示
select *from 表名 where 字段 not in((字段1,字段2,字段3)//排除这3个条件显示
select *from 表名 where 字段 is null //查询空值
select *from 表名 where 字段 is not null//查询不是空值
select *from 表名 where 字段 between 2000 and 2600 //查询之间 数值
select *from 表名 where 字段 not between 2000 and 2600 //查询之间 数值
select 字段B,字段A from 表 //自定义列显示顺序显示查询数据
select distinct 字段A from 表 //显示不同的数据
select *from 表名 where 字段A in(select 字段A from 表 where 字段B=值)
select *from 表名 where 字段A =(select 字段A from 表 where 字段B=值)
//查询所有字段等于字段B的字据嵌套查询
—————————————————————————
select *from 表A where 字段A in (select distinct字段A from 表B)
select *form 职员表 where 姓名 in(select 负责人 from 项目表)
在所有职员表中,查找有项目的人员名称
select *form 职员表 where 姓名 in(select distinct 负责人 from 项目表) //去掉重复人员名称
select *form 职员表 where 姓名 not in(select distinct 负责人 from 项目表) //查询不在项目中的人员名称

//跨表查询 查询B表中字段A 与 表A中字段A相等的数据 并且去重
—————————————————————————
order by 字段名 asc 从小到大 不加默认从小到大
order by 字段名 desc 从大到小
select form 表 order by 字段A asc, 字段B asc //字段A先排,如果有相同的再以字段B从 小到大排序
—————————————————————————
select count(
) as 统计行数 from 表名 // 统计行数*号代表所有
select count(字段A) as 统计行数 from 表名 // 统计字段A列的行数 count只统计非NULL值

—————————————————————————
select sum(工资) as 员工工资总额 from 表名//统计 sum统计某一列的总额
select avg(工资) as 平均工资 from 表名//求某一列的平均值
select max(工资) as 最大工资 from 表名//求某一列的最大值
select min(工资) as 最小工资 from 表名//求某一列的最小值
select * from 表名 where 工资>(select avg(工资) from 表名)//查询工资大于平均工资的值
select 编号,姓名,工资+奖金 as 总收入 from 表名
select *from 表名 where 字段A like ‘%王%’ //查询字段中包含王的数据 模糊查询
select *from 表名 order by 毕业学校 compute max(工资),min(工资),sum(工资),avg(工资) by 毕业学校
//通过年龄 进行排序 compute 分组汇总 用来分组的字段必须出现在前面 表里
select 字段A,字段B,字段C from 表 group by 字段C//跟据字段C进行分组
select 字段A,字段B,字段C from 表 group by 字段C having sum(字段A)>5000
分组后 结果大于5000的才显示,
—————————————————————————

update 表名 set 字段名A =值, 字段名B=值2,where 字段名C=值3//修改数据
条件值等于值3的时候修改当前索引记录
——————————————————————————
//删除
delete from 表名 where 字段=值
delete from 表名 where 字段<4000
delete from 表
——————————————————————————
// 修改增加数据库文件,和日志
alter datebase 数据库
add file

name=数据库名称,
filename=‘d:\temp\数据库名称.mdf’
size=6
)——————————————————————————
alter datebase 数据库
add log file

name=xxx,
filename=‘d:\temp\xxx.idf’,
filegrowth=10%

——————————————————————————
alter datebase 数据库 //删除mdf和 日志
remove file xxx
——————————————————————————
exec sp_rename “原表名”,“现表名” //修改表名
——————————————————————————
use 数据库名 //打开数据库
alter table “表名”//修改表
add email varchar(50)//表中增加一个字段
add QQ varchar(50) default ‘309505449’ //添加字段并设置默认值
——————————————————————————
use 数据库名//修改数据库表的字段类型
alter 表名
aiter colum 列名 int //改为整数型
——————————————————————————
use 数据库名
alter 表名
drop column 字段名 //删除指定字段名
——————————————————————————
use 数据库名//删除数据库中的表
drop table 表名
——————————————————————————
use 数据库名 //插入数据
insert into 表(字段A,字段B,字段C)values(值1,值2,值3)
——————————————————————————
在这里插入图片描述

select *from 网站经营项目 inter join 网站职员表 on 网站经营.负责人=网站职员表.名称//内联接

select *from 网站经营项目 left join 网站职员表 on 网站经营.负责人=网站职员表.名称//右链接

select *from 网站经营项目 right join 网站职员表 on 网站经营.负责人=网站职员表.名称//右链接//右链接

select *from 网站经营项目 full join 网站职员表 on 网站经营.负责人=网站职员表.名称//右链接//右链接//全显示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值