sql server 建表常用语句 (包括主键,外键 等)

Posted jwsoft007

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql server 建表常用语句 (包括主键,外键 等)相关的知识,希望对你有一定的参考价值。


sql server 建表常用语句 (包括主键,外键 等)

USE [YGGL]
GO

if exists(select 1 from sysobjects where name=Salary and type=u)
DROP TABLE [dbo].[Salary]
GO


CREATE TABLE [dbo].[Salary](
[EmployeeID] [char](6) NOT NULL PRIMARY KEY,
[InCome] [float] NOT NULL,
[OutCome] [float] NULL
)

if exists(select 1 from sysobjects where name= fk_DepartmentID and xtype= F )
ALTER TABLE dbo.Employes DROP CONSTRAINT fk_DepartmentID


/****** Object: Table [dbo].[Departments] Script Date: 2015/11/11 9:48:06 ******/
if exists(select 1 from sysobjects where name=Departments and type=u)
DROP TABLE [dbo].[Departments]
GO

CREATE TABLE [dbo].[Departments](
[DepartmentID] [char](3) NOT NULL PRIMARY KEY ,
[DepartmentName] [char](20) NOT NULL,
[Note] [char](16) NULL,
)

if exists(select 1 from sysobjects where name=Employes and type=u)
DROP TABLE [dbo].[Employes]
GO

CREATE TABLE [dbo].[Employes](
[EmployeeID] [char](6) NOT NULL PRIMARY KEY,
[Name] [char](10) NOT NULL,
[Birthday] [datetime] NOT NULL,
[Sex] [bit] NOT NULL,
[Address] [char](20) NULL,
[Zip] [char](6) NULL,
[PhoneNumber] [char](12) NULL,
[EmailAddress] [char](30) NULL,
[DepartmentID] [char](3) NOT NULL
)

ALTER TABLE [dbo].[Employes] ADD CONSTRAINT [fk_DepartmentID] FOREIGN KEY([DepartmentID])
REFERENCES [dbo].[Departments] ([DepartmentID])
GO



以上是关于sql server 建表常用语句 (包括主键,外键 等)的主要内容,如果未能解决你的问题,请参考以下文章

sql创建表格语句主键自动增加

常用SQL语句DDL建表建索引建序列(PGSQL)

用SQL语句获得PostgreSQL表的主键

sql 添加外键语句

常用SQL语句常见建表建索引等DDL语句(PGSQL)

sql中外键怎么写?