当前位置:网站首页>Sql Server 之SQL语句对基本表及其中的数据的创建和修改
Sql Server 之SQL语句对基本表及其中的数据的创建和修改
2022-07-26 10:40:00 【孩纸D】
SQL语句创建基本表
create table { < 表名 > | 数据库名 . [ dbo . ] 表名 }
(
列名 数据类型 [ 列级完整性约束条件 ]
[ , 列名 数据类型 [ 列级完整性约束条件 ] ]
......
[ 表级完整性约束条件 ]
)
【列级约束性条件】是指直接在列的后面添加 primary key (主键约束)、unique(唯一性约束)或 NULL、NOT NULL 或identity(seed,increment)(标识列)、default 表达式(指定默认值)。
【表级约束性条件】是指在最后添加 primary key (列名,列名 . . . ) (指定多列为主键)、foreign key( 外键列名 ) references 主表(列名) (创建外键约束)。
还有一个创建临时表:
create table # 表名
. . . . . .
“#”表示临时本地表,在当前数据库内使用,在当前数据库关闭时删除;“##”表示全局临时表,可在所有数据库中使用,在关闭所有数据库是删除。
SQL语句修改基本表
Alter Table < 表名 >
[ Alter column <旧列名>
新列名 | 新的数据类型 | [NULL | NOT NULL]
]
[ Add [column] <新列名> < 数据类型 > [完整性约束] ] --增加列
[ Add <表级完整性约束> ] [ , . . . n ]
[ Drop [column] <列名> [cascade | restrict] ] --删除某列,若加上cascade则表示也会删除引用了该列的其他列,若加上restrict,若该列被其他对象引用,则不会被删除此列。
[ Drop constraint <完整性约束名(列名)> ] --删除某列的完整性约束条件
删除基本表
Drop Table <表名> [ cascade | restrict ] --若加上cascade则在删除基本表的同时相关依赖对象也将被删除,若加上restrict,若有依赖该表的对象,则不会被删此表。
表中数据的维护
插入数据
insert <表名>
[ (列名,列名,. . . ) ]
values (' 数据 ',' 数据 ',. . .) [ , ( ' 数据 ', . . . ) ]
列名和数据是一一对应的,若没有指定列名,则按默认列名排序
修改数据
update 表名
set
列名 = 新值 [ , . . . n] -- 将把此列全部改为该新值
[ from 其他表名 [, . . .n] ] --指定要更新的数据将来自其他表
[ where 条件 ] --通过约束条件来限定要更新的行,而不是对每一行数据都进行更新
Truncate Table 表名 --将删除表中所有的行,而且不会把每一行的删除操作都计入日志文件中,这样速度就会比较快,另外,表的结构、列、约束、索引等保持不变
边栏推荐
- SAP ABAP 守护进程的实现方式
- Analysis of the transaction problem of chained method call
- mysql20210906
- RT-Thread 学习笔记(三)---用SCons 构建编译环境
- oracle 启动不了 tnslistener服务启动不了
- [notes on machine learning] [building a cyclic neural network and its application] deeplearning ai course5 1st week programming(keras)
- 剑指Offer(五):用两个栈实现队列
- Codepoint 58880 not found in font, aborting. flutter build apk时报错
- 【机器学习小记】【搭建循环神经网络及其应用】deeplearning.ai course5 1st week programming(keras)
- 第4期:大学生提前职业技能准备之一
猜你喜欢
![[leetcode daily question 2021/2/14]765. Lovers hold hands](/img/be/8639a05c733638bf0b3fdeb11abccf.png)
[leetcode daily question 2021/2/14]765. Lovers hold hands

GIS方法类期刊和论文的综述(Introduction)怎么写?

Application of.Net open source framework in industrial production
![[leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间](/img/e7/a48bb5b8a86cbc4cd5b37bb16661a8.png)
[leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间

RT-Thread 学习笔记(三)---用SCons 构建编译环境

在神州IV开发板上为STemWin 5.22加入触屏驱动

Issue 8: cloud native -- how should college students learn in the workplace

SAP ABAP 守护进程的实现方式

第8期:云原生—— 大学生职场小白该如何学

.NET5WTM(ASP.NET Core) PGSql开箱操作
随机推荐
Redis implementation of distributed lock solution
一文详解Nodejs中fs文件模块与path路径模块
RT-Thread 学习笔记(七)---开启基于SPI Flash的elmfat文件系统(中)
router.push(),router.repalce(),router.go()使用
SAP ABAP 守护进程的实现方式
Koin
剑指Offer(二十):包含min函数的栈
C语言鹏哥20210812C语言函数
12 复制对象时勿忘其每一个成分
鹏哥C语言第四课(3)
GIS方法类期刊和论文的综述(Introduction)怎么写?
鹏哥C语言第六节课
【机器学习小记】【搭建循环神经网络及其应用】deeplearning.ai course5 1st week programming(keras)
剑指Offer(十):矩形覆盖
11 handle "self assignment" in operator=
使用Geoprocessor 工具
使用float实现左中右布局,中间内容自适应
Centos8 (liunx) deploying WTM (asp.net 5) using PgSQL
px2rem-loader将px转化为rem,适配移动端vant-UI等框架
[paper after dinner] deep mining external perfect data for chestx ray disease screening