zoukankan      html  css  js  c++  java
  • SqlServer主键外键添加及判断表是否存在

    GO
    --判断表是否存在方式1
    if object_id(N'EF_User',N'U') is null
    --判断表是否存在方式2
    --if not exists (select * from dbo.SysObjects WHERE id = object_id(N'[EF_User]') AND OBJECTPROPERTY(ID, 'IsTable') = 1) 
    begin
    --直接创建自增且指定主键约束的表
    CREATE TABLE [dbo].[EF_User](
        [ID] [int] identity(1,1) not null,
        [loginName] [nvarchar](100) NULL,
        [realName] [nvarchar](100) NULL,
        [phoneNo] [nvarchar](100) NULL,
        CONSTRAINT [PK_User] PRIMARY KEY(ID)
    )
    --删除主键约束
    alter table [dbo].[EF_User] drop constraint PK_User
    --添加主键约束
    alter table [dbo].[EF_User] add constraint PK_User primary key(ID)
    
    end
    
    GO
    CREATE TABLE [dbo].[EF_Role](
        [ID] [int] identity(1,1) not null,
        [Name] [nvarchar](100) NULL,
        [remark] [nvarchar](100) NULL,
        constraint [PK_Role] Primary Key(ID)
    ) 
    
    GO
    CREATE TABLE [dbo].[EF_User_Role](
        [UserID] [int] NOT NULL,
        [RoleID] [int] NOT NULL
    )
    --添加外键约束
    alter table EF_User_Role add constraint FK_User_Role_User foreign key (UserID)
    references EF_User(ID)
    alter table EF_User_Role add constraint FK_User_Role_Role foreign key (RoleID)
    references EF_Role(ID)
    GO
  • 相关阅读:
    软件课程设计(3)
    软件课程设计(2)
    软件课程设计(1)
    继往开来第五天
    勤勤恳恳第四天
    撸起袖子第三天
    厉兵秣马第二天
    项目初定第一天
    Magic-Club第五天
    Magic-Club第四天
  • 原文地址:https://www.cnblogs.com/wanghonghu/p/5585962.html
Copyright © 2011-2022 走看看