一.建立資料庫:
1.語法: create database 表名
on (
《資料檔案引數》[,·······] [《檔案組引數》]
log on
《日誌檔案引數》[,········]
例子:
create database studbon primary
--預設就屬於primary檔案組,可省略(/*
--資料檔案的具體描述--
*/name='
studb_data
',
--主資料檔案的邏輯名稱
filename='
d:\studb_data.mdf',
--主資料檔案的物理名稱
size
=5mb,
--主資料檔案的初始大小
maxsize
=100mb,
--主資料檔案增長的最大值
filegrowth=15
%--主資料檔案的增長率
)log on(/*
--日誌檔案的具體描述,各引數含義同上--
*/name='
studb_log',
filename='
d:\studb_log.ldf',
size
=2mb,
filegrowth
=1mb
)
2.刪除資料庫:
語法:drop database 資料庫名
例子:drop database bankdb
二.建立表:
1.語法:
create table 表名
列1 資料型別 列的特徵,
列2 資料型別 列的特徵,
.......
例子:create table student
studentno int not null,
name nvarchar(20) not null,
age int not null
2.刪除表:
語法:drop table 表名
例子:drop table student
三.新增約束:
1. 常用的五大約束的語法示例
1.—-新增主鍵約束(將stuno作為主鍵)
alter table stuinfo
add constraint pk_stuno primary key (stuno)
2.—-新增唯一約束(身份證號唯一,因為每個人的都不一樣)
alter table stuinfo
add constraint uq_stuid unique(stuid)
3.—-新增預設約束(如果位址不填 預設為「位址不詳」)
alter table stuinfo
add constraint df_stuaddress default (『位址不詳』) for stuaddress
4.—-新增檢查約束 (對年齡加以限定 15-40歲之間)
alter table stuinfo
add constraint ck_stuage check (stuage between 15 and 40)
alter table stuinfo
add constraint ck_stu*** check (stu***=』男』 or stu***=』女′)
5.—-新增外來鍵約束 (主表stuinfo和從表stumarks建立關係,關聯欄位stuno)
alter table stuinfo
add constraint fk_stuno foreign key(stuno)references stuinfo(stuno)
2.刪除約束:
alter table 表名 drop constraint 約束名
例子:alter table student drop constraint fk_stuno
資料庫的實現
新建資料庫 use master goif exists select from sysdatabases where name s2222 drop database s2222 create database s2222 on primary name s2222 data filename d...
資料庫的實現
一 建立資料庫 建立資料庫的語法如下 create database 資料庫名稱 on primary 資料檔案引數 n 檔案組引數 n log on 檔案組引數的語法如下 filegroup 檔案組的邏輯名稱 default 檔案引數 執行儲存過程使用 exec 命令 exec 1 呼叫儲存過程 ...
資料庫的實現
一.使用sql語句管理資料庫 1.建立資料庫需要指定資料庫名稱 資料檔案 日誌檔案等。create database 資料庫名稱 on primary 資料檔案引數 n 檔案組引數 n log on 1.建立資料庫例項 create database netbardbon name netbar m...