sql server创建数据表的完整语法_sqlserver语法大全

sql server创建数据表的完整语法_sqlserver语法大全sqlserver基础语法创建数据库创建表1.创建数据库2.表的创建3.在现有表中添加标识列4.创建外键5.添加外键6.约束7.创建局部临时表8.创建全局临时表9.创建具有check约束字段的数据库表10.创建含有计算字段的数据库表11.创建含有自动编号字段的数据库表12.创建含有排序字段和默认值的数据表13.动态判断数据库表是否存在14.查看表的各种信息,可以查看指定数据库表的属性、表中字…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

1.创建数据库

语法:CREATE DATABASE <数据库名称>

Jetbrains全家桶1年46,售后保障稳定

CREATE DATABASE dbname -- 创建名为 dbname 的数据库

2.表的创建

 语法:
USE suntest  
create table 仓库  
(  
仓库编号 int ,   
仓库号 varchar(50) ,   
城市 varchar(50) ,   
面积 int  
)  
create table 仓库1  
(  
仓库编号 int not null ,   
仓库号 varchar(50) not null,   
城市 varchar(50) not null, --不能为空not null--  
面积 int  
)  
create table 仓库2  
(  
仓库编号 int primary key , --主键的关键字primary key--  
仓库号 varchar(50) unique, --唯一索引关键字unique--  
城市 varchar(50) not null, --不能为空not null--  
面积 int  
)  
create table 仓库3  
(  
仓库编号 int primary key , --主键的关键字primary key--  
仓库号 varchar(50) unique, --唯一索引关键字unique--  
城市 varchar(50) default '青岛', --不能为空not null--  
面积 int check (面积>=300 and 面积<=1800)  
)  
create table 职工表  
(  
职工编号 int identity (1,1) primary key,  
职工号 varchar(50) unique,  
仓库号 varchar(50),  
工资 int check(基本工资>=800 and 基本工资<=2100),  
)  
create table 订单表  
(  
订单编号 int identity(1,1) primary key,  
订单号 varchar(50) unique,  
职工号 varchar(50) references 职工表(职工号),--references两张表通过“职工号”关联--  
订购日期 datetime,  
销售金额 int  
)  
create table 阳光工资表  
(  
职工编号 int identity (1,1) primary key,  
职工号 varchar(50) unique,  
仓库号 varchar(50),  
基本工资 int check(基本工资>=800 and 基本工资<=2100),  
加班工资 int,  
奖金 int,  
扣率 int,  
应发工资 as (基本工资+加班工资+奖金-扣率) --as为自动计算字段,不能输入值--  
) 


3.在现有表中添加标识列

下面的例子向表T_test中添加一个名为ID,类型为int,种子为1,递增量为1的标识列
--创建表
CREATE TABLE T_test
(Name varchar(50)
)
--插入数据
INSERT T_test(Name) VALUES('张三')
--增加标识列
ALTER TABLE T_test
ADD ID int IDENTITY(1,1)

注:这只适用于刚建完表的情况,如果此时主键已经使用过了,表中存在许多数据,不能使用该方法删除主键,会导致数据丢失。(可行的方法,建一张相同的表来存储数据,在修改,插入)。

4.创建外键

create table 表名(
列名1 参数,
列名2 参数,
foreign key(列名) references 目标表名(目标列名)
);

5.添加外键

比如stuInfo(学生信息表)表是主表。他的主键是stuID,
另外还有一个stuExam表(学生考试成绩表)。在这个表中也有个列是stuID,但是要引用主表中的stuID.
那么在创建约束的时候:
alter table stuExam
add constraint fk_stuID foreign key(stuID) references stuInfo(stuID)
go

6.约束

 primary key   		主键
 not null, 			不能为空not null
 unique, 			唯一索引关键字unique
 check (面积>=300 and 面积<=1800)    check 约束

约束
非空约束 --NN,ont null constraint
必须填写数据不能为空
--指定表 Student 添加名为NN_Student_sClassId非空约束(指定列名sClassId),括号输入表达式
alter table Student add constraint NN_Student_sClassId check(sClassId is not null)
 
