博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL 常用的命令
阅读量:6457 次
发布时间:2019-06-23

本文共 5491 字,大约阅读时间需要 18 分钟。

 

--修改表名--格式:SP_RENAME TABLENAME,NEWTABLENAMESP_RENAME TABLENAME,NEWTABLENAME--只能对表,不能对临时表--修改字段名--格式:SP_RENAME 'TABLENAME.COLNAME',NEWCOLNAME,'COLUMN'SP_RENAME 'TABLENAME.COLNAME',NEWCOLNAME,'COLUMN' --添加字段--格式:ALTER TABLE TABLE_NAME ADD NEW_COLUMN DATA_TYPE [INTERALITY_CODITION]示例1ALTER TABLE STUDENT ADD NATIONALITY VARCHAR(20)ALTER TABLE STUDENT ADD ID IDENTITY INT(1,1)--示例2 添加INT类型的列,默认值为 0ALTER TABLE STUDENT ADD STUDENTNAME INT DEFAULT 0--示例3 添加INT类型的列,默认值为0,主键ALTER TABLE STUDENT ADD STUDENTID INT PRIMARY KEY DEFAULT 0 --示例4 判断STUDENT中是否存在NAME字段且删除字段IF EXISTS(SELECT * FROM SYSCOLUMNS WHERE ID=OBJECT_ID('STUDENT') AND NAME='NAME') BEGIN ALTER TABLE STUDENT DROP COLUMN NAME END 更改字段格式:ALTER TABLE TABLE_NAME ALTER COLUMN COLUMN_NAMEALTER TABLE STUDENT ALTER COLUMN NAME VARCHAR(200)删除字段格式:ALTER TABLE TABLE_NAME DROP COLUMN COLUMN_NAMEALTER TABLE STUDENT DROP COLUMN NATIONALITY;查看字段约束格式: SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE WHERE TABLE_NAME = TABLE_NAMESELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGEWHERE TABLE_NAME = 'STUDENT'查看字段缺省约束表达式 (即默认值等)格式:SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = TABLE_NAMESELECT TABLE_NAME, COLUMN_NAME, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNSWHERE TABLE_NAME='STUDENT'查看字段缺省约束名格式:SELECT NAME FROM SYSOBJECTS WHERE OBJECT_ID(TABLE_NAME)=PARENT_OBJ AND XTYPE=’D’SELECT NAME FROM SYSOBJECTSWHERE OBJECT_ID('表?名?')=PARENT_OBJ AND XTYPE='D'删除字段约束格式:ALTER TABLE TABLENAME DROP CONSTRAINT CONSTRAINTNAMEALTER TABLE STUDENT DROP CONSTRAINT PK__STUDENT__2F36BC5B772B9A0B添加字段约束格式:ALTER TABLE TABLENAME ADD CONSTRAINT CONSTRAINTNAME PRIMARY KEY (COLUMN_NAME)--示例1ALTER TABLE STUINFO ADD CONSTRAINT PK_STUNO PRIMARY KEY (STUNO)--示例2 添加主键约束(PRIMARY KEY)-- 存在主键约束PK_STUNO,则删除IF EXISTS(SELECT * FROM SYSOBJECTS WHERE NAME='PK_STUNO' AND XTYPE='PK')ALTER TABLE STUINFODROP CONSTRAINT PK_STUNOGO-- 重新添加主键约束PK_STUNOALTER TABLE STUINFO ADD CONSTRAINT PK_STUNO PRIMARY KEY (STUNO)GO--示例3 添加 唯一UQ约束(UNIQUE CONSTRAINT)-- 存在唯一约束UQ_STUNO,则删除IF EXISTS(SELECT * FROM SYSOBJECTS WHERE NAME='UQ_STUID' AND XTYPE='UQ')ALTER TABLE STUINFODROP CONSTRAINT UQ_STUIDGO-- 重新添加唯一约束UQ_STUIDALTER TABLE STUINFO ADD CONSTRAINT UQ_STUID UNIQUE (STUID)--示例4 添加默认DF约束(DEFAULT CONSTRAINT)-- 存在默认约束UQ_STUNO,则删除IF EXISTS(SELECT * FROM SYSOBJECTS WHERE NAME='DF_STUADDRESS' AND XTYPE='D')ALTER TABLE STUINFO DROP CONSTRAINT DF_STUADDRESSGO-- 重新添加默认约束DF_STUADDRESSALTER TABLE STUINFO ADD CONSTRAINT DF_STUADDRESS DEFAULT ('地址不详') FOR STUADDRESS--示例5 检查CK约束(CHECK CONSTRAINT)-- 存在检查约束UQ_STUNO,则删除IF EXISTS(SELECT * FROM SYSOBJECTS WHERE NAME='CK_STUAGE' AND XTYPE='C')ALTER TABLE STUINFO DROP CONS

 

  

 

 

 

--修改表名

--格式:SP_RENAME TABLENAME,NEWTABLENAME
SP_RENAME TABLENAME,NEWTABLENAME
--只能对表,不能对临时表

--修改字段名
--格式:SP_RENAME 'TABLENAME.COLNAME',NEWCOLNAME,'COLUMN'
SP_RENAME 'TABLENAME.COLNAME',NEWCOLNAME,'COLUMN'

 

--添加字段

--格式:ALTER TABLE TABLE_NAME ADD NEW_COLUMN DATA_TYPE [INTERALITY_CODITION]
示例1
ALTER TABLE STUDENT ADD NATIONALITY VARCHAR(20)
ALTER TABLE STUDENT ADD ID IDENTITY INT(1,1)

