• 数据库、数据表的创建SP2


     

    本人前一个版本的数据库、数据表的创建由于不是很详细,于是通过细心的修订,已经修复了很多Bug,希望这篇文章能够给大家一些帮助

     1 --代表注释,相当于C#里的//
     2 --切换到master数据库,目的是查询所要创建的数据库存不存在
     3 use master 
     4 go
     5 --打开高级选项
     6 exec sp_configure 'show advanced options',1
     7 go
     8 RECONFIGURE
     9 go
    10 --将xp_cmdshell设置为true
    11 exec sp_configure 'xp_cmdshell',1
    12 go
    13 RECONFIGURE
    14 go
    15 --利用xp_cmdshell创建文件路径,防止出现路径不存在的错误
    16 exec xp_cmdshell 'mkdir E:C#学习项目实例超市会员信息管理系统'
    17 go
    18 ------------------------------------------------------
    19 --判断数据库是不否存在,存在的话就删除
    20 if exists(select * from sysdatabases where name='SuperMark')
    21      drop database SuperMark
    22 go
    23 --创建数据库SuperMark
    24 create database SuperMark
    25 on primary  --创建主数据文件SuperMark_data.mdf
    26 (
    27    
    28    name='SuperMark_data',--文件的名字
    29    size=3mb,           --文件的初始大小
    30    maxsize=1024mb,     --文件的最大大小
    31    filegrowth=3mb,    --文件的自动增长
    32    filename='E:C#学习项目实例超市会员信息管理系统SuperMark_data.mdf' --文件的路径
    33 
    34 )
    35 log on   --创建数据库的配置文件,它一般不指定maxsize
    36 (
    37    name='SuperMark_log',
    38    size=3mb,
    39    filegrowth=3mb,
    40    filename='E:C#学习项目实例超市会员信息管理系统SuperMark_log.ldf'
    41 )
    42 go
    43 --切换到SuperMark数据库,这步很有必要,不切换的话,下面创建的数据表就不是这个数据库的了
    44 use SuperMark 
    45 --判断States表是否存在,存在就删除
    46 if exists(select * from sysobjects where name='States')
    47     drop table States
    48 go
    49 --创建表States
    50 create table States
    51 (
    52     --添加字段
    53   --字段名 数据类型 约束(主键,标识符,check,非空等)
    54     Id int primary key identity(1,1),
    55     StatesName varchar(20) not null,
    56 )
    57 
    58 -----------------
    59 if exists(select * from sysobjects where name='UsersInfo')
    60     drop table UsersInfo
    61 go
    62 create table UsersInfo
    63 (
    64     Id int primary key identity(1,1),
    65     CustomerId varchar(20) not null,
    66     CustomerPassword varchar(20) not null,
    67     CustomerType varchar(10) not null,
    68     Score int not null,
    69     statusId int not null
    70 )
    71 --------------------------------------------
    72 --alter table 表名 语句用于在已有的表中添加、修改或删除列。
    73 alter table UsersInfo --(alter table UsersInfo with nocheck 这样在执行时不检查原有数据)
    74 --添加约束 add constraint 约束名 约束类型 具体操作
    75 --外键约束add constraint FK_teacher_subjectId foreign key(subjectid) references subject(id)on delete no action
    76 --主键约束add constraint PK_id primary key(id)
    77 --唯一键约束add constraint UQ_name unique(name)
    78 --Check约束add constraint CK_Age check(age>0 and age<=100)
    79 --默认值add constraint DF_Birthday default('1999-9-9') for birthday
    80 add constraint FK_UsersInfo_statusId foreign key(statusid) references states(id) on delete no action
    81 
    82 -----------------------------------------
    83 --为表States插入3条数据
    84 insert into States values('合法账户')
    85 insert into States values('非法账户')
    86 insert into States values('被禁账户')
    87 -----------------------------------------
    88 --为表UsersInfo插入3条数据
    89 insert into UsersInfo values('leichaowen','leichaowenmima','钻石卡',69999,1)
    90 insert into UsersInfo values('zhouyanqun','zhouyanqunmima','铂金卡',59999,1)
    91 insert into UsersInfo values('leiqun','leiqunmima','金卡',49999,1)
    View Code
  • 相关阅读:
    [NOI2010]航空管制
    [POI2008]POD-Subdivision of Kingdom
    CF17C Balance
    [HAOI2007]理想的正方形
    [Code+#1]大吉大利,晚上吃鸡!
    HDU 3371
    hdu1102
    最短路算法、应用、模板总结
    csu十月月赛 并查集+分组背包
    csu 十月月赛 最短路+最小费用
  • 原文地址:https://www.cnblogs.com/leiqun9497/p/create_database_datatble.html
Copyright © 2020-2023  润新知