zoukankan      html  css  js  c++  java
  • mssql清空数据和添加主键约束语法

     
     
     
    1、原始表
    CREATE TABLE [dbo].[BASE_UserInDept](
        [HospitalID] [int] NOT NULL,
        [UserID] [int] NOT NULL,
        [DeptCode] [varchar](10) NOT NULL,
        [RoleID] [int] NOT NULL
    ) ON [PRIMARY]  
    GO
    2、可以在建表的时候添加主键
    CREATE TABLE [dbo].[BASE_UserInDept](
     [HospitalID] [int] NOT NULL,
     [UserID] [int] NOT NULL,
     [DeptCode] [varchar](10) NOT NULL,
     [RoleID] [int] NOT NULL,
    --添加主键约束
    CONSTRAINT [PK_BASE_UserInDept] PRIMARY KEY CLUSTERED ( [HospitalID] ASC, [UserID] ASC, [DeptCode] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] GO

    --添加主键DF_BASE_UserInDept默认约束
    ALTER TABLE [dbo].[BASE_UserInDept] ADD CONSTRAINT [DF_BASE_UserInDept] DEFAULT ((0)) FOR [RoleID] GO

     3、也可以直接使用

    ALTER TABLE table ADD CONSTRAINT 键名 PRIMARY KEY ...添加

    ALTER TABLE [dbo].[BASE_UserInDept] ADD  CONSTRAINT [PK_BASE_UserInDept] PRIMARY KEY CLUSTERED 
    (
     [HospitalID] ASC,
     [UserID] ASC,
     [DeptCode] ASC
    )ON [PRIMARY]

    4、清空表数据语法

    TRUNCATE TABLE name

  • 相关阅读:
    前端-JavaScript
    前端-HTML
    Python源程序(.py)转换为可执行文件(.exe)
    进程
    算法之动态规划问题
    算法之斐波那契数列
    贪心算法找零问题
    算法之迷宫问题
    数据结构相关知识
    常用排序算法
  • 原文地址:https://www.cnblogs.com/betobe/p/13714874.html
Copyright © 2011-2022 走看看