sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1796 随笔 :: 22 文章 :: 24 评论 :: 226万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

SQlServer使用SQl语句创建数据库
原文连接:https://www.cnblogs.com/yuesebote/p/10717920.html

1.创建数据库SQL语句

use master
go

if exists(select * from sysdatabases where name='CommonPermission')
begin
    select '该数据库已存在'
    drop database CommonPermission        --如果该数据库已经存在,那么就删除它
end
else
begin
    create database CommonPermission
    on  primary        --表示属于 primary 文件组
    (
        name='cpDB_data',        -- 主数据文件的逻辑名称
        filename='C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQL\MSSQL\DATA\cpDB_data.mdf',    -- 主数据文件的物理名称
        size=5mb,    --主数据文件的初始大小
        maxsize=100mb,     -- 主数据文件增长的最大值
        filegrowth=15%        --主数据文件的增长率
    )
    log on
    (
        name='cpDB_log',        -- 日志文件的逻辑名称
        filename='C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQL\MSSQL\DATA\cpDB_log.ldf',    -- 日志文件的物理名称
        size=2mb,            --日志文件的初始大小
        maxsize=20mb,        --日志文件增长的最大值
        filegrowth=1mb        --日志文件的增长率
    )
end

2.创建表SQL语句

use CommonPermission    --表示设置为在该数据库(Test)执行下面的SQL语句
go

if exists(select * from sysobjects where name='SysUser')
begin
    select '该表已经存在'
    drop table SysUser        --删除表
end
else
begin
    create table SysUser
    (
        ID            int             not null    identity(1,1)    primary key,    --设置为主键和自增长列,起始值为1,每次自增1
        userID        nvarchar(20)    not null,
        userPassWord  nvarchar(50)    not null,
        userSex       nvarchar(20)        null,
        userName      nvarchar(20)    not null,
        creatTime     datetime            null,
        creatPerson   nvarchar(20)        null,
        updateTime    datetime            null,
        updatePerson  nvarchar(20)        null,
    )
end

--添加约束                        
alter table SysUser add constraint
UQ_userID   --约束名
unique        --约束类型(唯一约束)
(userID)    --列名

--删除约束
alter table SysUser drop constraint
UQ_userID    --约束名

3.SQL语句创建表变量:

declare @Score table
(
    Id        int        not null,
    Name    varchar(50)  null
)

insert into @Score
select '1','刘邦' union
select '2','项羽'

select * from @Score

4.SQL语句创建临时表:

-- ## 表示全局临时表
create table ##temp
(
    Id        int        not null,
    Name    varchar(10)        null
)

-- # 表示局部临时表
create table #temp
(
    Id        int        not null,
    Name    varchar(10)        null
)

5.SQL 语句创建表并设置主外键关系:

if exists(select * from sysObjects where name='Course')
begin
    select '该表已经存在'
    drop table Course
end
else
begin
    create table Course
    (
      --列名    字段类型  是否为空   标识外键列(外键列名)         关联表的表名(关联的字段名)
         Stu_Id        int        null    foreign key(Stu_Id) references Student(S_Id),
         C_Id        int        not null    identity(1,1)    Primary key,
         C_Name        varchar(100)    not null
     )
end

posted on   sunny123456  阅读(8305)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示