主键约束 --PK,primary key constraint
唯一且不为空
--指定表 Student 添加名为PK_Student_sId主键约束(指定列名sId)
alter table Student add constraint PK_Student_sId primary key(sId)
 
唯一约束 --UQ,unique constraint
唯一,允许为空,但是同样的数据只能出现一次
--指定表 Student 添加名为UQ_Student_sName唯一约束(指定列名sName)
alter table Student add constraint UQ_Student_sName unique(sName)
    
默认约束 --DF,default constraint
设置默认值
--指定表 Student 添加名为DF_Student_sName默认约束(指定列名sBirthday),获取当前日期
alter table Student add constraint DF_Student_sName default(getdate()) for sBirthday
 
--指定表 Student 添加名为DF_Student_sName默认约束(指定列名sBirthday),指定日期  
alter table Student add constraint DF_Student_sName default('1995-12-12') for sBirthday  
--指定表 Student 添加名为DF_Student_sName默认约束(指定列名sSex),指定性别
alter table Student add constraint DF_Student_sSex default('男') for sSex     
 
检查约束 --CK,check constraint
设置范围以及格式限制
--指定表 Student 添加名为 CK_Student_sSex检查约束(指定列名sSex),限制为'男'或者'女'
alter table Student add constraint CK_Student_sSex check(sSex='男' or sSex='女')   
--指定表 Student 添加名为 CK_Student_sSex检查约束(指定列名sAge),限制为0-100之间的数字
alter table Student add constraint CK_Student_sAge check(sAge>=0 and sAge<=100)   
 
外键约束  --FK,foreign key constraint
表关系
alter table Student add constraint Fk_Student_sClassId foreign key(sClassId) references Class(cId)
--指定表Student添加sClassId外键为Class的主键cId
on delete cascade on update  cascade --级联删除 --级联更新
 
删除约束
alter table Student drop Constraint NN_Student_sClassId    --删除指定表中的约束

7.创建局部临时表

use db_sqlserver
go
create table #db_local_table
(
  id  int,
  name varchar(50),
  age int,
  area int
)
创建的临时表不能与其他会话共享,当会话结束时,行和表的定义都将被删除

8.创建全局临时表

use db_sqlserver
go
create table ##db_local_table
(
  id  int,
  name varchar(50),
  age int,
  area int
)
全局临时表对所有用户都是可见的,在每个访问该表的用户都断开服务器连接时,全局临时表才会被删除

9.创建具有check约束字段的数据库表

use db_sqlserver;
go
create table db_table7
(
  仓库编号 int primary key,
  职工号  varchar(50) unique,
  仓库号  varchar(50),
  工资   int,
  面积  int check(面积>=600 and 面积<=1800)
)

10.创建含有计算字段的数据库表

use db_sqlserver;
go
create table db_table8
(
  职工编号 int primary key,
  职工号 varchar(50) unique,
  仓库号 varchar(50),
  基本工资 int check(基本工资>=800 and 基本工资<=2100),
  加班工资 int,
  奖金 int,
  扣率 int,
  应发工资 as (基本工资 + 加班工资 + 奖金 - 扣率)
)

11.创建含有自动编号字段的数据库表

use db_sqlserver;
go
create table db_table9
(
   仓库编号 int identity(1,1) primary key,
   仓库号 varchar(50) unique,
   城市 varchar(50) default('青岛'),
   面积 int check(面积>=300 and 面积<=1800)
)

12.创建含有排序字段和默认值的数据表

create table db_table10 
(
   仓库编号 int identity(1, 1) primary key,
   仓库号 varchar(50) collate french_CI_AI not null,
   城市 varchar(50) default '青岛',
   面积 int check(面积>=300 and 面积<=1800)
)

13.动态判断数据库表是否存在

use db_sqlserver;
go
if(Exists(select * from sys.sysobjects where id=OBJECT_ID('db_table9')))
  print '数据库表名已经存在'
  
