数据库建表

新建表:
create table 表名
(
studentid char(10) not null primartkey,
sex char(2) check(sex in('男','女')) default '男',
birthday datetime,
cr tinyint not null check(cr>0),
)
create table 表名
(
studentid char(10) not null,
sex char(2),
birthday datetime,
cr tinyint not null,
constraint pk_表名_约束条件名 Primarykey(studentid),
constraint ck_表名_约束条件名 check(sex in('男','女')),
constraint ck_表名_约束条件名 check(cr>0)
)
foregin key(classid) reference class(classid)

向表里增加列:
alter table 表名 add 增加名 varchar(20) null
删除表里的列:
alter table 表名 drop column 删除的列名
修改表名称:
exec sp_rename '旧名','新名'
修改列定义:
alter table 表名
alter column 列名 char(50)
删除表:
drop table 表名
表中添加所有记录:
insert into 表名 values(‘  ’,‘  ’,‘   ’)全部项目
表中添加部分记录:
insert 表名(列名,列名,列名)values(‘  ’,‘  ’,‘  ’)部分项
查询全部:
select * from 表名
查询部分项:
select 列名,列名 from 表名
嵌套子查询:
create table girl
(
girlid char(20),
girlname char(20),
nation char(20)
)
insert into girl
select studentid,studentname,nation from student where sex='女'
修改表内容信息:
update student set classid='15' where studentid='041'
删除表信息:
delete studentid  where studentid='021'
删除表全部信息:
truncate table 表名
约束:
主键约束:primarykey
向建好的表里加主键:

alter table 表名add constraint pk_classid primarykey (classid)
唯一性约束:unique
向建好的表里加唯一约束:

alter table 表名add constraint un_classname unique(classname)
默认:default
向建好的表加默认:

alter table表名add constraint def_default'10' for列名
检查:check
向建好的表里加检查:

alter table 表名 add constraint ck_hh check(hh betwen 0 and 100)
外键:foreign key
向建好的表里加外键:

alter table student add foreign key (classid) reference class(classid)
条件查询:
姓王:
‘王%’
两字王:
‘王_%’
三字王:
'王__%'
多个姓:
‘[张王赵李]%’
多个姓以外:
‘[^张王赵李]%’
查询年限:
where year(birthday) between 1994 and 1997
查询两个条件:
where  address in('合肥','广东')
降序:
order by address desc
升序:
order by adress asc
前几名:
select top 3 * from student order by birthday asc
前%几:
top 10 percent 
消除重复行:
select distinct address,sex from student
改列名:
select 原列名 as '新列名' from 表
聚合函数:
select count(*) as '学生人数',avg(datediff(year,birthday,getdata())) as '平均年龄' from student
分组和汇总:
group by classid having count(*)<10(筛选分组汇总后)
计算和汇总:
select count(*)as ‘学生人数’
avg (year(getdata())-year(birthday))as '平均年龄'
from student (order by classid)
compute count(studentid),
avg(datediff(year,birthday,getdata()))
by classid
多表连接:
select classname from class inner join student on (student.classid=class.classid)主键=外键
内连接:
from A inner join B on 连接条件
左外连接:
from A left join B on 连接条件
右外连接:
from A right join B on 连接条件
全外连接:
from A full join B on 连接条件
交叉连接:
from A cross join B on 连接条件
简化连接(多表)
select classid,classname,studentname from class as c inner join student as s
on c.classid=s.classid
inner join course as o
on o.courseid=s.courseid
联合查询:
select studentid,studentname from student union select teachername,teacherid from teacher
子查询:not in(木子同班学生的信息)
select studentid,studentname from student where classid in(select classid from student where studentname=’木子‘)
比较查询:(any某个值,all所有值)
select studentid,studentname,birthday from student where birthday < all
(select birthday from student where adress='合肥')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值