当前位置:网站首页>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 表名 --将删除表中所有的行,而且不会把每一行的删除操作都计入日志文件中,这样速度就会比较快,另外,表的结构、列、约束、索引等保持不变
边栏推荐
- Centos8 (liunx) deploying WTM (asp.net 5) using PgSQL
- Parallelism, concurrency and several directions for high concurrency optimization
- Redis implementation of distributed lock solution
- (转载)ArcGIS Engine中各种点的创建方法
- GIS方法类期刊和论文的综述(Introduction)怎么写?
- centos8(liunx)部署WTM(ASP.NET 5)使用pgsql
- [leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间
- 第6期:大学生应该选择哪种主流编程语言
- 第7期:内卷和躺平,你怎么选
- Redis docker instance and data structure
猜你喜欢
Uniapp uses the simple method signalr (only for web debugging, cannot package apps)
【机器学习小记】【风格迁移】deeplearning.ai course4 4th week programming(tensorflow2)
RT-Thread 学习笔记(三)---用SCons 构建编译环境
【论文下饭】Deep Mining External Imperfect Data for ChestX-ray Disease Screening
在神州IV开发板上成功移植STemWin V5.22
[leetcode每日一题2021/2/18]【详解】995. K 连续位的最小翻转次数
从蚂蚁的觅食过程看团队研发(转载)
Zongzi battle - guess who can win
[leetcode daily question 2021/2/13]448. Find all the missing numbers in the array
文案秘籍七步曲至----文献团队协作管理
随机推荐
Flutter CachedNetworkImage圆角
鹏哥C语言第四课(3)
【机器学习小记】【风格迁移】deeplearning.ai course4 4th week programming(tensorflow2)
Constructors, method overloads, object arrays, and static
7-25 0-1背包 (50分)
2021-08-14三子棋
英语基础句型结构------起源
【小程序】onReachBottom 事件为什么不能触发 ?(一秒搞定)
剑指Offer(五):用两个栈实现队列
.net operation redis sorted set ordered set
mysql20210906
kali 查看ip地址
鹏哥C语言20210811程序结构作业
Flutter 防止科学计数并去除尾数无效0
RT-Thread 学习笔记(六)--- 开启基于SPI Flash的elmfat文件系统(上)
20210807#1 C语言程序结构
在altium designer中禁用USBJATG
剑指Offer(二十):包含min函数的栈
display-inline+calc实现左中右布局,中间自适应
[leetcode daily question 2021/4/29]403. Frogs cross the river