データベースの実装(ライブラリ、テーブル、制約の作成;テーブル、制約の削除)

18325 ワード

SQLServerデータベースの基礎知識の回顧
1)マスターデータファイル:*.mdf
2)マイナーデータファイル:*.ndf
3)ログファイル:*.ldf
各データベースには、少なくとも2つのファイルが含まれます.1つのデータファイルと1つのログファイルです.
SQL Serverのヘルプを表示する方法===========================ショートカットF 1
一、データベースの作成
1.構文
1 create database     
2    on  primary
3  (
4     <      >[,......n] [<     >]
5   )
6  log on
7   (
8   {<      > [,......n]}
9   )

データファイルのパラメータは次のとおりです.
1 (
2   [name=     ,]
3    filename=     
4   [,size=  ]
5    [,maxsize={    |unlimited}]
6    [,filegrowth=   ]
7   )

ファイルグループのパラメータ
1、文法
1 filegrowthファイルグループ名<ファイルパラメータ>[,......n]
2.例(上位8行で判断した場合、データベースが存在する場合はデータベースが存在しない場合は'2131231')
 1 --            
 2 if exists(select * from sysdatabases where name='MySchool')
 3 begin
 4 drop database MySchool
 5 end
 6 begin
 7 print '2131231'
 8 end
 9 --     
10 create database MySchool
11 on primary
12 (
13     --         
14     name='MySchool_data',        --          +++++++   
15     filename='E:\MySchool_data.mdf', --          +++++++   
16     size=5mb,                        --          
17     maxsize=100mb,                    --           
18     filegrowth=15%                    --         
19 )
20 log on
21 (
22 --         ,       
23 name='MySchool_log',
24 filename='E:\MySchool_log.ldf',
25 size=2mb,
26 filegrowth=1mb
27 )
28 go

二、表の作成
1、文法
1 create table   
2 3    1          
4   ......
5

 
2、例(5と6行でStudentという表があるか否かを判断)
 1 --   
 2 use MySchool --         MySchool,   MySchool    
 3 go
 4 --  
 5 if exists (select * from sysobjects where name='Student') 
 6 drop table Student
 7 
 8 create table Student   ---  Student 
 9 (
10  StudentNo int identity primary key not null,  --        ,  
11  loginpwd nvarchar(20) not null,
12  StudentName nvarchar(20) not null,
13  Sex bit default' ' not null,      --  ,  0,1
14  GradeId int not null,
15  Phone nvarchar(50) null,
16  Address nvarchar(100) null,
17  BornDate datetime not null,
18  Email nvarchar(20) null,
19  IdentityCard varchar(18) not null
20  )
21 go

三、削除表
1、文法
1 drop tableテーブル名
2、例
 1 drop table Student 
3、重点
テーブルの作成時にプライマリ・キーと自己増分カラムが作成された場合、既存のプライマリ・キーを削除するにはどうすればいいですか?
 
1 --    ,         
2 select * from sysobjects where xtype='PK'
3 --    
4 alter table [Student] drop      (  )   

 
四、制約の追加(例)
構文:
1 alter tableテーブル名
2 add constraintコンストレイント名コンストレイントタイプ具体的なコンストレイント説明
例:
1、デフォルトの制約を追加する(デフォルトの「アドレス不詳」)
1 alter table Student
2 add constraint df_address default('    ') for address

2、検査制約の追加(1996年10月26日生まれ)
1 alter table Student 
2 add constraint ck_BornDate check (BornDate >='1996-10-26')

3、一意の制約を追加する(身分証明書は世界に一つしかない)
 1 alter table Student
2 add constraint uq_IdentityCard unique (IdentityCard) 
 
4、主キー制約の追加
 1 alter table Student
2 add constraint pk_StudentNo primary key(StudentNo) 
5.外部キー制約の追加(マスターテーブルStudentとスレーブテーブルREsultの関係、関連列StudentNo)
1 alter table Result 
2 add constraint fk_StudentNo
3     foreign key(StudentNo) references Student (StudentNo) 

五、T-SQLの文の文法を振り返る
1、データの追加
1 Insert intoテーブル名(カラム名、...)Value(値1,…) 
2、データの修正
1 updateテーブル名set列1=値1、列2=値2、......where(条件)
3、データの削除
1 delete fromテーブル名where(条件)
4、クエリ文
1 select列1,列2,.....fromテーブル名where(条件)order byカラム名
理解の小例
1 select StudentName,StudentNo from Student where BornDate>='1996-10-26' order by StudentNo 

六、フォルダの作成
1 exec sp_configure 'show advanced options',1
2 go
3 reconfigure
4 go
5 exec sp_configure 'xp_cmdshell',1
6 go
7 reconfigure
8 go
9 exec xp_cmdshell 'mkdir E:\    '