SQL Server 入门学习总结---进阶篇

版权声明:本文为博主原创文章,转载需标明出处。 https://blog.csdn.net/My_heart_/article/details/64125218

本篇博客主要讲述数据库经常使用的一些约束、索引、删除语句以及视图,下面吧这些具体知识点一点一点讲述出来。


主题:

约束:NOT NULL 、 unique、primary key  、foreign key 、check 、default;

索引:创建与删除

删除:drop 、 truncate

向已有的表添加、修改或者删除列:alter table

视图:创建、更新、查看

----------------------------------------------------------------------------------

约束:NOT NULL 、 unique、primary key  、foreign key 、check 、default;

约束用于规定表中的数据规则。如果存在约束的数据行为,行为会被约束中止。

约束可以在创建表的时候规定,或者在表创建之后一定。


Not NULL ---表示某列不能存储NULL值,强制字段始终包含值

unique      ---保证某列的每一行必须只能有唯一值

primary key  ---主键pk,唯一标识数据库表的每条记录(每一个表的主键必须是唯一的,且不能为NULL)

                     --- PRIMARY KEY 约束拥有自动定义的 UNIQUE 约束

                     --- 每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束

foreign key  ---外键fk,保证一个表的数据 匹配另一个表中的值的参照完整性(一个表的fk指向另一个表的pk)

check     ---限制列中 值的范围

default   ---向列中设置默认值


--在表外部添加约束条件基本形式:
alter table table_name
add constraint 约束名;

--撤销约束
alter table  table_name
drop constraint 约束名;
--当然,外键要另当别论了
alter table table_name1
add constraint 约束名 foreign key(column_name)   references  table_name2(column_name);


具体使用

--给ID添加not null约束
  create table person
  (
	ID  int  not null ,     ---在创建表的时候添加not null   (SQL Server 只能在创建表的时候添加not null 约束)
	name char(5) 
  )

--创建unique约束 
create table person2_unique   --创建一个用来演示的表
  (
	ID  int   ,
	name char(5)
  )
--在表创建之后添加添加
  alter table person2_unique   --方式一:给ID 添加unique,不命名(系统会默认给出名字)
  add unique(ID)

  alter table  person2_unique
  add constraint uk_person2  unique(name)  --方式二:给name添加unique约束,命名为uk_person2
 
 
  --撤销unique约束
  alter table person2_unique
  drop constraint uk_person2;  --撤销约束名为uk_person2的约束


 --创建primary key
  create table person1_primary
  (
	ID  int not null,
	name char(5),
	primary key(ID)  --创建表的时候创建Pk
  )
  create table person2_primary
  (
	ID  int not null,
	name char(5),
  )
  alter table person2_primary
  add primary key (ID)


  alter table person2_primary --删除主键
  drop   PK__person2___3214EC2715502E78  --这个名字是系统自己起的,在表的 键属性可以找到

  alter table person2_primary  --pk命名为  pk_person2
  add  constraint pk_person2 primary key( ID)

--foreign key

  create table person1_foerign  --创建表的时候添加外键
  (
	ID  int  not null,
	name char(5),
	ID_F  int     foreign key(ID_F) references person1_primary(ID) ---方式一:未命名,创建外键指向person1_primary的ID列
	--方式二:命名了fk为 fk_person1
	--constraint  fk_person1   foreign key(ID_F)   references person1_primary(ID) 
  )

  create table person2_foreign  --创建表之后添加外键
  (
	ID int  not null,
	name char(5),
	ID_F int 
  )
  alter table person2_foreign
  add constraint fk_person2 foreign key(ID_F) references person1_primary(ID)

--check约束
  create table person1_check
  (
	ID int check(ID >0),  --在内部添加ID的约束条件:ID>0
	name char(5)
  )

  
  create table person2_check
  (
	ID int,
	name char(5)
  ) 

  alter table person2_check
  add constraint check_person check(ID >0);  --在外部添加条件

 --default约束
  create table person1_default
  (
	ID int  default(3),  --在内部添加ID的默认值3
	name char(5)
  )

  
  create table person2_default
  (
	ID int,
	name char(5)
  ) 
  alter table person2_default
  add constraint Df_person default(3)  for ID; --在外部给ID添加默认值3


----------------------------------------------------------------------------------

索引:创建与删除

在不读取整个表的情况下,索引使数据库应用程序可以更快地查找数据。


创建语法:

create index index_name    --如果要创建唯一索引,只需在index前  create之后添加unique
on table_name (column_name);

删除语法:

drop index index_name;


实例演示:

create index My_index
on  person(ID)  --给person 的ID列添加索引

----------------------------------------------------------------------------------

删除:drop 、 truncate

通过drop语句,可以 很轻松的删除索引、表和数据库


drop index index_name;  --删除索引

drop table table_name;   -- 删除表

drop database  database_name;  --删除数据库



--仅仅删除表中数据

truncate table table_name;


truncate、delete、drop的区别与联系

----------------------------------------------------------------------------------

向已有的表添加、修改或者删除列:alter table


在表中添加列:

alter table table_name
add column_name  datatype

删除表中列的数据:

alter table  table_name
drop column  column_name 

改变表中列的数据类型:

alter table table_name
alter column column_name  new_datatype


-----添加一个小知识点:

auto increment字段  会在新记录插入表中时生成一个唯一的数字。

SQL 使用identity 关键字来执行auto increment任务,默认地identity从1开始,每次递增1 

使用语法

  column_name   identity (初始值,递增值);

----------------------------------------------------------------------------------

视图:创建、更新、查看

视图是基于SQL查询结果集的可视化的表。


创建

CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
注意: 视图总是显示最新的数据!每当用户查询视图时,数据库引擎通过使用视图的 SQL 语句重建数据


更新视图

update [view_name] set column_name = new_number  
where  条件


使用实例

 --view 创建
  create view  my_view as
  select * from person
  where ID >3 and ID <10

  --查询
  select * from [my_view];

  --更新
  update  [my_view]
  set name = '1357' 
  where ID =5;




猜你喜欢

转载自blog.csdn.net/My_heart_/article/details/64125218