else 
  print '该数据库表名不存在,可以利用该名创建表'

14.查看表的各种信息,可以查看指定数据库表的属性、表中字段属性、各种约束等信息

use db_sqlserver;
go
execute sp_help db_table9;

15.用select语句查看数据库表的属性信息

use db_sqlserver;
go
select * from sysobjects where type='U'

16.重命名数据库表

use db_sqlserver;
go
execute sp_rename "db_table9", "db_renametable"

17.增加数据库表的新字段

use db_sqlserver;
go
alter table db_table1 add 电子邮件 varchar(50)
alter table db_table1 add 联系方式 varchar(50) default '0532-88886396'
 
select name 字段名, xusertype 类型编号, length 长度 from syscolumns where id = object_id('db_table1')

18.修改数据库表的字段

use db_sqlserver;
go
alter table db_table1 alter column 电子邮件 varchar(200)
 
 
select name 字段名, xusertype 类型编号, length 长度 from syscolumns where id = object_id('db_table1')

19.删除数据库表字段

use db_sqlserver;
go
alter table db_table1 drop column 电子邮件 
 
 
select name 字段名, xusertype 类型编号, length 长度 from syscolumns where id = object_id('db_table1')

20.删除数据库表

use db_sqlserver;
go
drop table db_table1
drop table db_table1, db_table2
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/219255.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • js后退按钮事件触发

    js后退按钮事件触发if(window.history&&window.history.pushState){$(window).on(‘popstate’,function(){varhashLocation=location.hash;varhashSplit=hashLocation.split(“#!/”);…

    2022年7月25日
    12
  • RabbitMQ Network Partitions

    RabbitMQ Network PartitionsClusteringandNetworkPartitionsRabbitMQclustersdonottoleratenetworkpartitionswell.IfyouarethinkingofclusteringacrossaWAN,don’t.Youshouldusefederationortheshovelinstead.H

    2022年6月26日
    26
  • Vue(10)表单输入绑定v-model「建议收藏」

    Vue(10)表单输入绑定v-model「建议收藏」v-modelv-model指定可以实现表单值与属性的双向绑定。即表单元素中更改了值会自动的更新属性中的值,属性中的值更新了会自动更新表单中的值绑定的属性和事件v-model在内部为不同的输入元

    2022年7月29日
    6
  • js数组怎么删除指定元素_给数组添加一个元素的方法

    js数组怎么删除指定元素_给数组添加一个元素的方法js数组是js部分非常重要的知识,有时我们有这么个需求js数组删除指定元素,先定义一个函数来获取删除指定元素索引值,然后用js数组删除的方法,来删除指定元素即可,就两步不难,很简单。1、JS的数组对象定义一个函数,用于查找指定的元素在数组中的位置,也就是索引值,代码如下: 1 2 3 4 5 6 Array….

    2022年9月27日
    0
  • postman汉化版下载_postcrossing中文版

    postman汉化版下载_postcrossing中文版postman汉化版下载之后直接解压使用或者替换对应版本下的resources/app.asar重要提醒:不要更新下载地址:链接:https://share.weiyun.com/0TFvHgOq密码:<atitle=”o35jeq”>&nbsp;&nbsp;</a>…

    2022年9月27日
    0
  • 0xc0000225无法进系统_win10系统出现0xc0000225无法进入系统的恢复方法

    0xc0000225无法进系统_win10系统出现0xc0000225无法进入系统的恢复方法win10系统出现0xc0000225无法进入系统的恢复方法?win10系统有很多人都喜欢使用,我们操作的过程中常常会碰到win10系统出现0xc0000225无法进入系统的问题。如果遇到win10系统出现0xc0000225无法进入系统的问题该怎么办呢?很多电脑水平薄弱的网友不知道win10系统出现0xc0000225无法进入系统究竟该怎么解决?其实不难根据下面的操作步骤就可以解决问题 第一步、…

    2022年6月26日
    46

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号