資料庫語法

2021-09-30 10:45:07 字數 1636 閱讀 3885

建立資料庫

drop database myschool;  --刪除資料庫

create database myschool --建立資料庫on(

name='myschool_data', --資料庫名稱

filename='d:\myschool_data.mdf', --物理檔名

size=5mb,  --初始大小

maxsize=30mb,  --最大大小

filegrowth=15%  --主檔案增長率

)log on

(name='myschool_log',  --日誌檔名

filename='d:\myschool_log.ldf',  -- 日誌物理檔名

maxsize=4mb,  -- 最大大小

size=2mb,

filegrowth=1mb

)go  -- 繼續執行下面的語句

use myschool --使用myschool資料庫

go--drop table class; --刪除表

create table class  --建立表

(--[列名] [型別] [約束條件]

cld int identity(1,1) primary key,--cld為主鍵,遞增列,每次增一

cname nvarchar(50) not null,   -- unique,--cname卻唯一值

cdesciption text

--設定多列共同決定主鍵的語句:primary key (sno,cno);   --sno和cno 為列名

--設定外來鍵的語句:foreign key (sno) references student(sno);  --前乙個sno為本表的列,後乙個sno為student表的列

--check(cage>1);--約束列cage輸入的值大於1; check(cage between 1 and 100) --則約束cage的值在1-100

--default '男' for s***;--為s***列設定預設值)

修改基本表:

--新增一列

alter table 《表名》 add 《新列名》 《資料型別》 [完整性約束]  

例如:alter table student add s_entrance date

--在student表中新增了乙個新的列 s_entrance 資料型別為date

--修改表中列的屬性

alter table 《表名》 alter column 《列名》 《資料型別》

例如:alter table student alter column sage int

--將student表中sage列的資料型別改為int

--為列增加約束條件

alter table 《表名》 [完整性約束條件]

例如: alter table student add unique(sno)

--為student表中的sno列新增唯一約束條件

刪除基本表

drop table 《表名》 [restrict | cascade]

restrict: 若存在依賴該錶的物件→其他的表或列(外來鍵等) ,則該錶不能刪除

cascade : 在刪除該錶的同時,相關依賴物件將會一併刪除

資料庫語法

建立乙個資料表 create table 表名 列名1型別1 約束,列名2型別 2 約束,列名 n型別n 約束 建立表時直接建立各種約束 create table 表名 列名1型別1 primary key 列名1,列名2,列名 n 主鍵約束列名2 型別2 unique,唯一約束列名3 型別3 id...

資料庫 資料庫簡單操作語法

1.create 建立 建立資料庫 create database 資料庫名稱 建立資料庫指定字符集 create database 資料庫名稱 character set 字符集名 2.retrieve 查詢 查詢所有資料庫的名稱 show databases 3.update 修改 修改資料庫字...

SQLServer 資料庫語法

1.資料庫轉換格式 字串轉換為日期格式 convert datetime,2010 02 10 2.單純獲取資料庫時間 select getdate 3.getdate 可以與 2010 09 25 這樣的日期字串比較 4.資料查詢 乙個伺服器兩個庫select from utildata.dbo....