sql server中创建数据库和表的语法
生活随笔
收集整理的這篇文章主要介紹了
sql server中创建数据库和表的语法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
下面是sql server中創(chuàng)建數(shù)據(jù)庫(kù),創(chuàng)建數(shù)據(jù)表以及添加約束的sql語(yǔ)句:
use master --創(chuàng)建數(shù)據(jù)庫(kù) if exists (select * from sysdatabases where name = 'jobtest')drop database jobtest create database jobtest on (name='jobtest_data',filename = 'D:\DB\jobtest_data.mdf',size = 10MB,filegrowth = 10% ) log on (name = 'jobtest_log',filename = 'D:\DB\jobtest_log.ldf',size = 10MB,maxsize = 20MB,filegrowth = 1MB ) go--創(chuàng)建student表 if exists (select * from sysobjects where name = 'student') drop table student create table student (id int not null,name varchar(50) not null,age int not null )go --創(chuàng)建grade表 if exists (select * from sysobjects where name = 'grade') drop table grade create table grade (id int not null,gname varchar(50) not null ) --添加約束 --主鍵 alter table student add constraint pksid primary key (id) --唯一約束 alter table student add constraint ukid unique(id) --默認(rèn)約束 alter table student add constraint df_name default('張三') for name --檢查約束 alter table student add constraint ck_age check (age>0 and age<100) --必須給另一個(gè)表添加主鍵 alter table grade add constraint pkid primary key (id)--外鍵約束 alter table student add constraint fkgid foreign key (id) references grade (id)總結(jié)
以上是生活随笔為你收集整理的sql server中创建数据库和表的语法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 9个最佳相机标识以及如何免费制作自己的[
- 下一篇: 干货!sqlserver数据库所有知识点