sqlserver 建表语句

CREATE TABLE [dbo].[teacher]
(
    [teacher_id] [tinyint] IDENTITY(1,1) NOT NULL,
    [teacher_name] [nvarchar](max) NOT NULL DEFAULT (N'无'),
    [gender] [nvarchar](max) NOT NULL DEFAULT (N'无'),
    [birthday] [date] DEFAULT (N'1990-01-01'),
    [address] [nvarchar](max) DEFAULT (N'无'),
    [idnumber] [nvarchar](max) DEFAULT (N'无'),
    [class_id] [int] not null ,
    PRIMARY KEY ([teacher_id]),
    CHECK ([gender]=N'男' OR [gender]=N'女' OR [gender]=N'无')
)
//===========================================================
//===========================================================
CREATE TABLE [dbo].[表名]
(
    [teacher_id] [tinyint] IDENTITY(1,1) NOT NULL, //自增长从1开始每次增加1
    [teacher_name] [nvarchar](max) NOT NULL DEFAULT (N'无'),//字段长度给最大
    [gender] [nvarchar](max) NOT NULL DEFAULT (N'无'),
    [birthday] [date] DEFAULT (N'1990-01-01'),
    [address] [nvarchar](max) DEFAULT (N'无'),
    [idnumber] [nvarchar](max) DEFAULT (N'无'),
    [class_id] [int] not null ,
    PRIMARY KEY ([teacher_id]),//设置主键
    CHECK ([gender]=N'男' OR [gender]=N'女' OR [gender]=N'无')
)

标准建表语句

-- 表名是 t_s_user_custom_config 
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[t_s_user_custom_config]') AND type in (N'U'))
DROP TABLE [dbo].[t_s_user_custom_config];
CREATE TABLE [dbo].[t_s_user_custom_config](
    config_id INT NOT NULL IDENTITY(1,1),
    user_id NVARCHAR(255),
    config_type NVARCHAR(255),
    config_value NVARCHAR(255),
    create_by VARCHAR(100),
    update_by VARCHAR(100),
    create_date DATETIME2,
    update_date DATETIME2,
    version INT,
    PRIMARY KEY (config_id)
)
-- 下面是给表和字段加注释
EXEC sp_addextendedproperty 'MS_Description', '用户自定义配置', 'SCHEMA', dbo, 'table', t_s_user_custom_config, null, null;
EXEC sp_addextendedproperty 'MS_Description', '主键id', 'SCHEMA', dbo, 'table', t_s_user_custom_config, 'column', config_id;
EXEC sp_addextendedproperty 'MS_Description', '用户id', 'SCHEMA', dbo, 'table', t_s_user_custom_config, 'column', user_id;
EXEC sp_addextendedproperty 'MS_Description', '配置类型(名字)', 'SCHEMA', dbo, 'table', t_s_user_custom_config, 'column', config_type;
EXEC sp_addextendedproperty 'MS_Description', '配置值(变量)', 'SCHEMA', dbo, 'table', t_s_user_custom_config, 'column', config_value;
EXEC sp_addextendedproperty 'MS_Description', '创建人', 'SCHEMA', dbo, 'table', t_s_user_custom_config, 'column', create_by;
EXEC sp_addextendedproperty 'MS_Description', '更新人', 'SCHEMA', dbo, 'table', t_s_user_custom_config, 'column', update_by;
EXEC sp_addextendedproperty 'MS_Description', '创建时间', 'SCHEMA', dbo, 'table', t_s_user_custom_config, 'column', create_date;
EXEC sp_addextendedproperty 'MS_Description', '更新时间', 'SCHEMA', dbo, 'table', t_s_user_custom_config, 'column', update_date;
EXEC sp_addextendedproperty 'MS_Description', '版本号', 'SCHEMA', dbo, 'table', t_s_user_custom_config, 'column', version;

你可能感兴趣的:(数据库,sqlserver,数据库,sql)