--示例2 添加INT类型的列,默认值为 0

ALTER TABLE STUDENT ADD STUDENTNAME INT DEFAULT 0

--示例3 添加INT类型的列,默认值为0,主键
ALTER TABLE STUDENT ADD STUDENTID INT PRIMARY KEY DEFAULT 0
--示例4 判断STUDENT中是否存在NAME字段且删除字段
IF EXISTS(SELECT * FROM SYSCOLUMNS WHERE ID=OBJECT_ID('STUDENT') AND NAME='NAME') BEGIN
ALTER TABLE STUDENT DROP COLUMN NAME
END
更改字段
格式:ALTER TABLE TABLE_NAME ALTER COLUMN COLUMN_NAME
ALTER TABLE STUDENT ALTER COLUMN NAME VARCHAR(200)
删除字段
格式:ALTER TABLE TABLE_NAME DROP COLUMN COLUMN_NAME
ALTER TABLE STUDENT DROP COLUMN NATIONALITY;
查看字段约束
格式: SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE WHERE TABLE_NAME = TABLE_NAME
SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
WHERE TABLE_NAME = 'STUDENT'
查看字段缺省约束表达式 (即默认值等)
格式:SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = TABLE_NAME
SELECT TABLE_NAME, COLUMN_NAME, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='STUDENT'
查看字段缺省约束名
格式:SELECT NAME FROM SYSOBJECTS WHERE OBJECT_ID(TABLE_NAME)=PARENT_OBJ AND XTYPE=’D’
SELECT NAME FROM SYSOBJECTS
WHERE OBJECT_ID('表?名?')=PARENT_OBJ AND XTYPE='D'
删除字段约束
格式:ALTER TABLE TABLENAME DROP CONSTRAINT CONSTRAINTNAME
ALTER TABLE STUDENT DROP CONSTRAINT PK__STUDENT__2F36BC5B772B9A0B
添加字段约束
格式:ALTER TABLE TABLENAME ADD CONSTRAINT CONSTRAINTNAME PRIMARY KEY (COLUMN_NAME)
--示例1
ALTER TABLE STUINFO ADD CONSTRAINT PK_STUNO PRIMARY KEY (STUNO)

--示例2 添加主键约束(PRIMARY KEY)

-- 存在主键约束PK_STUNO,则删除
IF EXISTS(SELECT * FROM SYSOBJECTS WHERE NAME='PK_STUNO' AND XTYPE='PK')
ALTER TABLE STUINFO
DROP CONSTRAINT PK_STUNO
GO
-- 重新添加主键约束PK_STUNO
ALTER TABLE STUINFO ADD CONSTRAINT PK_STUNO PRIMARY KEY (STUNO)
GO

--示例3 添加 唯一UQ约束(UNIQUE CONSTRAINT)

-- 存在唯一约束UQ_STUNO,则删除
IF EXISTS(SELECT * FROM SYSOBJECTS WHERE NAME='UQ_STUID' AND XTYPE='UQ')
ALTER TABLE STUINFO
DROP CONSTRAINT UQ_STUID
GO
-- 重新添加唯一约束UQ_STUID
ALTER TABLE STUINFO ADD CONSTRAINT UQ_STUID UNIQUE (STUID)

--示例4 添加默认DF约束(DEFAULT CONSTRAINT)

-- 存在默认约束UQ_STUNO,则删除
IF EXISTS(SELECT * FROM SYSOBJECTS WHERE NAME='DF_STUADDRESS' AND XTYPE='D')
ALTER TABLE STUINFO DROP CONSTRAINT DF_STUADDRESS
GO
-- 重新添加默认约束DF_STUADDRESS
ALTER TABLE STUINFO ADD CONSTRAINT DF_STUADDRESS DEFAULT ('地址不详') FOR STUADDRESS

--示例5 检查CK约束(CHECK CONSTRAINT)

-- 存在检查约束UQ_STUNO,则删除
IF EXISTS(SELECT * FROM SYSOBJECTS WHERE NAME='CK_STUAGE' AND XTYPE='C')
ALTER TABLE STUINFO DROP CONS

转载于:https://www.cnblogs.com/qianjinyan/p/9790020.html

你可能感兴趣的文章
Android快速开发常用知识点系列目录
查看>>
Java ActiveMQ队列模式案例
查看>>
EJB2的配置
查看>>
最容易理解的对卷积(convolution)的解释
查看>>
《机器学习实战》知识点笔记目录
查看>>
Linux操作系统实时性分析
查看>>
mysql导出导入所有数据库
查看>>
[转载]数据库缓存算法思想与实现
查看>>
完美解决NC502手工sql的查询引擎排序及合计问题
查看>>
PHP+MySQL代码部署在Linux(Ubuntu)上注意事项
查看>>
Tiny语言执行环境TM机源码
查看>>
PE文件之资源讲解
查看>>
windows 7/mac编译cocos2d-x-3.2*的android工程报错
查看>>
MYSQL导入导出.sql文件(转)
查看>>
使用Elasticsearch、Logstash、Kibana与Redis(作为缓冲区)对Nginx日志进行收集(转)
查看>>
git review报错一例
查看>>
Tomcat在Linux上的安装与配置
查看>>
《信息安全系统设计基础》 课程教学
查看>>
Linux平台下使用rman进行oracle数据库迁移
查看>>
全栈工程师学习Linux技术的忠告
查看>>