标签:
创建数据库:
USE master GO IF EXISTS (SELECT * FROM sysdatabases WHERE name=‘stuDB‘) DROP DATABASE stuDB CREATE DATABASE stuDB ON PRIMARY ( /*--数据文件的具体描述*/ NAME=‘stuDB_data‘, FILENAME=‘D:\MyCreateDBTest\stuDB_data.mdf‘, SIZE=3mb, MAXSIZE=100mb, FILEGROWTH=15% ) LOG ON ( /*日记文件的具体描述*/ NAME=‘stuDB_log‘, FILENAME=‘D:\MyCreateDBTest\stuDB_log.ldf‘, SIZE=1mb, FILEGROWTH=1mb ) GO
创建表:
USE stuDB GO IF EXISTS(SELECT * FROM sysobjects WHERE name=‘stuInfo‘) DROP TABLE stuInfo CREATE TABLE stuInfo /*创建学员信息表*/ ( stuID INT IDENTITY(1,1) PRIMARY KEY, ----编号,标识列 stuName VARCHAR(20) NOT NULL, ---学生姓名,非空(必填) stuNo CHAR(6) NOT NULL, ---学号,非空 stuAge INT NOT NULL, ---年龄 stuID NUMERIC(18,0) , ---学生身份证号 stuAddress TEXT ---学生住址 ) GO IF EXISTS(SELECT * FROM sysobjects WHERE name=‘stuMarks‘) DROP TABLE stuMarks CREATE TABLE stuMarks /*创建学员成绩表*/ ( markID INT IDENTITY(1,1) PRIMARY KEY, stuID INT NOT NULL, mark FLOAT NOT NULL ) GO
创建约束:
ALTER TABLE stuMarks ADD CONSTRAINT FK_stuID FOREIGN KEY(stuID) REFERENCES stuInfo(stuID) /*检查约束,要求年龄只能在15~40岁之间*/ ALTER TABLE stuInfo ADD CONSTRAINT CK_stuAge CHECK(stuAge BETWEEN 15 AND 40) /*默认约束,如果地址不详,则默认为“地址不详”*/ ALTER TABLE stuInfo ADD CONSTRAINT DF_stuAddress DEFAULT (‘地址不详‘) FOR stuAddress GO
删除约束:
ALTER TABLE stuInfo DROP CONSTRAINT FK_stuID
标签:
原文地址:http://www.cnblogs.com/laixiancai/p/4390418.html