SQL server語法總結

2021-11-01 21:13:47 字數 3488 閱讀 2761

標示符的作用:

[ ]表示該選項可以省略

[,....] 表示該選項可以重複一次或者多次

< > 表示在實際的語句中用相應的內容進行替代

表示有相應的引數時, {}中的內容是必須的

1)建立資料庫

create database database_name

[on[ [,....n]]

[, [,....n]]

][log on [,.....n]]

[collate collate_name]

filespec定義

[primary]

( [name = logical_file_name',]

filename = 'os_file_name',

[,size= size]

[,maxsze = ]

[,filegrowth= growth_incrementn]

定義filegroup filegroupname [,......n]

2)刪除資料庫

drop database database_name [,....n]

3)資料庫更改名稱:

採用儲存過程來進行實現

sp_renamedb 'old_name' , 'new_name'

4)修改資料庫

alter database database_name

5) 縮小資料庫

dbcc shrinkdatabase

( database_name [,target_percent]

[, ]

) 說明:

target_percent : 目標資料庫的大小

noturncate : 收縮後空間不釋放

turncateonly : 收縮後空間釋放給作業系統(檔案系統)

6)建立表

create table

[ database_name.[owner]. | [owner]. ]table_name

( | [ ]

n] )

[ on ]

[ text_image on ]

說明:列的定義

computed_column_expression 列的表示式,用於列之間有依賴關係時候使用的

表的約束

primary key 主鍵約束

unique 唯一約束

其中的定義部分:

::=[ collate ]

[ [default constraint_expression] | [ identity [(seed,increment) [not for republication ]]]

] [rowguidcol]

[ ] [....n]

::= [constraint constraint_name]

[ clustered | nonclusterd ]

[ with fillfactor = fill_factor ]

[ on ]]

]| [ foreign key ]

refrences ref_table[ (ref_column) ]

[ on delete ]

[ on update ]

[ not for republication ]

]| check [ not for republicaton]

(logical_expression )

} ::= [constraint contraint_name ]

[ clustered | nonclusterd ]

[ with fillfactor = fill_factor ]

[ on ]

] | foreign key

[ (column [,....n] ) ]

references ref_table [ (ref_column[,...n])]

[ on delete ]

[ on update ]

[not for republication ]

| check [ not for republication ]

}例子:create table xscj.dbo.xs (

xuehao char(6) not null ,

xingming char(8) not null ,

zhuanyeming char(10) null ,

xingbie bit not null default ((0)) ,

chushengshijian smalldatetime null,

zongxuefen tinyint null,

beizhu text null

)5)建立約束

建立主建約束

constraint constraint_name primary key [clustered| nonclustered] (column_name,...) [ on filegroup]

建立唯一約束

constraint constraint_name unique [ clusterd | nonclustered] (column_name,....) [ on filegroup]

檢查約束

檢查約束指定在一行成功插入或者刪除之前,必須要滿足的條件。列級的檢查約束測試單個列的值,並且編碼為列定義的一部分。

外來鍵約束

constraint constraint_name foreign key (column_name) references table_name.(column_name)

例子:constraint salecusfk foreign key (custid) refrences dbo.customer(custid)

在外鍵約束中,on delete 決定了當你試圖刪除有外來鍵約束的行時,系統所採取的措施。

引數 no action 表明刪除操作失敗並返回出錯資訊

引數 cascade 表明帶有外來鍵約束的所有行在指向的行被刪除時,都會被隨之刪除

on update 及其引數的用法同on delete

6)增加、刪除和修改表的字段

例子:alter table xs

add yuwen decimal(5,3) check (yuwen between 0 and 100)

alter table xs

alter column yuwen decimal(7,5) not null

alter table xs

drop column yuwen

7)刪除表

drop table

8)插入表資料

語法:方法1

insert [into]

[,....n] )

| derived_table

| executed_statment } }

| default_values

方法2insert [into]

SQL Server 2019備份恢復總結

一 資料庫儲存概述 1 資料檔案型別 primary data files 每個資料庫都有乙個單獨的主要資料檔案,預設以.mdf副檔名。主要資料檔案不僅包含資料資訊,還包含與該資料庫結構相關的資訊。建立資料庫時,資料庫結構相關資訊不僅存在於master資料庫中,同時還包含在primary data ...

SQL server資料庫總結筆記

建表的兩種方式 一 兒子,爸爸,爺爺式 包含式,屬於式 至少需要兩個表。經典案例 學校 班級 學生 建表訣竅 1 乙個物件 實體 乙個表 2 小表裡面有大表 主鍵 下級的小表包含上一級大表的主鍵,比如學生表裡面包含班級表的主鍵,班級表包含學校表的主鍵,學生表裡面沒有必要包含學校表的主鍵,不是不行,而...

sqlserver巡檢方案

目錄1 前言 1 1.1 目的 1 1.2 範圍 1 1.3 內容提要 1 2 巡檢方案 1 2.1 巡檢週期 1 2.2 巡檢目標 1 2.3 服務型別 1 2.4 巡檢方式 1 2.5 巡檢項 1 2.5.1 伺服器 1 2.5.2 資料庫 2 2.5.3 應用系統 3 2.5.4 網路通訊 4...