SQL Server 基本SQL用法
-
- SQL Server 基本SQL用法
- DDL_数据库操作
- DDL_数据表操作
- DML_增删改
- DML_查询
- DML_多表联合查询
- 视图
- 索引
DDL_数据库操作
create database db_test
on
(
name = 'db_test',
filename='e:\dbfiles\dbtest.mdf',
size=10,
maxsize=100,
filegrowth=5
)
log on
(
name='db_test_log',
filename='e:\dbfiles\dbtestlog.ldf',
size=2,
maxsize=10,
filegrowth=1
);
drop database db_test;
use db_test;
backup database db_test to disk='e:\aaa.bak' with format,name = 'tset';
restore filelistonly from disk='e:\aaa.bak';
restore database db_test from disk='e:\aaa.bak'
with move 'db_test' to 'e:\dbfiles\dbtest.mdf',
move 'db_test_log' to 'e:\dbfiles\dbtestlog.ldf'
drop database db_test;
create database db_test
on(
name = 'db_test',
filename = 'e:\db_files\db_test.mdf',
size=10,
maxsize=100,
filegrowth=2
)
log on (
name = 'db_test_log',
filename='e:\db_files\testlog.ldf',
size=2,
maxsize=10,
filegrowth=1
);
use db_test;
create table students (
stu_num char(5) primary key,
stu_name varchar(20) not null,
stu_sex char(2) not null check (stu_sex='男' or stu_sex='女') default '男',
);
DDL_数据表操作
use db_test
drop table tb_grade;
drop table tb_students;
drop table tb_courses;
create table tb_students(
stu_num int primary key identity(1000001,1),
stu_name varchar(20) not null,
stu_sex char(2) not null check(stu_sex='男' or stu_sex='女') default('男'),
stu_age int not null check (stu_age>0 and stu_age<200),
stu_tel char(11) unique
);
create table tb_courses(
course_num int primary key identity (1001,1),
course_name varchar(50) not null,
course_xf int not null
);
create table tb_grade(
snum int,
cnum int,
score int not null,
primary key(snum,cnum),--联合主键的设置
constraint fk_grade_students foreign key (snum) references tb_students(stu_num),
constraint fk_grade_course foreign key (cnum) references tb_courses(course_num)
);
alter table tb_students add stu_addr varchar(100);
alter table tb_students alter column stu_addr varchar(50) not null;
alter table tb_grade alter column snum int not null;
alter table tb_grade alter column cnum int not null;
alter table tb_grade add constraint pk_grade primary key (snum,cnum);
alter table tb_grade drop constraint fk_grade_course;
alter table tb_grade add constraint fk_grade_course foreign key (cnum) references tb_courses(course_num);
drop database db_test;
create database db_test
on(
name = 'db_test',
filename = 'e:\db_files\db_test.mdf',
size=10,
maxsize=100,
filegrowth=2
)
log on (
name = 'db_test_log',
filename='e:\db_files\testlog.ldf',
size=2,
maxsize=10,
filegrowth=1
);
use db_test;
create table tb_stus (
stu_num char(5) primary key,
stu_name varchar(20) not null,
stu_sex char(2) not null check (stu_sex='男' or stu_sex='女') default '男',
stu_age int not null,
stu_tel char(11) unique
);
create table tb_courses(
course_num int primary key identity(100,1),
course_name varchar(50) not null,
course_xf int not null
);
create table tb_grade(
snum char(5) not null,
cnum int not null,
score int not null,
primary key(snum,cnum),
constraint fk_grade_stus foreign key (snum) references tb_stus(stu_num),
constraint fk_grade_courses foreign key (cnum) references tb_courses(course_num)
);
DML_增删改
insert into tb_courses values('大学语文',3);
insert into tb_stus values ('10010','张三','男',23,'13030303300');
insert into tb_stus (stu_num,stu_name,stu_sex,stu_age,stu_tel) values ('10011','李四','女',21,'13030303301');
delete from tb_stus where stu_num = '10016';
delete from tb_stus where stu_age < 24 or stu_age>25;
delete from tb_stus where stu_age between 24 and 25;
delete from tb_stus where stu_age not between 24 and 25;
update tb_stus set stu_age=27 where stu_num='10010';
update tb_stus set stu_age=17,stu_name='小花',stu_tel='13030303322' where stu_num='10011';
update tb_stus set stu_age=stu_age+1 where stu_sex='女';
update tb_stus set stu_age=stu_age+1;
DML_查询
select * from tb_stus;
select stu_num,stu_name from tb_stus where stu_age between 23 and 30;
select stu_num as'学号',stu_name '姓名',stu_age '年龄' from tb_stus;
select stu_num as'学号',stu_name '姓名',2016-stu_age '出生年份' from tb_stus;
select * from tb_stus where stu_sex='男';
select * from tb_stus where stu_age>20;
select * from tb_stus where stu_name like '%T%';
select * from tb_stus where stu_name like 'T%';
select * from tb_stus where stu_name like '_i%';
select * from tb_stus where stu_age between 19 and 24;
select * from tb_stus where stu_name not like 'T%';
select * from tb_stus where stu_age not between 19 and 24;
select * from tb_stus where (stu_age<20 and stu_sex='女') or stu_num='10010';
select * from tb_stus where stu_age <=20 or stu_sex='女';
select
stu_sex '性别',
sum(stu_age) '总和',
min (stu_age) '最小年龄',
max(stu_age) '最大值',
avg(stu_age) '平均值',
count(*) '人数'
from tb_stus
group by stu_sex;
select stu_sex '性别',count(*) '人数' from tb_stus group by stu_sex;
select stu_sex '性别',count(*) '人数' from tb_stus where stu_age>20 group by stu_sex;
select stu_sex '性别',count(*) '人数' from tb_stus group by stu_sex having avg(stu_age)>24;
select * from tb_stus where stu_age>20 and stu_num in
(select stu_num from tb_stus where stu_sex='女');
select * from tb_stus order by stu_age;
select * from tb_stus order by stu_age desc;
select * from tb_stus order by stu_age asc;
select * from tb_stus order by stu_sex desc,stu_age asc;
select top(2) * from tb_stus;
select top(2) stu_num from tb_stus;
select * from tb_stus
where stu_num not in (select top(2) stu_num from tb_stus);
DML_多表联合查询
use db_test
create table dept(
deptno int primary key identity(1001,1),
dname varchar(20) not null,
daddr varchar(30) not null
);
use db_test
create table emp(
eno varchar(5) primary key,
name varchar(20) not null,
age int not null,
sal decimal(8,2) not null,
deptno int,
constraint fk_emp_dept foreign key (deptno) references dept(deptno)
);
insert into dept(dname,daddr) values ('市场部','武汉');
insert into dept(dname,daddr) values ('研发部','宜昌');
insert into dept(dname,daddr) values ('人事部','北京');
insert into dept(dname,daddr) values ('财务部','上海');
insert into dept(dname,daddr) values ('售后服务部','深圳');
insert into emp(eno,name,age,sal,deptno) values ('10001','张三',23,1000.00,1002);
insert into emp(eno,name,age,sal,deptno) values ('10003','李四',29,1300.00,1002);
insert into emp(eno,name,age,sal,deptno) values ('10007','赵六',33,1700.00,1002);
insert into emp(eno,name,age,sal,deptno) values ('10002','mike',19,800.00,1001);
insert into emp(eno,name,age,sal,deptno) values ('10004','rose',21,1200.00,1001);
insert into emp(eno,name,age,sal,deptno) values ('10005','jack',22,3000.00,1004);
insert into emp(eno,name,age,sal,deptno) values ('10006','tom',26,2400.00,null);
insert into emp(eno,name,age,sal,deptno) values ('10008','hans',28,2000.00,null);
select * from dept;
select * from emp;
select dept.deptno,dname,daddr,eno,name,age,sal from dept,emp where emp.deptno = dept.deptno;
select * from dept left join emp on dept.deptno=emp.deptno;
select * from dept right join emp on dept.deptno=emp.deptno;
select * from dept full join emp on dept.deptno=emp.deptno;
select * from dept join emp on emp.deptno = dept.deptno ;
select * from dept,emp where emp.deptno = dept.deptno;
select * from dept d,emp e where e.deptno = d.deptno;
select deptno from dept where dname='研发部';
select * from emp where deptno=1002;
select * from emp where deptno in (select deptno from dept where dname='研发部');
select deptno,count(*) 'num' from emp group by deptno having deptno>0;
select * from dept left join(select deptno,count(*) 'num' from emp group by deptno having deptno>0) v on dept.deptno=v.deptno;
select dname from dept where deptno in (select distinct deptno from emp);
select * from tb_stus;
select * from tb_couresses
;
select deptno,dname from dept
union
select eno,name from emp;
视图
use db_test;
create view stu_grade as
(select deptno,count(*) 'num' from emp group by deptno having deptno>0);
create view stu_nv as(select * from tb_stus where stu_sex='女');
select * from tb_stus;
select * from stu_nv;
索引
create index stu_name_tb_stus on tb_stus(stu_name);
drop index stu_name_tb_stus on tb_stus;
create index stu_name_tb_stus on tb_stus(stu_name desc,stu_age asc);