exec sp_renamedb 'Mytest','Mytest'--對數據庫重命名
go
--修改數據庫屬性,設置為只讀
exec sp_dboption 'Mytest','read only',false--true
go
--設置數據庫為自動壓縮
exec sp_dboption 'Mytest',autoshrink ,true--false
--收縮數據庫的大小
DBCC shrinkdatabase ('Mytest',10)--將壓縮數據庫Mytest的大小,以使userdb中文件有10MB的可用空間
go
--分離數據庫
exec sp_detach_db 'Mytest'
--附加數據庫
exec sp_attach_db @dbname='Mytest',
@filename1='D:\Mytest\DB_data.mdf',
@filename2='D:\Mytest\DB_data.ldf';
--數據庫的備份
go
exec sp_addumpdevice 'disk','mydiskdump','d:\Mytest\Mytest.dat'--創建磁盤設備邏輯名
backup database Mytest to mydiskdump
go
--數據庫的恢復
restore database 'Mytest'from mydiskdump
go
exec sp_dropdevice 'mydiskdump'
--查看數據庫
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是基數據,increment是增長的速率,系統自動為該列添加數據
Student_name varchar(20)
)
--兩種插入數據的方法
insert into Student values('wubo')
insert into Student select 'zhang' union select 'lin'
--添加主鍵約束
alter table Student add constraint s_pk primary key (Student_no)
--察看數據庫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),
)
--設置服務器的identity關鍵字可以插入數據
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,以前沒有該列的數據都設置為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'
--復制表又復制數據,先創建表然后再復制數據,自增和NOT NULL可以復制,別的約束不能復制
select * into temp1 from Course
--只復制表結構
select *into temp from Course where 1>2
--刪除表中元素,不能刪除被引用的數據,用以確保引用完整性
delete from Course where Course_name='c++'
/****************************************************/
--這兩種方法不能被外鍵引用,不可帶條件刪除
--刪除表中所有元素,寫日志
delete table Course
--刪除表中所有數據,不寫日志,不安全
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 刪除時可以級聯刪除
--on update cascade 更新時可以級聯更新
--刪除數據時,先刪除主表的數據,然后刪除從表的數據,主表是:被引用的表,從表是:引用的表
--插入數據時,先插入被引用的表,然后插入引用的表、
/***************************多表查詢與聚合****************************/
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)
)
--插入數據
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 子句的交叉聯接將產生聯接所涉及的表的笛卡爾積。第一個表的行數乘以第二個表的行數等于笛卡爾積結果集的大小
select e.employeeid, d.name as department from humanresources.employee e cross joinh umanresources.department d order by e.employeeid, d.name
--內連接:僅顯示兩的連接表中的匹配行的連接
select * from goods inner join provider on goods.provider_id=provider.provider_id
--聯合查詢:
聯合查詢 union all關鍵字.
(1)將兩個或更多查詢的結果合并為單個結果集,該結果集包含聯合查詢中的所有查詢的全部行。UNION 運算不同于使用聯接合并兩個表中的列的運算。
(2) 下面列出了使用 UNION 合并兩個查詢結果集的基本規則:
所有查詢中的列數和列的順序必須相同。
數據類型必須兼容。
(3) all 參數:將全部行并入結果中。其中包括重復行。如果未指定該參數,則刪除重復行
------------------------------------------------------------------
--自引用問題
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
sqlserver2005基礎知識
2007-05-29 15:55
--創建數據庫:
use master
--兩種判斷數據庫是否存在的方法
if db_id('Mytest')is not null******(if exists(select *from sysdatabases where name='Mytest'))
drop database Mytest
go
--exec xp_cmdshell 'mkdir D:\Mytest'--調用DOS命令創建文件夾
--sql server2005種有三種類型文件
主數據文件.mdf,次要數據文件.ndf,日志文件.ldf
create database Mytest
on
(name=Mytest_dat,
filename='D:\Mytest\DB_data.mdf',
--CREATE DATABASE 失敗。
--主文件必須至少是 3 MB 才能容納模型數據庫的副本,創建主數據文件
size=3mb,
--maxsize=??可以規定最大值當沒有設置此項的時候,說明數據庫是無限增長的
filegrowth=1mb 當文件大于設置的size時,文件增長的大小為1Mb
)
log on--創建日志文件 --Log文件的設置項和主文件的設置項一樣的
(
name=Mytest_log,
filename='D:\Mytest\DB_data.ldf',
size=1mb,
--maxsize=??可以規定最大值
filegrowth=1mb
|