exec sp_renamedb 'Mytest','Mytest'--對數(shù)據(jù)庫重命名
go
--修改數(shù)據(jù)庫屬性,設(shè)置為只讀
exec sp_dboption 'Mytest','read only',false--true
go
--設(shè)置數(shù)據(jù)庫為自動壓縮
exec sp_dboption 'Mytest',autoshrink ,true--false
--收縮數(shù)據(jù)庫的大小
DBCC shrinkdatabase ('Mytest',10)--將壓縮數(shù)據(jù)庫Mytest的大小,以使userdb中文件有10MB的可用空間
go
--分離數(shù)據(jù)庫
exec sp_detach_db 'Mytest'
--附加數(shù)據(jù)庫
exec sp_attach_db @dbname='Mytest',
@filename1='D:\Mytest\DB_data.mdf',
@filename2='D:\Mytest\DB_data.ldf';
--數(shù)據(jù)庫的備份
go
exec sp_addumpdevice 'disk','mydiskdump','d:\Mytest\Mytest.dat'--創(chuàng)建磁盤設(shè)備邏輯名
backup database Mytest to mydiskdump
go
--數(shù)據(jù)庫的恢復(fù)
restore database 'Mytest'from mydiskdump
go
exec sp_dropdevice 'mydiskdump'
--查看數(shù)據(jù)庫
sp_helpdb,sp_helpfilegroup,sp_database;
use Mytest
go
if exists (select *from sysobjects where name='Student')
--if object_id('Student','u')is not null)
drop table Student
create table Student
(
Student_no int identity(1000,1),--自動增長 indentity(seed,increment) seed是基數(shù)據(jù),increment是增長的速率,系統(tǒng)自動為該列添加數(shù)據(jù)
Student_name varchar(20)
)
--兩種插入數(shù)據(jù)的方法
insert into Student values('wubo')
insert into Student select 'zhang' union select 'lin'
--添加主鍵約束
alter table Student add constraint s_pk primary key (Student_no)
--察看數(shù)據(jù)庫mytest的信息
sp_helpdb mytest--或者sp_databases Mytest
select *from Student
--------------------------------------------------------------------
if exists (select *from sysobjects where name='Course')
drop table Course
create table Course
(
Course_no int identity(1000,1),
Course_name varchar(20),
)
--設(shè)置服務(wù)器的identity關(guān)鍵字可以插入數(shù)據(jù)
set identity_insert course on
set identity_insert course off
insert into Course values( 'c','zhang')
insert into Course select 'java' union select 'c++'
select *From Course
--添加約束公式
alter table table_name add constraint constraint_name constraint_type(column_name)
**********
alter table Course add constraint C_pk primary key (Course_no)
--察看約束公式
exec sp_helpconstraint table_name
**********
exec sp_helpconstraint Course
--刪除主鍵約束
alter table Course drop C_pk
--添加外鍵約束
alter table table_name add constraint constraint_name foreign key (column_name) references referenced_table(referenced_table's column_name)
--添加列并指定默認值為NULL,以前沒有該列的數(shù)據(jù)都設(shè)置為NULL
alter table Course add Course_teacher varchar(20) default null
--刪除列
alter table Course drop column Course_teacher
--重命名列
exec sp_rename 'Course.Course_teacher','Course_teacher', 'column'
--重命名表
exec sp_rename 'Course','NewCourse'
--復(fù)制表又復(fù)制數(shù)據(jù),先創(chuàng)建表然后再復(fù)制數(shù)據(jù),自增和NOT NULL可以復(fù)制,別的約束不能復(fù)制
select * into temp1 from Course
--只復(fù)制表結(jié)構(gòu)
select *into temp from Course where 1>2
--刪除表中元素,不能刪除被引用的數(shù)據(jù),用以確保引用完整性
delete from Course where Course_name='c++'
/****************************************************/
--這兩種方法不能被外鍵引用,不可帶條件刪除
--刪除表中所有元素,寫日志
delete table Course
--刪除表中所有數(shù)據(jù),不寫日志,不安全
truncate table Course
/***************************************************/
--模式匹配,%匹配任何字符串,_匹配任何一個字符,模式是大小寫敏感的
select *from Course where Course_name like 'c%'--只要第一個為c的字母就可以匹配
select *from Course where Course_name like 'c_+'--這個字符串有三個字符,第二個字符可以使任意的
select *from Course where Course_no between 1000 and 1002
/***************************************************/
--外鍵操作
create table dept
(
d_id int primary key,
d_name varchar(20)
)
create table emp
(
e_id int primary key,
e_name varchar(20),
e_no int foreign key references dept(d_id) on update cascade on delete cascade
)
[ ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
[ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
如果 timestamp 列是外鍵或被引用鍵的一部分,則不能指定 CASCADE。
--外健的建立是在主表建立外健的列是唯一屬性的情況下才能建立
--on delete no action 無級連更新,刪除時外鍵沖突報錯并回滾delete
--on update no action 無級連更新,更新時外鍵沖突報錯并回滾update
--on delete cascade 刪除時可以級聯(lián)刪除
--on update cascade 更新時可以級聯(lián)更新
--刪除數(shù)據(jù)時,先刪除主表的數(shù)據(jù),然后刪除從表的數(shù)據(jù),主表是:被引用的表,從表是:引用的表
--插入數(shù)據(jù)時,先插入被引用的表,然后插入引用的表、
/***************************多表查詢與聚合****************************/
create table company
(
c_id int primary key,
c_name varchar(20),
c_tel varchar(20)
)
create table dept
(
d_id int primary key,
d_name varchar(20),
d_tel varchar(20),
d_no int foreign key references company(c_id)
)
create table emp
(
e_id int primary key,
e_name varchar(20),
e_tel varchar(20),
e_no int foreign key references dept(d_id)
)
--插入數(shù)據(jù)
insert into company select 1000,'sun','110'
union select 1001,'ibm','120'
union select 1002,'mir','130'
union select 1003,'top','140'
union select 1004,'mos','150'
insert into dept select 1,'hr','1100',1000
union select 2,'money','1200',1000
union select 3,'kaifa','1300',1000
union select 4,'zuzhi','1400',1000
insert into emp select 100,'wubo','1',1
union select 101,'zhang','2',2
union select 102,'lin','3',3
union select 103,'linbo','4',4
select *from company
select *from dept
select *from emp
select * from emp left outer join dept on emp.e_no=dept.d_id left outer join company on dept.d_no=company.c_id
--左連接運算時,左邊的在運算后全部存在,右邊的不匹配的用NULL表示
select emp.e_id,emp.e_name from emp left outer join dept on emp.e_no=dept.d_id
--右連接運算時,右表的在運算后全部存在,左邊的不匹配的用NULL表示
select * from dept right outer join emp on emp.e_no=dept.d_id
--全連接
select *from dept full join emp on emp.e_no=dept.d_id
--交叉連接(笛卡爾積)
沒有 WHERE 子句的交叉聯(lián)接將產(chǎn)生聯(lián)接所涉及的表的笛卡爾積。第一個表的行數(shù)乘以第二個表的行數(shù)等于笛卡爾積結(jié)果集的大小
select e.employeeid, d.name as department from humanresources.employee e cross joinh umanresources.department d order by e.employeeid, d.name
--內(nèi)連接:僅顯示兩的連接表中的匹配行的連接
select * from goods inner join provider on goods.provider_id=provider.provider_id
--聯(lián)合查詢:
聯(lián)合查詢 union all關(guān)鍵字.
(1)將兩個或更多查詢的結(jié)果合并為單個結(jié)果集,該結(jié)果集包含聯(lián)合查詢中的所有查詢的全部行。UNION 運算不同于使用聯(lián)接合并兩個表中的列的運算。
(2) 下面列出了使用 UNION 合并兩個查詢結(jié)果集的基本規(guī)則:
所有查詢中的列數(shù)和列的順序必須相同。
數(shù)據(jù)類型必須兼容。
(3) all 參數(shù):將全部行并入結(jié)果中。其中包括重復(fù)行。如果未指定該參數(shù),則刪除重復(fù)行
------------------------------------------------------------------
--自引用問題
create table employ
(
e_id int primary key,
e_name varchar(23),
e_tel varchar(23),
e_high int foreign key references employ(e_id)
)
insert into employ select 1,'wubo','13484623684',null
union select 2,'zhang','13772436004',1
union select 3,'lin','12345678945',1
union select 4,'bolin','231456789',2
select *From employ
select *from employ e inner join employ m on e.e_id=m.e_high
--創(chuàng)建數(shù)據(jù)庫: |