iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >SQL SERVER 学习过程(二)
  • 920
分享到

SQL SERVER 学习过程(二)

SQLSERVER学习过程(二) 2017-07-04 18:07:10 920人浏览 猪猪侠
摘要

休息了好长一段时间,这几天照着书本自己慢慢敲的命令,看的再多不如手动去做。 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 "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 
删除表Student主键约束PK_Stu
alter table Student drop constraint PK_Stu

--创建、修改、删除唯一约束性
--唯一性约束可以保证除主键外的其他一个或者多个列的数据唯一性,以防止在列中输入重复的值
--在Departents中的Dep_name列定义唯一性约束,在表中插入两条相同的数据,查看提示不允许插入重复值

 
USE AdventureWorks2012;   
GO  
ALTER TABLE Person.Password   
ADD CONSTRAINT AK_Password UNIQUE (PasswordHash, PasswordSalt);   
GO  


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"

--违反约束检查--



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

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

 

原文地址:https://www.cnblogs.com/Mratos/arcHive/2022/03/16/16012972.html

您可能感兴趣的文档:

--结束END--

本文标题: SQL SERVER 学习过程(二)

本文链接: https://www.lsjlt.com/news/9191.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • oracle怎么查询当前用户所有的表
    要查询当前用户拥有的所有表,可以使用以下 sql 命令:select * from user_tables; 如何查询当前用户拥有的所有表 要查询当前用户拥有的所有表,可以使...
    99+
    2024-05-14
    oracle
  • oracle怎么备份表中数据
    oracle 表数据备份的方法包括:导出数据 (exp):将表数据导出到外部文件。导入数据 (imp):将导出文件中的数据导入表中。用户管理的备份 (umr):允许用户控制备份和恢复过程...
    99+
    2024-05-14
    oracle
  • oracle怎么做到数据实时备份
    oracle 实时备份通过持续保持数据库和事务日志的副本来实现数据保护,提供快速恢复。实现机制主要包括归档重做日志和 asm 卷管理系统。它最小化数据丢失、加快恢复时间、消除手动备份任务...
    99+
    2024-05-14
    oracle 数据丢失
  • oracle怎么查询所有的表空间
    要查询 oracle 中的所有表空间,可以使用 sql 语句 "select tablespace_name from dba_tablespaces",其中 dba_tabl...
    99+
    2024-05-14
    oracle
  • oracle怎么创建新用户并赋予权限设置
    答案:要创建 oracle 新用户,请执行以下步骤:以具有 create user 权限的用户身份登录;在 sql*plus 窗口中输入 create user identified ...
    99+
    2024-05-14
    oracle
  • oracle怎么建立新用户
    在 oracle 数据库中创建用户的方法:使用 sql*plus 连接数据库;使用 create user 语法创建新用户;根据用户需要授予权限;注销并重新登录以使更改生效。 如何在 ...
    99+
    2024-05-14
    oracle
  • oracle怎么创建新用户并赋予权限密码
    本教程详细介绍了如何使用 oracle 创建一个新用户并授予其权限:创建新用户并设置密码。授予对特定表的读写权限。授予创建序列的权限。根据需要授予其他权限。 如何使用 Oracle 创...
    99+
    2024-05-14
    oracle
  • oracle怎么查询时间段内的数据记录表
    在 oracle 数据库中查询指定时间段内的数据记录表,可以使用 between 操作符,用于比较日期或时间的范围。语法:select * from table_name wh...
    99+
    2024-05-14
    oracle
  • oracle怎么查看表的分区
    问题:如何查看 oracle 表的分区?步骤:查询数据字典视图 all_tab_partitions,指定表名。结果显示分区名称、上边界值和下边界值。 如何查看 Oracle 表的分区...
    99+
    2024-05-14
    oracle
  • oracle怎么导入dump文件
    要导入 dump 文件,请先停止 oracle 服务,然后使用 impdp 命令。步骤包括:停止 oracle 数据库服务。导航到 oracle 数据泵工具目录。使用 impdp 命令导...
    99+
    2024-05-14
    oracle
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作