posts - 0, comments - 77, trackbacks - 0, articles - 356
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          sqlserver2005基本操作

          Posted on 2008-02-24 16:38 semovy 閱讀(1048) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): MS SQLServer方面

          exec sp_renamedb 'Mytest','Mytest'--對(duì)數(shù)據(jù)庫(kù)重命名
          go
          --修改數(shù)據(jù)庫(kù)屬性,設(shè)置為只讀
          exec sp_dboption 'Mytest','read only',false--true
          go
          --設(shè)置數(shù)據(jù)庫(kù)為自動(dòng)壓縮
          exec sp_dboption 'Mytest',autoshrink ,true--false
          --收縮數(shù)據(jù)庫(kù)的大小
          DBCC shrinkdatabase ('Mytest',10)--將壓縮數(shù)據(jù)庫(kù)Mytest的大小,以使userdb中文件有10MB的可用空間
          go
          --分離數(shù)據(jù)庫(kù)
          exec sp_detach_db 'Mytest'
          --附加數(shù)據(jù)庫(kù)
          exec sp_attach_db @dbname='Mytest',
                             @filename1='D:\Mytest\DB_data.mdf',
                             @filename2='D:\Mytest\DB_data.ldf';
          --數(shù)據(jù)庫(kù)的備份
          go
          exec sp_addumpdevice 'disk','mydiskdump','d:\Mytest\Mytest.dat'--創(chuàng)建磁盤(pán)設(shè)備邏輯名
          backup database Mytest to mydiskdump
          go
          --數(shù)據(jù)庫(kù)的恢復(fù)
          restore database 'Mytest'from mydiskdump
          go
          exec sp_dropdevice 'mydiskdump'
          --查看數(shù)據(jù)庫(kù)
          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),--自動(dòng)增長(zhǎng) indentity(seed,increment) seed是基數(shù)據(jù),increment是增長(zhǎng)的速率,系統(tǒng)自動(dò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ù)庫(kù)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)
          --添加列并指定默認(rèn)值為NULL,以前沒(méi)有該列的數(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)建表然后再?gòu)?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++'
          /****************************************************/
          --這兩種方法不能被外鍵引用,不可帶條件刪除
          --刪除表中所有元素,寫(xiě)日志
          delete table Course
          --刪除表中所有數(shù)據(jù),不寫(xiě)日志,不安全
          truncate table Course
          /***************************************************/
          --模式匹配,%匹配任何字符串,_匹配任何一個(gè)字符,模式是大小寫(xiě)敏感的
          select *from Course where Course_name like 'c%'--只要第一個(gè)為c的字母就可以匹配
          select *from Course where Course_name like 'c_+'--這個(gè)字符串有三個(gè)字符,第二個(gè)字符可以使任意的
          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 無(wú)級(jí)連更新,刪除時(shí)外鍵沖突報(bào)錯(cuò)并回滾delete
          --on update no action 無(wú)級(jí)連更新,更新時(shí)外鍵沖突報(bào)錯(cuò)并回滾update
          --on delete cascade 刪除時(shí)可以級(jí)聯(lián)刪除
          --on update cascade 更新時(shí)可以級(jí)聯(lián)更新
          --刪除數(shù)據(jù)時(shí),先刪除主表的數(shù)據(jù),然后刪除從表的數(shù)據(jù),主表是:被引用的表,從表是:引用的表
          --插入數(shù)據(jù)時(shí),先插入被引用的表,然后插入引用的表、
          /***************************多表查詢(xún)與聚合****************************/
          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
          --左連接運(yùn)算時(shí),左邊的在運(yùn)算后全部存在,右邊的不匹配的用NULL表示
          select emp.e_id,emp.e_name from emp left outer join dept on emp.e_no=dept.d_id
          --右連接運(yùn)算時(shí),右表的在運(yùn)算后全部存在,左邊的不匹配的用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
          --交叉連接(笛卡爾積)
          沒(méi)有 WHERE 子句的交叉聯(lián)接將產(chǎn)生聯(lián)接所涉及的表的笛卡爾積。第一個(gè)表的行數(shù)乘以第二個(gè)表的行數(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)合查詢(xún):
          聯(lián)合查詢(xún) union all關(guān)鍵字.
          (1)將兩個(gè)或更多查詢(xún)的結(jié)果合并為單個(gè)結(jié)果集,該結(jié)果集包含聯(lián)合查詢(xún)中的所有查詢(xún)的全部行。UNION 運(yùn)算不同于使用聯(lián)接合并兩個(gè)表中的列的運(yùn)算。
          (2) 下面列出了使用 UNION 合并兩個(gè)查詢(xún)結(jié)果集的基本規(guī)則:
                  所有查詢(xún)中的列數(shù)和列的順序必須相同。
                  數(shù)據(jù)類(lèi)型必須兼容。
          (3) all 參數(shù):將全部行并入結(jié)果中。其中包括重復(fù)行。如果未指定該參數(shù),則刪除重復(fù)行
          ------------------------------------------------------------------
          --自引用問(wèn)題
          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基礎(chǔ)知識(shí)
          2007-05-29 15:55

          --創(chuàng)建數(shù)據(jù)庫(kù):
          use master
          --兩種判斷數(shù)據(jù)庫(kù)是否存在的方法
          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'--調(diào)用DOS命令創(chuàng)建文件夾
          --sql server2005種有三種類(lèi)型文件
          主數(shù)據(jù)文件.mdf,次要數(shù)據(jù)文件.ndf,日志文件.ldf
          create database Mytest
          on
          (name=Mytest_dat,
          filename='D:\Mytest\DB_data.mdf',
          --CREATE DATABASE 失敗。
          --主文件必須至少是 3 MB 才能容納模型數(shù)據(jù)庫(kù)的副本,創(chuàng)建主數(shù)據(jù)文件
          size=3mb,
          --maxsize=??可以規(guī)定最大值當(dāng)沒(méi)有設(shè)置此項(xiàng)的時(shí)候,說(shuō)明數(shù)據(jù)庫(kù)是無(wú)限增長(zhǎng)的
          filegrowth=1mb 當(dāng)文件大于設(shè)置的size時(shí),文件增長(zhǎng)的大小為1Mb
          )
          log on--創(chuàng)建日志文件           --Log文件的設(shè)置項(xiàng)和主文件的設(shè)置項(xiàng)一樣的
          (
          name=Mytest_log,
          filename='D:\Mytest\DB_data.ldf',
          size=1mb,
          --maxsize=??可以規(guī)定最大值
          filegrowth=1mb

          主站蜘蛛池模板: 丰城市| 东阳市| 东莞市| 贞丰县| 景宁| 武平县| 东台市| 穆棱市| 龙陵县| 吴江市| 仪陇县| 三亚市| 环江| 虎林市| 连平县| 晴隆县| 新津县| 高阳县| 垦利县| 竹溪县| 利川市| 龙川县| 梨树县| 南平市| 滁州市| 广昌县| 盖州市| 乌兰察布市| 柘城县| 北京市| 读书| 定安县| 长寿区| 孟连| 曲松县| 抚州市| 剑川县| 仪征市| 漳浦县| 安宁市| 新平|