CREATE DATABASE edu ON(
NAME=edu_data,
FILENAME='c:/edu_data.mdf',
SIZE=10M,
MAXSIZE=1024M,
FILEGROWTH=1M)
LOG ON(
NAME=edu_log,
FILENAME='c:/edu_log.ldf',
SIZE=5M,
MAXSIZE=100M,
FILEGROWTH=10%)
create database st
use st
drop database st
create schema st
drop schema cascade
1 create table st.Student(....);
2 create schema st
create table Student(...);
3 set search_path to st;
create table Student(..);
create table Student
(Sno char(9) primary key,
Sname char(20) unique,
Ssex char(20) check(sex=''or sex='')
)
create table sc
(Sno char(9)
Cno char(4)
Grade smallint
primary key (Sno,Cno)
foreign key(Sno) references Student(Sno),
foreign key(Cno) references Course(Cno));
alter table Student add Scom datetime;
add unique(Cname);
alter column Sage smallint;
create unique(每一个索引值对应唯一数据) index student on Student(Sno);
create cluster index Stusname on Student(Sname);
drop index st.student;
select 20xx-Sage age,distinct Sno
from Student
where Sdept in('is','cs');
select *
from Student
where Sno like'212';
where rtrim(Sname) like '_liu%\' escape'\';
select Sno
from SC
where Grade ie null
order by Grade desc(降序);
select count(distict *) sum avg max min
from Student;
分组 grop by 分组后聚集函数作用于每一个组
select Cno,count(Sno)集合查询 使用了聚集函数或在group by 后 from SC
group by Cno;
select sdept,count(*)
from Student
where ssex=''
group by sdept
order by sdept desc;
having 对集合特性的限制
select Sno,count(Cno)
from SC
where Grade>=85
group by Sno
having count(*)>3
order by count(*)desc;
select Student.Sno,Sname,Cno,grade
from Student,SC
whereStudent.Sno=SC.Sno;(两表连接条件)
select Studednt.Sno,Sname,Cnno,grade
from Student inner join SC on Student.Sno=SC.Sno
where sdept='CS';
select S1.Sno,S1.Sname,S1.Sdept
from Student S1,Student S2
where S1.Sdept=S2.Sdept and S2.Sname='';
(选修了课程的学生信息)
select s1.Sno,Sname,Cno,grade
from Student S1 left outer join SC s2 on s1.Sno=s2.Sno order by s1,Sno;
课程被选修的情况
right outer join
所有情况
full outer join
select s=Student.sno,Sname,avg(grade) 平均成绩
from Student inner join SC on Student.Sno=SC.Sno
group by Student.Sno,Sname
having avg(grade)>=85
order by 平均成绩 desc;
in
=
any/all
select Sname,Sage
from Student
where Sdept<>'cs' and Sage<any(select Sage
from Student
where Sdept='cs')
where Sage<(select max(Sage)
from Student
where Sdept='cs')and Sdept<>'cs';(all与<min)
select Sname
from Student
where exists
(select *
from SC
where Sno=Student.Sno and Cno='1'
)
select Sname
from Student,SC
where Student.Sno=SC.Sno and SC.cno='1';
select Sno,Cno
from SC x
where grade>=(select avg(grade)
from SC y
where y.Sno=x.Sno);
select Sno,Cno
from SC x,SC y
where y.Sno=x.Sno
group by Sno,Cno
having grade>avg(grade);
select Sname
from Student
where not exists
(select *
from Course
where not exists
(select *
from SC
where Sno=Student.Sno and Cno=Course.Cno));
select Sno
from Student
where not exists
(select *
from Course
where Cno in(select Cno from SC where Sno='1')
and not exist(select *
from SC
where Sno=Student.Sno and Cno=Course.Cno));
并交差
seletc Sno
from SC
where Cno='1'
union
select Sno
from SC
where Cno='2';
select distinct Sno
from SC'
where Cno='1'or Cno='2';
intersect 对应 and
seletc Sno
from SC
where Cno='1'
intersect
select Sno
from SC
where Cno='2';
select Sno
from SC
where Cno='1' and Sno in
(select Sno
from SC
where Cno='2');
seletc Sno
from SC
where Cno='1'
except
select Sno
from SC
where grade<19;
insert into Student
values ('1','','',19,'cs');
insert into Student(Sdept,Avgge) select Sdept,avg(Sage) from Student
group by Sdept;
update Student
set Sage=21,Sdept='is' where Sno='12';
update Student
set Sage=Sage+1;
update SC
set Grade=0
where Sno in
(seelct Sno
from Student
where Sdept='cs');
delete
from Student
where Sno='11';
delete from SC;
delete
from SC
where 'cs'=
(select Sdept
from Student
where Student.Sno=SC.Sno);
create view IS
as
select Sno,Sname,Sage from Student
where Sdept='is';
[数据库] 简单SQL语句总结
全篇以学生成绩的管理为例描述。
1.在查询结果中显示列名:
a.用as关键字:select name as '姓名' from students order by age
b.直接表示:select name '姓名' from students order by age
2.精确查找:
a.用in限定范围:select * from students where native in ('湖南', '四川')
b.between...and:select * from students where age between 20 and 30
c.“=”:select * from students where name = '李山'
d.like:select * from students where name like '李%' (注意查询条件中有“%”,则说明是部分匹配,而且还有先后信息在里面,即查找以“李”开头的匹配项。所以若查询有“李”的所有对象,应该命令:'%李%';若是第二个字为李,则应为'_李%'或'_李'或'_李_'。)
e.[]匹配检查符:select * from courses where cno like '[AC]%' (表示或的关系,与"in(...)"类似,而且"[]"可以表示范围,如:select * from courses where cno like '[A-C]%')
3.对于时间类型变量的处理
a.smalldatetime:直接按照字符串处理的方式进行处理,例如:
select * from students where birth > = '1980-1-1' and birth <= '1980-12-31'
4.集合函数
a.count()求和,如:select count(*) from students (求学生总人数)
b.avg(列)求平均,如:select avg(mark) from grades where cno=?B2?
c.max(列)和min(列),求最大与最小
5.分组group
常用于统计时,如分组查总数:
select gender,count(sno)
from students
group by gender
(查看男女学生各有多少)
注意:从哪种角度分组就从哪列"group by"
对于多重分组,只需将分组规则罗列。比如查询各届各专业的男女同学人数 ,那么分组规则有:届别(grade)、专业(mno)和性别(gender),所以有"group by grade, mno, gender"
select grade, mno, gender, count(*)
from students
group by grade, mno, gender
通常group还和having联用,比如查询1门课以上不及格的学生,则按学号(sno)分类有:select sno,count(*) from grades
where mark<60
group by sno
having count(*)>1
6.UNION联合
合并查询结果,如:
SELECT * FROM students
WHERE name like ?张%?
UNION [ALL]
SELECT * FROM students
WHERE name like ?李%?
7.多表查询
a.内连接
select g.sno,s.name,c.coursename
from grades g JOIN students s ON g.sno=s.sno
JOIN courses c ON g.cno=c.cno
(注意可以引用别名)
b.外连接
b1.左连接
select courses.cno,max(coursename),count(sno)
from courses LEFT JOIN grades ON courses.cno=grades.cno
group by courses.cno
左连接特点:显示全部左边表中的所有项目,即使其中有些项中的数据未填写完全。 左外连接返回那些存在于左表而右表中却没有的行,再加上内连接的行。
b2.右连接
与左连接类似
b3.全连接
select sno,name,major
from students FULL JOIN majors ON students.mno=majors.mno
两边表中的内容全部显示
c.自身连接
select c1.cno,c1.coursename,c1.pno,c2.coursename
from courses c1,courses c2 where c1.pno=c2.cno
采用别名解决问题。
d.交叉连接
select lastname+firstname from lastname CROSS JOIN firstanme
相当于做笛卡儿积
8.嵌套查询
a.用关键字IN,如查询李山的同乡:
select * from students
where native in (select native from students where name=? 李山?)
b.使用关键字EXIST,比如,下面两句是等价的:
select * from students
where sno in (select sno from grades where cno=?B2?)
select * from students where exists
(select * from grades where
grades.sno=students.sno AND cno=?B2?)
9.关于排序order
a.对于排序order,有两种方法:asc升序和desc降序
b.对于排序order,可以按照查询条件中的某项排列,而且这项可用数字表示,如:
select sno,count(*) ,avg(mark) from grades
group by sno
having avg(mark)>85
order by 3
10.其他
a.对于有空格的识别名称,应该用"[]"括住。
b.对于某列中没有数据的特定查询可以用null判断,如select sno,courseno from grades where mark IS NULL
c.注意区分在嵌套查询中使用的any与all的区别,any相当于逻辑运算“||”而all则相当于逻辑运算“&&”
d.注意在做否定意义的查询是小心进入陷阱:
如,没有选修?B2?课程的学生 :
select students.*
from students, grades
where students.sno=grades.sno
AND grades.cno <> ?B2?
上面的查询方式是错误的,正确方式见下方:
select * from students
where not exists (select * from grades
where grades.sno=students.sno AND cno='B2')
11.关于有难度多重嵌套查询的解决思想:
如,选修了全部课程的学生:
select *
from students
where not exists ( select *
from courses
where NOT EXISTS
(select *
from grades
where sno=students.sno
AND cno=courses.cno))
最外一重:从学生表中选,排除那些有课没选的。用not exist。由于讨论对象是课程,所以第二重查询从course表中找,排除那些选了课的即可。
简单SQL语句总结1.在查询结果中显示列名:a.用as关键字:selectnameas'姓名'fromstudentsorderb…
Create用法:(建库,建表,建视图,建索引)1、建库:Createdatabase数据库名2、建表:Createtable表名…
SQL语句总结一.模式的定义和删除1.定义模式:createscheme模式名authorization用户名Eg:定义一个学生-…
SQL数据库语句一、对表操作1.新建数据库createdatabase数据库名2.新建表createtable表名(字段1数据类型…
CREATEDATABASEeduON(NAME=edu_data,FILENAME='c:/edu_data.mdf',SIZE…
数据定义模式的定义与删除创建:CREATESCHEMA模式名AUTHORIZATION用户名EG:CREATESCHEMA“S-T…
SQL查询语句:?查询:select?操作语句:更新update插入insert删除delete?定义语句:createtable…
更新数据UPDATEtable_nameSETcol_name1='',col_name2=''WHEREcol_name='';…
SQL语句总结之常用语一些常用的SQL语句:新建表:createtable[表名]([自动编号字段]intIDENTITY(1,1…
SQL语句总结一、插入记录1.插入固定的数值语法:INSERT[INTO]表名[(字段列表)]VALUES(值列表)示例1:Ins…