T SQL入門攻略之12 建立資料表

2021-05-23 09:19:05 字數 4001 閱讀 4550

使用主鍵約束

主鍵不允許重複也不允許有空值

--1單字段主鍵 if

object_id

('testtable'

,'u')is

notnull

drop

table testtable;

create

table testtable  (

col1        varchar

( 10),

col2        int,

col3        datetime,

col4        numeric

( 10, 2),

col5        xml,

col6        image,

primary

key( col1) );

--2多欄位主鍵

ifobject_id

('testtable'

,'u')is

notnull

drop

table testtable;

create

table testtable  (

col1        varchar

( 10),

col2        int,

col3        datetime,

col4        numeric

( 10, 2),

col5        xml,

col6        image,

primary

key( col1, col2, col3) );

使用唯一性約束

唯一unique

用於強制非主鍵列的唯一性,我們可以將唯一約束定義在乙個欄位上也可以定義在多個欄位上。

--1單字段唯一約束

ifobject_id

('testtable'

,'u')is

notnull

drop

table testtable;

create

table testtable (

col1    char

( 10),

col2    int,

col3    float,

unique

(col1) );

--2多欄位唯一約束

ifobject_id

('testtable'

,'u')is

notnull

drop

table testtable;

create

table testtable (

col1    char

( 10),

col2    int,

col3    float,

unique

(col1, col2) );

主鍵與唯一約束的相似點與不同點:

相似點:

在主鍵列或逐漸列的組合上不允許出現重複值

,在被定義唯一性約束的列或列的組合上也不允許出現重複值

,他們所在列都建立了乙個唯一性索引。

不同點:

在表裡只能定義定義乙個主鍵,但可以定義多個唯一約束,主鍵所在列不允許空值但唯一性約束列允許空值

使用非空約束

使用not null

約束的字段其值不允許為空

(null) if

object_id

('testtable'

,'u')is

notnull

drop

table testtable;

create

table testtable (

col1 char

( 10)not

null

unique

,--

非空約束

和唯一性約束

col2 int

notnull,

-- 非空約束

col3 float );

使用預設約束

對有的字段可能不希望直接對其輸入值或者暫時不輸入

,同時希望它自己能夠形成乙個初始值或者有的字段值是取自其他地方這時候可以使用預設約束。 if

object_id

('usertable'

,'u')is

notnull

drop

table usertable;

create

table usertable (

username        varchar

( 20),

loginuser       varchar

( 10)

default

user,

logintime       datetime

default

getdate

(),

uservocation    varchar

( 50)

default

' 計算機及其相關'

); 使用檢查約束

為了避免輸入資料時候出現人為錯誤,可以通過定義檢查約束的方法來解決(

check)

ifobject_id

('testtable'

,'u')is

notnull

drop

table testtable;

create

table testtable (

userid  varchar

( 10)

check

( userid like

'[a-z]%[1-4]'

anddatalength

( userid)= 5),

-- age

值不能為0

--userid

值的長度必須為

5 --userid

值中最後乙個

字元必須1、

2、3或

4 --userid

值的首字元必須為字母

age int

check

( age > 0 and age <= 150) );

建立臨時表和表變數

1:臨時表

臨時表表名以#或

##為字首的一類資料表是臨時儲存資料庫

tempdb

中的一類資料庫物件。其中以

#字首的臨時表為本地臨時表

,在當前會話內有效

,會話外無效。字首

##的臨時表是全域性臨時表所有會話都可以訪問。臨時表的生命週期是建立臨時表的會話的生命週期只要建立他的會話還存在

,該臨時表就會持續存在

.資料表都會自動儲存到

tempdb

中,資料庫重啟後這些表將會被刪除.

2:表變數

表變數是用於儲存表資料的一種變數

,與一般變數一樣也使用

declare

來宣告

不管是臨時表還是表變數都要把他們當資料表來操作

3:表變數與臨時表的不同點

(1)臨時表儲存在磁碟上(邏輯上儲存在資料庫

tempdb

上)表變數則儲存在記憶體中,

(2)訪問臨時表會生成日誌資訊

,而訪問表變數則不會 (

3)可以對臨時表建立索引

但不能對錶變數建立索引 (

4)臨時表需要鎖機制

,而表變數則不需要

T SQL入門攻略之12 建立資料表

title t sql 入門攻略之12 建立資料表 author wufeng4552 date 2010 06 18 使用主鍵約束 主鍵不允許重複也不允許有空值 1單字段主鍵 if object id testtable u is notnull drop table testtable crea...

T SQL入門攻略之12 建立資料表

title t sql 入門攻略之12 建立資料表 author wufeng4552 date 2010 06 18 使用主鍵約束 主鍵不允許重複也不允許有空值 1單字段主鍵 if object id testtable u is notnull drop table testtable crea...

T SQL 入門攻略

更改資料庫名 1 alter database database name modify name new database name 2 sp renamedb olddbname,newdbname 新增資料檔案和檔案組 擴大 1 新增資料檔案 use master godeclare data...