日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

SQLServer数据表的创建

發布時間:2025/4/5 数据库 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQLServer数据表的创建 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1 SQLServer數據表的創建
      • 1.1 創建數據表的語法
      • 1.2 標識列的特殊說明
      • 1.3 建表舉例

1 SQLServer數據表的創建

1.1 創建數據表的語法

create table 表名 {字段1 數據類型 列的特征,字段2 數據類型 列的特征,... } go

列的特征包含的內容如下:

  • 是否為空(NULL):在輸入數據時,數據庫的列允許為空時,可以不輸入數據,否則必須輸入。列是否為空要根據數據庫設計的具體要求決定,對于關鍵列必須禁止為空。
  • 是否是標識列(自動編號)。
  • 是否有默認值:如果數據表的某列在用戶不輸入數據的時候,希望提供一個默認的內容,比如用戶如果不輸入學員地址,則默認‘地址不詳’。
  • 是否為主鍵:主鍵是實體的唯一標識,保證實體不被重復。一個數據表必須有主鍵才有意義,否則更新和刪除實體可能會出異常。

1.2 標識列的特殊說明

標識列使用的意義: 有時候,一個數據表存儲的實體,很難找到不重復的列作為主鍵列,比如學員成績表中存儲著學生的多次考試成績,則學號也很容易重復,其他列更無法做到不重復。SQLServer提供了一個“標識列”,也叫“自動增長列”或“自動編號”,它本身沒有什么具體意義,但我們也可以讓它表示特殊意義。比如學生成績表中的自動表示列Id,不表示實體屬性;但學生信息表中的StudentId也是標識列,但它表示學生實體屬性(學號)。

標識列的使用方法:

  • 該列必須是整數類型,或沒有小數位數的精確類型。
  • 標識種子:標識列的起始大小。
  • 表示增量:標識列每次遞增(自動增加)的值。

注意問題:

  • 有標識列的數據被刪除某一行時,數據庫會將該行空缺,而不會填補。
  • 標識列由系統自動維護,用戶既不能自己輸入數據,也不能修改數值。
  • 標識列可以同時定義為主鍵,也可以不定義為主鍵,根據需要決定。

1.3 建表舉例

建表舉例1:

--創建學員信息數據表 use StudentManageDB goif exists(select * from sysobjects where name='Students') drop table Students go create table Students (StudentId int identity(10000,1),--學號StudentName varchar(20) not null,--姓名Gender char(2) not null,--性別Birthday datetime not null,--出生日期StudentIdNo numeric(18,0) not null,--身份證號Age int not null,--年齡PhoneNumber varchar(50),StudentAddress varchar(500),ClassId int not null --班級外鍵 ) go --創建班級表 if exists(select * from sysobjects where name='StudentClass') drop table StudentClass go create table StudentClass (ClassId int primary key,--班級編號ClassName varchar(20) not null ) go --創建成績表 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,--學號外鍵CSharp int null,SQLServer int null,UpdateTime datetime not null--更新時間 ) go --創建管理員表 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:

--指向要操作的數據庫 use CourseManageDB go --創建講師表 if exists(select * from sysobjects where name='Teacher') drop table Teacher go create table Teacher (TeacherId int identity(1000,1) primary key , --講師編號,主鍵LoginAccount varchar(50) not null, --登錄賬號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,--課時Credit int not null, --學分CategoryId int not null, --外鍵約束TeacherId int not null ) go

參考資料:

  • .NET/C#工控上位機VIP系統學習班【喜科堂互聯教育】
  • 總結

    以上是生活随笔為你收集整理的SQLServer数据表的创建的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。