SQL基本語句演練

create database database2 數據庫名稱
On
 (
   name='database2_dat',名字
   filename='C:\Program Files\Microsoft SQL Server\MSSQL\Data\database2_dat.mdf',路驚
   size=10,maxsize=unlimited,filegrowth=10%文件大小最大隻增長方式
 )
 log on 日誌文件
(
  name='database2_log',
  filename='C:\Program Files\Microsoft SQL Server\MSSQL\Data\database2_log.ldf',
  size=1,maxsize=5,filegrowth=1
)
數據庫文件和日誌文件語句。。
 

filegroup fg1
 (  name='database52_data',
  filename='c:\program files\microsoft sql server\mssql\data\dddd_data.ndf'
  size=10,maxsize=20,filegrowth=5
 )
文件組文件語句。。必須要在數據庫名稱之後才能生效。。文件組包括在數據庫裏面。。
      2  重命名。。把WOKAO 文件改爲LIU文件 語句
             方法一sp_renamedb wokao , liu 
             方法二  alter database liu
                   modify name=wokao   


             數據管理

 

      --創建數據庫
create database liushuai
 on
 (name='liushuai',
  filename='c:\program files\microsoft sql server\mssql\data\liushuai_dat.mdf',
  size=10,
  maxsize=20,
  filegrowth=10%
  )
 use liushuai
--創建學生表
create table liushuai
 (
   sno int primary key, --學號。爲整數,主鍵
   sname varchar(10) not null,--姓名爲可變不爲空
   stel char(12), --電話爲固定12位
   sage smallint check(sage>=18 and sage<=25),--約束於在18-25之間
 ssex bit default(1)  --性別
   )
 --查看對象
 select * from liushuai 
--使用增量
 create table cs
 (sno int identity(1,1)
 )
