當(dāng)前位置:
首頁(yè) >
SQLServer数据表的创建
發(fā)布時(shí)間:2025/4/5
35
豆豆
生活随笔
收集整理的這篇文章主要介紹了
SQLServer数据表的创建
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 1 SQLServer數(shù)據(jù)表的創(chuàng)建
- 1.1 創(chuàng)建數(shù)據(jù)表的語(yǔ)法
- 1.2 標(biāo)識(shí)列的特殊說(shuō)明
- 1.3 建表舉例
1 SQLServer數(shù)據(jù)表的創(chuàng)建
1.1 創(chuàng)建數(shù)據(jù)表的語(yǔ)法
create table 表名 {字段1 數(shù)據(jù)類型 列的特征,字段2 數(shù)據(jù)類型 列的特征,... } go列的特征包含的內(nèi)容如下:
- 是否為空(NULL):在輸入數(shù)據(jù)時(shí),數(shù)據(jù)庫(kù)的列允許為空時(shí),可以不輸入數(shù)據(jù),否則必須輸入。列是否為空要根據(jù)數(shù)據(jù)庫(kù)設(shè)計(jì)的具體要求決定,對(duì)于關(guān)鍵列必須禁止為空。
- 是否是標(biāo)識(shí)列(自動(dòng)編號(hào))。
- 是否有默認(rèn)值:如果數(shù)據(jù)表的某列在用戶不輸入數(shù)據(jù)的時(shí)候,希望提供一個(gè)默認(rèn)的內(nèi)容,比如用戶如果不輸入學(xué)員地址,則默認(rèn)‘地址不詳’。
- 是否為主鍵:主鍵是實(shí)體的唯一標(biāo)識(shí),保證實(shí)體不被重復(fù)。一個(gè)數(shù)據(jù)表必須有主鍵才有意義,否則更新和刪除實(shí)體可能會(huì)出異常。
1.2 標(biāo)識(shí)列的特殊說(shuō)明
標(biāo)識(shí)列使用的意義: 有時(shí)候,一個(gè)數(shù)據(jù)表存儲(chǔ)的實(shí)體,很難找到不重復(fù)的列作為主鍵列,比如學(xué)員成績(jī)表中存儲(chǔ)著學(xué)生的多次考試成績(jī),則學(xué)號(hào)也很容易重復(fù),其他列更無(wú)法做到不重復(fù)。SQLServer提供了一個(gè)“標(biāo)識(shí)列”,也叫“自動(dòng)增長(zhǎng)列”或“自動(dòng)編號(hào)”,它本身沒(méi)有什么具體意義,但我們也可以讓它表示特殊意義。比如學(xué)生成績(jī)表中的自動(dòng)表示列Id,不表示實(shí)體屬性;但學(xué)生信息表中的StudentId也是標(biāo)識(shí)列,但它表示學(xué)生實(shí)體屬性(學(xué)號(hào))。
標(biāo)識(shí)列的使用方法:
- 該列必須是整數(shù)類型,或沒(méi)有小數(shù)位數(shù)的精確類型。
- 標(biāo)識(shí)種子:標(biāo)識(shí)列的起始大小。
- 表示增量:標(biāo)識(shí)列每次遞增(自動(dòng)增加)的值。
注意問(wèn)題:
- 有標(biāo)識(shí)列的數(shù)據(jù)被刪除某一行時(shí),數(shù)據(jù)庫(kù)會(huì)將該行空缺,而不會(huì)填補(bǔ)。
- 標(biāo)識(shí)列由系統(tǒng)自動(dòng)維護(hù),用戶既不能自己輸入數(shù)據(jù),也不能修改數(shù)值。
- 標(biāo)識(shí)列可以同時(shí)定義為主鍵,也可以不定義為主鍵,根據(jù)需要決定。
1.3 建表舉例
建表舉例1:
--創(chuàng)建學(xué)員信息數(shù)據(jù)表 use StudentManageDB goif exists(select * from sysobjects where name='Students') drop table Students go create table Students (StudentId int identity(10000,1),--學(xué)號(hào)StudentName varchar(20) not null,--姓名Gender char(2) not null,--性別Birthday datetime not null,--出生日期StudentIdNo numeric(18,0) not null,--身份證號(hào)Age int not null,--年齡PhoneNumber varchar(50),StudentAddress varchar(500),ClassId int not null --班級(jí)外鍵 ) go --創(chuàng)建班級(jí)表 if exists(select * from sysobjects where name='StudentClass') drop table StudentClass go create table StudentClass (ClassId int primary key,--班級(jí)編號(hào)ClassName varchar(20) not null ) go --創(chuàng)建成績(jī)表 if exists(select * from sysobjects where name='ScoreList') drop table ScoreList go create table ScoreList (Id int identity(1,1) primary key,StudentId int not null,--學(xué)號(hào)外鍵CSharp int null,SQLServer int null,UpdateTime datetime not null--更新時(shí)間 ) go --創(chuàng)建管理員表 if exists(select * from sysobjects where name='Admins') drop table Admins go create table Admins (LoignId int identity(1000,1) primary key,LoginPwd varchar(20) not null,--登錄密碼AdminName varchar(20) not null ) go建表舉例2:
--指向要操作的數(shù)據(jù)庫(kù) use CourseManageDB go --創(chuàng)建講師表 if exists(select * from sysobjects where name='Teacher') drop table Teacher go create table Teacher (TeacherId int identity(1000,1) primary key , --講師編號(hào),主鍵LoginAccount varchar(50) not null, --登錄賬號(hào)LoginPwd varchar(18) not null, TeacherName varchar(20) not null,Phonenumber char(11) not null, --電話NowAddress nvarchar(100) not null --住址 ) go --課程分類表 if exists (select * from sysobjects where name='CourseCategory') drop table CourseCategory go create table CourseCategory (CategoryId int identity(10,1) primary key,CategoryName varchar(20) not null ) go --課程表 if exists (select * from sysobjects where name='Course') drop table Course go create table Course (CourseId int identity(1000,1) primary key,CourseName nvarchar(50) not null ,CourseContent nvarchar(500) not null,ClassHour int not null,--課時(shí)Credit int not null, --學(xué)分CategoryId int not null, --外鍵約束TeacherId int not null ) go參考資料:
總結(jié)
以上是生活随笔為你收集整理的SQLServer数据表的创建的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SQLServer中的数据类型
- 下一篇: SQLServer数据的基本操作:简单的