SQL Server 基本SQL用法

SQL Server 基本SQL用法

DDL_数据库操作

--这是注释
--有关数据库操作的DDL指令
--①创建一个新的数据库
--一个SQLsever数据库包含一个数据文件,和一个日志文件
--数据文件(mdf)用来存储数据库中的数据
--日志文件(ldf)存储数据库的操作日志
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: 刪除 数据对象
--delete 删除数据
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)
);
--1sql中的数据类型:
--整型:int 
--浮点型:float,decinal
--字符型:char(定长字符串,少于长度用空格补全,效率高),varchar(最多放多少字节)


--2删除数据表
--drop table tb_students

--3数据表字段约束
--字段不能为空 not null
--字段值唯一  unique
--字段类型 char/varchar/int/float
--字段值长度 字段类型后面()中定义
--主键约束 primary key 默认唯一
--外键约束 constraint fk_grade_students foreign key (snum) references tb_students(stu_num);
--check约束  check(布尔)
--默认值 default(' ')
--自动增长 identity(1001,1) 字段必须是整型

--修改数据表
--alter/drop 对数据库中的对象进行修改/删除
--update/delete 对数据表的中数据进行修改/删除

--1添加表字段
alter table tb_students add  stu_addr varchar(100);
--2修改字段约束
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;
--3修改表中的主外键关系
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 <tableName> valuses (v1,v2,v3...)
insert into tb_courses values('大学语文',3);

insert into tb_stus values ('10010','张三','男',23,'13030303300');
--要求:当进行添加操作时,必须在表名后面通过()列出赋值的字段名,即使是所有字段,也请列出字段名
--好处1:所给的值无需和表定义的列顺序保持一致,只需和表名后列出的字段名保持一致
--好处2:可以提高语句的兼容性/健壮性(例如新添一个字段后则无法使用上面的方式)
--insert into <tableName> (c1,c2,c3...) valuses (v1,v2,v3...)
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;
--给字段取别名(加不加as都可以)
select stu_num as'学号',stu_name '姓名',stu_age '年龄' from tb_stus;
--计算列
select stu_num as'学号',stu_name '姓名',2016-stu_age '出生年份' from tb_stus;
--条件查询:在select语句后添加where子句表示查询满足条件的数据
select * from tb_stus where stu_sex='男';
select * from tb_stus where stu_age>20;

--like查询
--查询出名字中包含字母'T'的学生信息(%代表0~n个字符)
select * from tb_stus where stu_name like '%T%';
--查询出名字中首字母'T'的学生信息
select * from tb_stus where stu_name like 'T%';
--查询出名字中包含字母'i'的学生信息( _ 代表任意一个字符)
select * from tb_stus where stu_name like '_i%';

--between..and.. 关键字
select * from tb_stus where stu_age between 19 and 24;

--not 条件取反
select * from tb_stus where stu_name not like 'T%';
select * from tb_stus where stu_age not between 19 and 24;

--多条件查询 ( or , and)
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='女';

--group by分组查询 select 不能字节显示字段名,只能显示被分组的字段名。
--或者聚合函数(count,avg,max,min,sum)
--count(*) 统计记录数
--avg(column) 统计指定字段的平均值
--max(column) 获取当前字段的最大值
--min(column) 获取当前字段的最小值
--sum(column) 统计指定字段的值总和
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;
--分组后筛选 having
--显示平均年龄大于24的分组(按性别分组)
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='女');



--排序 order by :对查询结果按照某个字段查询
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;
--查询前2条
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); 

--分页查询 

--select  top(m) * from tb_stus
--where stu_num not in (select  top((n-1)*m) 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)
);
--添加5个部门信息
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 ('售后服务部','深圳');
--添加8个员工信息
--研发部1002
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);
--市场部1001
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);
--财务部1004
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='研发部';

--查询出员工表中部门编号为1002的员工信息
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;

--查询有员工的部门名称(distinct用于select之后用于去除查询结果的重复项)
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;

索引

--索引相当于一本书的目录,可以很大程度上提高我们的查询效率
--创建索引
--1若将某数据表的某字段设置为主键则会自动为其建立索引
--2若将某数据表的某字段设置为唯一键(unique)则也会自动为其建立索引
--3如果我们频繁的根据某一字段进行查询操作,则需手动在此字段建立索引
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);
  • 13
    点赞
  • 88
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值