--修改基本表
alter table }{--創建數據庫
create database liushuai
 on
 (name='liushuai',
  filename='c:\program files\microsoft sql server\mssql\data\liushuai_dat.mdf',
  size=10,
  maxsize=20,
  filegrowth=10%
  )
 use liushuai
--創建學生表
create table liushuai
 (
   sno int primary key, --學號。爲整數,主鍵
   sname varchar(10) not null,--姓名爲可變不爲空
   stel char(12), --電話爲固定12位
   sage smallint check(sage>=18 and sage<=25),--約束於在18-25之間
 ssex bit default(1)  --性別
   )
 --查看對象
 select * from liushuai
--使用增量
 create table cs
 (sno int identity(1,1)
 )
--修改基本表
alter table <表名>
add [字段名 數據類型]--增加新量
alter table [表名]
drop column[列名]--刪除該列
alter table [表明]
alter column[列名  類型]--修改該列類型
--課程表
create table course
 (
  cno int primary key,--課程號
  cname varchar(10)not null,--課程名不爲空
  cpno int identity(2,1),--上一課程號,必須完成上一個號纔可以完成該號
  ccredit decimal(4,1),--學分在111.1規定內
  cutime smallint --課時數爲整數
  )
--學生選課表
create table sc 
(
  sno int identity(1,1),  --學號
  cno int unique,         --課程號唯一不允許取重複直
  grade decimal(4,1)      --成績
)
add sbig int--增加新量
alter table liushuai
drop column sbig--刪除該列
alter table liushuai
alter column sname varchar(11)--修改該列類型
--課程表
create table course
 (
  cno int primary key,--課程號 爲主鍵(contraint pk primary key(cno,cpno)設這倆個爲主鍵。只是倆個屬性來表示。一個表中只能有個一主鍵,可以用倆個屬性來表示。。呵呵)
  cname varchar(10)not null,--課程名不爲空
  cpno int identity(2,1),--上一課程號,必須完成上一個號纔可以完成該號
  ccredit decimal(4,1),--學分在111.1規定內
  cutime smallint --課時數爲整數
  )
--學生選課表
create table sc 
(
  sno int identity(1,1),  --學號
  cno int unique,         --課程號唯一不允許取重複直
  grade decimal(4,1)      --成績
)
      單行的修改。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
create database student
use student
create table student
(
   sno int primary key,
   sname varchar(10)not null,
   ssex bit default(1),
   stel char(12),
   sage smallint check:(sage>=18 and sage<=25)
  )
--插入數據
  insert into student(sno,sname)
  values (1,'李一')
  insert into student(sno,sname,ssex,stel,sage)
  values (2,'曉亮',1,123456123123,19)
--增加新列
 alter table wokao
 add stel char
--把student的stel插入到wokao
select * from student

 insert into wokao(stel)
 select stel
 from student

select sage不需要事先見表
into aaaa
from student

 


insert into wokao
 select sname,stel
 from student
--把都爲空的年齡該爲20
update student set sage=20
where sage is null
--當年齡爲22時改爲23
update student set sage=23
where sage=22
--刪除年齡爲23時的一行。。。。
delete from student
where sage=23
--所有年齡+1
update student
set sage=sage+1 
            約束規則。。。。。。。。。。。。。。。。。。。。。。。

create database student
use student
create table liushuai
(
  sno int,
  sname varchar(10),
  schengji decimal(4,2),
  ssex bit default(1),
  stel char(12),
  sage smallint check:(sage>=18 and sage<=25),
  constraint pk primary key (sno)  --設置sno 爲主鍵
)
--查看主鍵信息
sp_pkeys liushuai
--瀏覽表中的所有約束建
sp_helpconstraint liushuai
--刪除主鍵
alter table liushuai
drop constraint pk
sp_pkeys liushuai
--添加主鍵
alter table liushuai
add constraint pk primary key (sno)
--查看主鍵
sp_pkeys liushuai
--添加唯一建
alter table liushuai
add constraint up unique (sname) --姓名添加唯一建
alter table liushuai with nocheck

--禁用約束
alter table liushuai
nocheck constraint CK__liushuai__sage__7C8480AE
--禁用約束然後添加不符合要求的
insert into liushuai
values (2,'王2',82,1,123456789123,15)

--啓用約束
alter table liushuai
check constraint CK__liushuai__sage__7C8480AE

 

drop table liu

create table liu
(
 sno int,
 sname varchar(10),
 stel char(12),
constraint fk foreign key(sno) references liushuai(sno)
on delete cascade
 )


alter table liu
drop  constraint CK
select * from shuai
--連續添加多個數據
insert into liushuai(Sno,sname)
select 9,'saf' union
select 2,'sg' union
select 3,'ags'

insert into liu(Sno)
select 1 union
select 2 union
select 9

alter table liu with nocheck
add constraint CK check(sno>0 and sno<5)

insert into liu(Sno)
select 12 union
select 0
                              使用規則。。。。。。。。。。。。。。。。。。。。。。
--使用規則
-創建一個表格
create table shuai
(
 sno int,
 sname varchar,
 gongzi int
)
--使用一個規則
create rule gongzi_rule
as @gongzi>=1000 and @gongzi<=1500
--綁定規則
sp_bindrule gongzi_rule,'shuai.gongzi'
 --添加數據
insert into shuai(gongzi)
values (500)
--解除規則
sp_unbindrule 'shuai.gongzi'
--列如
create table wang
(
 sno int primary key,
 sname varchar(10),
 sage smallint check:(sage>=18 and sage<=25)
)
create rule xingming_rule
as @xingming in('劉')
 sp_bindrule xingming_rule,'wang.sname'
insert into wang(sno,sname)
values(2,'劉')
create rule xing
--建立以個以姓劉的名字g規則
--先建立一個表格
create table yang
(
 sname varchar(12)
)
--第二建立一個規則
create rule ming
as @ming like '劉%'
--綁定規則
sp_bindrule ming,'yang.sname'
--添加數據
insert into yang(sname)
values ('劉帥')
select * from yang
--不符合要求
insert into yang(sname)
values ('楊華')

 

 

 

 

 

--刪除student表中的主鍵約束
sp_pkeys student
alter table sc
drop constraint FK_SC_Student
alter table student
drop constraint PK_Student
select * from student
--向student表添加主鍵約束
alter table student
add constraint PK_student primary key(Sno)
--
create table stu
(
 Sno int,
 Sname varchar(10)
)
--向表中添加主鍵約束
alter table stu
alter column sno int not null

alter table stu
add constraint PK_stu primary key(sno)
--向student中sage列添加檢查約束,不檢查之前的數據
select * from student
alter table student with nocheck
add constraint CK_student check(sage>18 and sage<25)
insert into student(sage,Sno)
values (12,2005007)
alter table student
nocheck constraint CK_student

--禁用course表中的所有約束
alter table course
nocheck constraint all
--創建規則
create rule rule_sc
as
@score>=60 and @score<=100
--綁定規則
select * from sc
sp_bindrule rule_sc,'sc.grade'
--檢驗是否已綁定
insert into sc
values(2005005,4,59)
--解除綁定
sp_unbindrule 'sc.grade'
--創建郵箱數據類型'%@%'
sp_addtype 'Email','varchar(50)'
--創建規則
create rule rule_Email
as
@email like '%@%'
--將規則綁定到Email數據類型上
sp_bindrule 'rule_Email','Email'
--檢驗
create table tongxunlu
(
 Sname varchar(10),
 Semail Email
)
--插入記錄,可插入
insert into tongxunlu
values('aa','[email protected]')
select * from tongxunlu
--插入一條記錄
insert into tongxunlu
values('aa','wanghy_0716')
--不能插入,因爲不符合Email數據類型的要求
--解除綁定
sp_unbindrule 'Email'
drop rule rule_Email

create table test8
(
  xingming varchar(20),
  xingbie varchar(2),
  nianling smallint
)
--創建默認值
Create   default     default_xb

As      '男'
--將默認值綁定到test8.xingbie
SP_bindefault default_xb,'test8.xingbie'
--單表查詢
--顯示錶中所有行和列
select * from student
--顯示部分列(查詢所有學生的姓名和年齡)
select Sname,Sage
from student
--顯示部分列和部分行(查詢性別爲男的學生姓名)
select Sname
from student
where Ssex=1

select * from sc
--查詢學生的成績(成績+5)
select Grade+5
from sc
--查詢所有學生的年齡(加1)
select Sname as 姓名,Sage+1 as 年齡
from student
--查詢2005001的成績
select sno 學號,grade 成績
from sc
where sno=2005001

--查詢前三個學生的信息
select top 3 * from student
--查詢前50%的學生信息
select top 50 percent *
from student
--查詢前50%的男生學生信息(姓名)
select top 50 percent Sname
from student
where Ssex=1
--查詢計算機專業的學生信息
select * from student
where Sdept='計算機專業'
--顯示面積大於300的倉庫記錄
use cangku
go
select * from 倉庫
where 面積>300

--顯示工資在600到1230之間的職工信息
select * from 職工
where 工資>600 and 工資<1230
select * from 職工
使用計算列。。。。。。。。。。。。。。。
銷售價格降低0.5 price-price*0.5
select grade from sc
select grade-grade*0.5 from sc  成績降低0.5
                         嵌套查詢。。。。。。。。。。。。。。。。。。。。
select 列名 新列名,列名
from 表名,表名2
where 條件表達式
group by 列名
having 條件表達式(可以包含聚合函數)
order by 列名1 asc,列名2 desc
--查詢每個學生的平均成績
select sno,avg(grade) as 平均成績
from sc
group by sno
--查詢平均成績在前三名的學生信息
select top 3 sno,avg(grade) 平均成績
from sc
group by sno
order by avg(grade) desc

select * from student
--查詢與劉晨同一專業的學生姓名
select sname from student
where sdept=
(
 select sdept from student
 where sname='劉晨'
) and sname<>'劉晨'
--查詢計算機專業、生物專業和數學專業的學生姓名
select sname,sdept
from student
where sdept in('計算機專業','生物專業','數學專業')

--查詢沒參加考試的學生名單
select sname
from student
where sno in
(--找出SC表中沒參加考試的學生學號
 select sno from sc
 where grade is null
)
--查詢選修了一號課程的學生姓名和專業
select Sname,Sdept
from student
where sno in
(--找選修1號課程的學號
 select sno from sc
 where cno=1
)
--查找哪些城市至少有一個倉庫的職工的工資爲1250
select 城市
from 倉庫
where 倉庫號 in
(
 select 倉庫號 from 職工
 where 工資=1250
)

use student
go
--參加考試的學號名單和成績
select student.Sname,sc.Grade
from student,sc
where student.sno=sc.sno and grade is not null
--查詢既不是計算機專業,也不是生物專業、數學專業的學生姓名
select sname from student
where sdept!='計算機專業' and sdept!='生物專業' and sdept!='數學專業'
--方法二
select sname from student
where sdept not in('計算機專業','生物專業','數學專業')
--判斷cangku數據庫是否存在,如果存在提示
--”數據庫已存在"
use master
go
if exists(
 select * from sysdatabases
 where name='cangku'
)
print '數據庫已存在'
--檢查本次考試,本班如果沒有人成績達到
--90分以上,則每人減5分;
use student
go
if  exists(
 select sno from sc
 where grade>90
)
update sc
set grade=grade-5

select * from sc
--檢查本次考試,本班如果沒有人成績達到
--95分以上,則每人加5分;
if not exists(
 select sno from sc
 where grade>95
)
update sc
set grade=grade+5
select * from sc
--檢索有職工的工資大於或等於WH2倉庫中
--其中一名職工工資的職工信息

use cangku
go
select 職工號,工資
from 職工
where 工資>any(
  select 工資 from 職工
  where 倉庫號='wh2'
       )
--檢索有職工的工資大於或等於WH2倉庫中
--所有職工工資的職工信息
select * from 職工
where 工資>=all(
  select 工資 from 職工
  where 倉庫號='wh2'
       )
--查詢年齡大於20和專業爲計算機專業的
--學生姓名、年齡
select sname,sage from student
where sage>20
union
select sname,sage from student
where sdept='計算機專業'

--1、找出和職工e4掙同樣工資的所有職工信息
use cangku
select * from 職工
where 工資 in (
 select 工資 from 職工
 where 職工號='e4'
) and 職工號!='e4'

2、找出管理的倉庫面積在300以下的職工信息
select *from 職工
where  倉庫號 in(
      select 倉庫號 from 倉庫
      where 面積<300
)
--3、找出沒有參加考試的學生姓名
 use student
 go
 select student.sname,sc.grade
 from student, sc
 where grade is null and student.sno=sc.sno
4、找出平均分最高的學生姓名
select Sname from student
where sno in
(
 select top 1 sno from sc
group by sno
order by avg(grade) desc
)


5、查詢所有成績都及格的學生信息
(可以畢業的學生信息)
 
  select distinct sno
  from sc
  where sno not in(
  select sno from sc
  where grade is null
       )
  group by sno
  having min(grade)>60

--9、求出選修了3門以上課程且所有成績高於70分
--的學生的平均年齡
--找出選修了3門以上課程的學號
select avg(sage) from student
where sno in
(
 select sno
 from sc
 where sno not in(select sno from sc where grade is  null)
 group by sno
 having count(cno)>3 and min(grade)>70
)
select * from student


10、求出選修了‘數據結構’課程的男學生信息
(假設性別1的爲男學生)
select * from student
where sno in(select sno from sc
             where cno in(
                         select cno from course
                         where cname='數據結構')) and ssex=1

11、查詢選修了課程並且有考試成績的學生的總人數
 
        接連查詢。。。。。。。。。。。。。。。。。。。。
--7\查詢沒有選修1號課程的學生姓名
if exists(select cno from sc where cno=1)
select sname from student
where sno not in
(
 select sno from sc
 where cno=1
)
                    
select * from sc
8、求出選修了3門以上課程且所有成績
高於75分的學生的最小年齡
select top 1 sage from student
where sno in
(
 select sno from sc
 where sno not in (select sno from sc
   where grade is  null
   )
  group by sno
 having count(cno)>3 and min(grade)>75
)
order by sage
select * from sc
9、求出選修了‘電子商務專業’課程(數據庫和ASP)
的男學生信息
select * from student
where ssex=1 and sno in
(
 select sno from sc
 where cno in(
   select cno from course
   where cname='數據庫'or cname='ASP語言'
   )
)
10 、將計算機專業全體學生的成績每人增加3分
select grade+3 加分後的成績 from sc
where sno in
(
 select sno from student
 where sdept='計算機專業'
)
11、刪除計算機專業所有學生的選課記錄
delete from sc
where sno in
(
 select sno from student
 where sdept='計算機專業'
)
select * from sc
12、查詢選修了全部課程的學生姓名
select sname from student
where sno in
(
 select sno from sc
 group by sno
 having count(cno)=(
   select count(cno) from course
   )
)
select * from sc
12、查詢選修了全部課程的學生姓名
分析:一、每個學生選修的課程數目
 select sno,count(cno) from sc
 group by sno
   二\課程表中總的課程數目
      select count(cno) from course
     三、判斷一和二的結果是否相等,如果相等
 說明該學生選了所有科目
    
     四\根據學生學號找姓名
    select sname from student
 where sno in
(
 select sno from sc
 group by sno
 having count(cno)=(select count(cno) from course)
)

drop database student
13、查詢至少選修了學生2005002選修全部課程
的學生學號(即所選修課程包含或者
等於2005002學生的課程)
分析:找出2005002選修的課程,然後找出哪個學生包含
 了2005002選的所有課程

select sno from sc
where cno  in
(
select cno from sc
where sno=2005002
) and sno!=2005002
group by sno
having count(cno)>=
(
 select count(cno) from sc
 where sno=2005002
)
 (a+b)*(c+d+e)=ac+ad+ae+bc+bd+be
select * from course,sc
--所有學生的選課情況
select sname,cname,grade
from student,course,sc
where student.sno=sc.sno and course.cno=sc.cno
--

select * from student
insert into course(cname)
values('linux')

insert into student(sno,sname)
values(2005007,'萬雨晨')
查詢學生的選課情況(內聯接) 所關聯的數據如果出現null則該數據不顯示
select Sname,cno,grade
from student
inner join sc
on student.sno=sc.sno
--查詢所有學生的選課情況(左聯接)第一張表的內容全顯示。第二滿足的顯示不滿足的不顯示
select Sname,Cno,grade
from student S
left join sc
on S.sno=SC.sno
--查詢所有學生的選課情況(右聯接)第一滿足的顯示第二的全顯示
select Sname,Cno,grade
from sc
right join student S
on S.sno=SC.sno

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章