SQL Server 学习过程(二)中,有哪些进阶技巧分享?

2026-05-05 23:381阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1191个文字,预计阅读时间需要5分钟。

SQL Server 学习过程(二)中,有哪些进阶技巧分享?

休息了好长一段时间,这几天照着书本自己慢慢梳理的命令,看得再多也不如动手去做。+use HrSystem go create table Employees ( Em_id int primary key identity(1,1), -- 设置这个列为主键,并且作为唯一标识列

休息了好长一段时间,这几天照着书本自己慢慢敲的命令,看的再多不如手动去做。

SQL Server 学习过程(二)中,有哪些进阶技巧分享?

use HrSystem go create table Employees ( Em_id int primary key identity(1,1),--设置这个列为主键,并且为唯一标识列-- Emp_name varchar(50) not null, --不为空-- Sex char(2) DEFAULT('男'), -- 设置这个列默认为男 -- Title varchar(20) not null, Wage float default(0), IdCard varchar(20), Dep_id int not null ) --查看数据库的储存空间-- sp_spaceused N'Employees' --使用dbcc checkident命令检查和设置表的标识值-- use HrSystem go create table tmpTable ( id int primary key identity, name varchar(50) ) insert into tmpTable values('a') insert into tmpTable values('b') insert into tmpTable values('c') insert into tmpTable values('d') go dbcc checkident(tmpTable,noreseed) go --使用delete语句删除tmpTable表中的两条数据-- delete from tmpTable where id>2 --使用下面的语句重置标识符为2-- dbcc checkident(tmpTable,reseed,2) --验证标识符的值-- insert into tmpTable values('c') --重命名表名-- --语法:sp_rename 原对象名,新对象名,对象类型-- --存储过程使用sp_rename将Employees重命名为EmpInfo-- use HrSystem go sp_rename Employees,EmpInfo --修改表的列名,将表名-- /*使用存储过程sp_rename时,使用column对列名进行重命名 将表名Employees列Wage重命名为Salary*/ sp_rename 'Employees.Wage','Salary','column' --向表中添加列-- --使用alter table 语句向表中添加列-- --alter table 表名 add 列名 数据类型和长度 列属性 在Employees中增加列,列名为Tele,数据类型为varchar,长度为50,列属性允许为空 use HrSystem go alter table Employees add Tele varchar(50) null --修改列属性-- --alter table 表名 alter column 列名 新数据类型和长度 新列属性 --在表Employees中修改列Tele的数据值为char,长度为30,并允许为空 alter table Employees alter column Tele char(30) null --删除表中列-- alter table 表名 drop column 列名 --在表中Employees中删除列Tele alter table Employees drop column Tele --删除表-- drop table 表名 --删除表Departments drop table Departments --创建主键约束-- /*主键是表中的一列或者一组列,他们的值唯一的标识标中的每一行,在创建和修改表时,可以定义主键约束, 主键的列的值不允许为空*/ constraint 主键名 primary key [clustered|nonclustered] (列名1,[列名2]) --在HrSystem中建立表Super,并使用constraint子句定义主键PK_test use HrSystem go create table Super ( Super_id int, Super_name varchar(50), constraint pk_Super primary key(Super_id) ) --修改主键约束-- add constraint 主键名 primary key [clustered|nonclustered] (列名1,[列名2,]) --创建student,然后将Stuid列设置为主键 create table Student (Stuid int identity(1,1), StuName varchar(50) not null, Sex bit default(0), Class varchar(50) not null, Score float default(0), ) go alter table Student add constraint pk_Stu primary key nonclustered (Stuid) go --删除主键约束-- alter table 表名 drop constraint <primary key 约束名> 删除表Student主键约束PK_Stu alter table Student drop constraint PK_Stu --创建、修改、删除唯一约束性 --唯一性约束可以保证除主键外的其他一个或者多个列的数据唯一性,以防止在列中输入重复的值 --在Departents中的Dep_name列定义唯一性约束,在表中插入两条相同的数据,查看提示不允许插入重复值 /*该示例在表 PasswordHash 中的 PasswordSalt 和 Person.Password列上创建唯一约束。*/ USE AdventureWorks2012; GO ALTER TABLE Person.Password ADD CONSTRAINT AK_Password UNIQUE (PasswordHash, PasswordSalt); GO /*上条命令来自SQL SERVER示例*/ use HrSystem go alter table Departments add constraint Dep_name unique (Departments) --创建表Facker,将Facker_name设置为唯一性约束代码 use HrSystem go create table Facker (Facker_id int, Facker_name varchar(50), constraint PK_Facker primary key(Facker_id), constraint IX_Facker unique(Facker_name) ) --删除表Facker唯一约束IK_Facker alter table Facker drop constraint IK_Facker --从sys.key_constraints获取约束信息-- --系统视图 sys.key_constraints用来保存主键约束和唯一约束信息-- use HrSystem go select * from sys.key_constraints go --单独查询某一个约束信息 select * from sys.key_constraints where name='pk_Super' --违反约束检查-- /*constraint 约束名 check [not for replication] (逻辑表达式) */ /*创建Sutentone表,设置索引键和check约束*/ use HrSystem GO create table Sutentone (Stuid int identity(1,1), StuName varchar(50) not null, Sex bit default(0), Class varchar(50) not null, Score float default(0), constraint PK_Stdent primary key(Stuid), constraint ix_Stdent unique(StuName), constraint ck_Stdent check(Score>=0), ) go /*创建Client表,同时创建检查约束,定义邮政编码Postcode是由6位数字组成的字符串*/ use HrSystem go create table Client (id int identity(1,1), CitOrg varchar(50) not null, address varchar(100) not null, Postcode varchar(10) not null, constraint pk_Client primary key(id), constraint ix_Client unique(CitOrg), constraint ck_Client check(Postcode like'[0-9][0-9][0-9][0-9][0-9][0-9]'), --like是SQL SERVER关键字,用于对指定的字符串进行模式匹配,[0-9]指定单字符在0至9的范围。-- ) --修改约束检查-- use HrSystem go alter table Employees add constraint ck_sex check (Sex='男' or Sex='女') --删除检查约束-- --删除表Employees中ck_Sex的约束-- use HrSystem go alter table Employees drop constraint ck_Sex go --从information_schema.check_constraints获取检查约束信息-- --从系统视图中information_schema.check_constraints可以查看当前数据库中当前用户 有权限查看的所有检查约束信息-- select * from information_schema.check_constraints --创建和使用默认约束-- constraint 约束名 default 约束表达式 [for 列名] --在表Employees中的列Title的默认约束为‘职员’-- use HrSystem go alter table Employees add constraint de_Title default '职员' for Title go

本文共计1191个文字,预计阅读时间需要5分钟。

SQL Server 学习过程(二)中,有哪些进阶技巧分享?

休息了好长一段时间,这几天照着书本自己慢慢梳理的命令,看得再多也不如动手去做。+use HrSystem go create table Employees ( Em_id int primary key identity(1,1), -- 设置这个列为主键,并且作为唯一标识列

休息了好长一段时间,这几天照着书本自己慢慢敲的命令,看的再多不如手动去做。

SQL Server 学习过程(二)中,有哪些进阶技巧分享?

use HrSystem go create table Employees ( Em_id int primary key identity(1,1),--设置这个列为主键,并且为唯一标识列-- Emp_name varchar(50) not null, --不为空-- Sex char(2) DEFAULT('男'), -- 设置这个列默认为男 -- Title varchar(20) not null, Wage float default(0), IdCard varchar(20), Dep_id int not null ) --查看数据库的储存空间-- sp_spaceused N'Employees' --使用dbcc checkident命令检查和设置表的标识值-- use HrSystem go create table tmpTable ( id int primary key identity, name varchar(50) ) insert into tmpTable values('a') insert into tmpTable values('b') insert into tmpTable values('c') insert into tmpTable values('d') go dbcc checkident(tmpTable,noreseed) go --使用delete语句删除tmpTable表中的两条数据-- delete from tmpTable where id>2 --使用下面的语句重置标识符为2-- dbcc checkident(tmpTable,reseed,2) --验证标识符的值-- insert into tmpTable values('c') --重命名表名-- --语法:sp_rename 原对象名,新对象名,对象类型-- --存储过程使用sp_rename将Employees重命名为EmpInfo-- use HrSystem go sp_rename Employees,EmpInfo --修改表的列名,将表名-- /*使用存储过程sp_rename时,使用column对列名进行重命名 将表名Employees列Wage重命名为Salary*/ sp_rename 'Employees.Wage','Salary','column' --向表中添加列-- --使用alter table 语句向表中添加列-- --alter table 表名 add 列名 数据类型和长度 列属性 在Employees中增加列,列名为Tele,数据类型为varchar,长度为50,列属性允许为空 use HrSystem go alter table Employees add Tele varchar(50) null --修改列属性-- --alter table 表名 alter column 列名 新数据类型和长度 新列属性 --在表Employees中修改列Tele的数据值为char,长度为30,并允许为空 alter table Employees alter column Tele char(30) null --删除表中列-- alter table 表名 drop column 列名 --在表中Employees中删除列Tele alter table Employees drop column Tele --删除表-- drop table 表名 --删除表Departments drop table Departments --创建主键约束-- /*主键是表中的一列或者一组列,他们的值唯一的标识标中的每一行,在创建和修改表时,可以定义主键约束, 主键的列的值不允许为空*/ constraint 主键名 primary key [clustered|nonclustered] (列名1,[列名2]) --在HrSystem中建立表Super,并使用constraint子句定义主键PK_test use HrSystem go create table Super ( Super_id int, Super_name varchar(50), constraint pk_Super primary key(Super_id) ) --修改主键约束-- add constraint 主键名 primary key [clustered|nonclustered] (列名1,[列名2,]) --创建student,然后将Stuid列设置为主键 create table Student (Stuid int identity(1,1), StuName varchar(50) not null, Sex bit default(0), Class varchar(50) not null, Score float default(0), ) go alter table Student add constraint pk_Stu primary key nonclustered (Stuid) go --删除主键约束-- alter table 表名 drop constraint <primary key 约束名> 删除表Student主键约束PK_Stu alter table Student drop constraint PK_Stu --创建、修改、删除唯一约束性 --唯一性约束可以保证除主键外的其他一个或者多个列的数据唯一性,以防止在列中输入重复的值 --在Departents中的Dep_name列定义唯一性约束,在表中插入两条相同的数据,查看提示不允许插入重复值 /*该示例在表 PasswordHash 中的 PasswordSalt 和 Person.Password列上创建唯一约束。*/ USE AdventureWorks2012; GO ALTER TABLE Person.Password ADD CONSTRAINT AK_Password UNIQUE (PasswordHash, PasswordSalt); GO /*上条命令来自SQL SERVER示例*/ use HrSystem go alter table Departments add constraint Dep_name unique (Departments) --创建表Facker,将Facker_name设置为唯一性约束代码 use HrSystem go create table Facker (Facker_id int, Facker_name varchar(50), constraint PK_Facker primary key(Facker_id), constraint IX_Facker unique(Facker_name) ) --删除表Facker唯一约束IK_Facker alter table Facker drop constraint IK_Facker --从sys.key_constraints获取约束信息-- --系统视图 sys.key_constraints用来保存主键约束和唯一约束信息-- use HrSystem go select * from sys.key_constraints go --单独查询某一个约束信息 select * from sys.key_constraints where name='pk_Super' --违反约束检查-- /*constraint 约束名 check [not for replication] (逻辑表达式) */ /*创建Sutentone表,设置索引键和check约束*/ use HrSystem GO create table Sutentone (Stuid int identity(1,1), StuName varchar(50) not null, Sex bit default(0), Class varchar(50) not null, Score float default(0), constraint PK_Stdent primary key(Stuid), constraint ix_Stdent unique(StuName), constraint ck_Stdent check(Score>=0), ) go /*创建Client表,同时创建检查约束,定义邮政编码Postcode是由6位数字组成的字符串*/ use HrSystem go create table Client (id int identity(1,1), CitOrg varchar(50) not null, address varchar(100) not null, Postcode varchar(10) not null, constraint pk_Client primary key(id), constraint ix_Client unique(CitOrg), constraint ck_Client check(Postcode like'[0-9][0-9][0-9][0-9][0-9][0-9]'), --like是SQL SERVER关键字,用于对指定的字符串进行模式匹配,[0-9]指定单字符在0至9的范围。-- ) --修改约束检查-- use HrSystem go alter table Employees add constraint ck_sex check (Sex='男' or Sex='女') --删除检查约束-- --删除表Employees中ck_Sex的约束-- use HrSystem go alter table Employees drop constraint ck_Sex go --从information_schema.check_constraints获取检查约束信息-- --从系统视图中information_schema.check_constraints可以查看当前数据库中当前用户 有权限查看的所有检查约束信息-- select * from information_schema.check_constraints --创建和使用默认约束-- constraint 约束名 default 约束表达式 [for 列名] --在表Employees中的列Title的默认约束为‘职员’-- use HrSystem go alter table Employees add constraint de_Title default '职员' for Title go