≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 数据库 > SQL Server

SQL Server创建数据库的完整代码

人气:880 时间:2020-11-27

这篇文章主要为大家详细介绍了SQL Server创建数据库的完整代码,具有一定的参考价值,可以用来参考一下。

感兴趣SQL Server创建数据库的完整代码的小伙伴,下面一起跟随四海网的小编罗X来看看吧。<br>
 
--1.创建数据库
-- 四海网:www.q1010.com
use master
go
 
if exists(select * from sysdatabases where name='Test')
begin
    select '该数据库已存在'
    drop database Test        --如果该数据库已经存在,那么就删除它
end
else
begin
    create database Test
    on  primary        --表示属于 primary 文件组
    (
        name='stuDB_data',        -- 主数据文件的逻辑名称
        filename='D:\stuDB_data.mdf',    -- 主数据文件的物理名称
        size=5mb,    --主数据文件的初始大小
        maxsize=100mb,     -- 主数据文件增长的最大值
        filegrowth=15%        --主数据文件的增长率
    )
    log on
    (
        name='stuDB_log',        -- 日志文件的逻辑名称
        filename='D:\stuDB_log.ldf',    -- 日志文件的物理名称
        size=2mb,            --日志文件的初始大小
        maxsize=20mb,        --日志文件增长的最大值
        filegrowth=1mb        --日志文件的增长率
    )
end
接下来是创建数据表的 SQL 语句:
use Test    --表示设置为在该数据库(Test)执行下面的SQL语句
go
 或者在这里选择数据库。
--四海网:www.q1010.com
use Test    --表示设置为在该数据库(Test)执行下面的SQL语句
go
 
if exists(select * from sysobjects where name='Student')
begin
    select '该表已经存在'
    drop table Student        --删除表
end
else
begin
    create table Student
    (
        S_Id        int        not null    identity(1,1)    primary key,    --设置为主键和自增长列,起始值为1,每次自增1
        S_StuNo        varchar(50)        not null,
        S_Name        varchar(20)        not null,
        S_Sex        varchar(10)        not null,
        S_Height    varchar(10)        null,
        S_BirthDate        varchar(30)        null
    )
end
 
--添加约束                       
alter table Student add constraint
UQ_S_StuNo    --约束名
unique        --约束类型(唯一约束)
(S_StuNo)    --列名
 
--删除约束
alter table Student drop constraint
UQ_S_StuNo    --约束名
SQL语句创建表变量:
declare @Score table
(
    Id        int        not null,
    Name    varchar(50)  null
)
 
insert into @Score
select '1','刘邦' union
select '2','项羽'
 
select * from @Score
SQL语句创建临时表:
-- ## 表示全局临时表
create table ##temp
(
    Id        int        not null,
    Name    varchar(10)        null
)
 
-- # 表示局部临时表
create table #temp
(
    Id        int        not null,
    Name    varchar(10)        null
)
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
2.完整SQL Server代码
--四海网:www.q1010.com
--创建数据表
 
use stu_db    --表示设置为在该数据库(Test)执行下面的SQL语句
go
 
if exists(select * from sysobjects where name='Students')
begin
    select '该表已经存在'
    drop table Students        --删除表
end
else
begin
    create table Students
    (
        stuID            int            not null    identity(1,1)    primary key,    --设置为主键和自增长列,起始值为1,每次自增1
        stuNumber  nvarchar(10)    not null,
        stuClass  nvarchar(50)    not null,
        stuName       nvarchar(20)        null,
        stuSex      nvarchar(20)    not null,
        stuAge      nvarchar(20)    not null,
    )
end
 
select *from Students
 
 
INSERT INTO Students VALUES ('001','软件01', '小明','男','18')
INSERT INTO Students VALUES ('002','软件01', '小李','男','18')
INSERT INTO Students VALUES ('003','软件06', '小丽','女','25')
INSERT INTO Students VALUES ('004','软件01', '小米','男','30')
 
select *from Students where stuNumber='008'
 
 
 
--添加
 
INSERT INTO [stu_db].[dbo].[Students]([stuNumber],[stuClass],[stuName],[stuSex],[stuAge])VALUES('008','计算机111','小牧','男','20')
 
--更改
 
UPDATE [stu_db].[dbo].[Students] SET [stuNumber] = '',[stuClass] = '',[stuName] = '',[stuSex] = '',[stuAge] = '' WHERE stuNumber='' AND stuClass=''
 
--删除
DELETE FROM [stu_db].[dbo].[Students]WHERE stuNumber='' AND stuClass=''
 
 
--查询
SELECT *FROM [stu_db].[dbo].[Students]WHERE stuNumber='' AND stuClass=''
 

本文来自:http://www.q1010.com/179/14292-0.html

注:关于SQL Server创建数据库的完整代码的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:

您可能感兴趣的文章

上一篇:SQL Server创建数据库的命令用法示例
下一篇:SQL Server函数或存储过程中抛出异常的实现方法
热门文章
  • SQL Server SQL获取第一条记录的方法
  • SQL Server出现System.OutOfMemoryException异常的解决方法
  • SQL Server的 update from 语句的简单示例
  • SQL Server 数据库备份方法菜鸟教程
  • SQL Server 多表关联时在where语句中慎用trim()方法
  • SQL Server数据类型及长度限制详细说明
  • mybaits非配置原因,导致SqlSession was not registered for synchronization异常解析
  • SQL Server 收缩后对数据库的使用有影响吗?
  • SQL Server 格式导致的Excel导入sql出现异常的解决方法
  • SQL Server 连接服务器出现错误 7391的解决方法
  • 最新文章
    • SQL Server存储过程基本语法的简单示例
    • sql查询时增加自动编号和分页的简单示例
    • sql轻松应付百万数据的高效数据分页存储过程的简单示例
    • sql获取一条数据中所有字段的名称和值的实现方法
    • sql分割函数的简单示例
    • SQL Server异常捕获的简单示例
    • SQL SERVER回滚恢复误操作数据的实现方法
    • SQL Server函数或存储过程中抛出异常的实现方法
    • SQL Server创建数据库的完整代码
    • SQL Server创建数据库的命令用法示例

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。