SQLserver总结(1)



在sqlserver中数据库的目录结构如下:


sql的目录结构如上图,他的系统数据库默认有4个
当需要用代码建表的时候需要新建查询


   
   
use master
go--go为结束语
--创建数据库
if exists(select * from sysdatabases where name = 'school')
drop database school
 
 
create database school
on(--主数据文件,只有一个
name = 'school',--逻辑名称
fileName = 'D:\sqlDataBase\ school.mdf',--路径
size = 5MB,--大小
fileGrowth = 1MB,--增长方式
maxsize = 100MB --限制最大的大小
),
(--次数据文件
name = 'school2',--逻辑名称
fileName = 'D:\sqlDataBase\ school2.mdf',--路径
size = 5MB,--大小
fileGrowth = 1MB,--增长方式
maxsize = 100MB --限制最大的大小
 
)
    
    
log on(--日志文件
name = 'schoollog',--逻辑名称
fileName = 'D:\sqlDataBase\ schoollog.ldf',--路径
size = 5MB,--大小
fileGrowth = 1MB --增长方式
)
表约束:
primary key  主键约束
foreign key 外 键约束
unique 唯一约束 
check 检查约束
default 默认约束 
identity(初始值,每次增长的值) 自增
not null  不为空

添加外部约束,便于删除
   
   
--如果存在就删除
if exists (select * from sysobjects where name='stduensspk')
alter table students drop constraint stduensspk
go
--添加外部主键约束
alter table students
add constraint stduensspk primary key(Id)
go
    
    
if exists (select * from sysobjects where name = 'nameul')
alter table students drop constraint nameul
go
if exists (select * from sysobjects where name = 'addressdf')
alter table students drop constraint addressdf
go
 
--添加外部unique,foreign key约束
alter table students
add
constraint nameul unique(s_Name),
constraint addressdf default '湖北武汉' for s_address
go


增加一条数据:
   
   
insert into students(name,age,id) values('hehe','14','10011001');
    
    
--插入多行 插入多行时default没用,多行插入时必须要赋值,如果主键为自增长,则多行插入的顺序会改变
insert into classes(name)
select 'c++'
union select 'c#'
union select 'c^^'

删除一条数据
   
   
--删除
delete from classes where Id > 123265;
truncate table students;
delete students;
 
修改一条数据   
   
   
--修改
update students set age = 10;
update students set age = 25 where stuName = 'hehe';
查询数据  (简单查询)
   
   
--查询
 
select * from students;

 
 
在sqlserver中数据库的目录结构如下:

sql的目录结构如上图,他的系统数据库默认有4个
当需要用代码建表的时候需要新建查询

    
    
use master
go--go为结束语
--创建数据库
if exists(select * from sysdatabases where name = 'school')
drop database school
 
 
create database school
on(--主数据文件,只有一个
name = 'school',--逻辑名称
fileName = 'D:\sqlDataBase\ school.mdf',--路径
size = 5MB,--大小
fileGrowth = 1MB,--增长方式
maxsize = 100MB --限制最大的大小
),
(--次数据文件
name = 'school2',--逻辑名称
fileName = 'D:\sqlDataBase\ school2.mdf',--路径
size = 5MB,--大小
fileGrowth = 1MB,--增长方式
maxsize = 100MB --限制最大的大小
 
)
     
     
log on(--日志文件
name = 'schoollog',--逻辑名称
fileName = 'D:\sqlDataBase\ schoollog.ldf',--路径
size = 5MB,--大小
fileGrowth = 1MB --增长方式
)
表约束:
primary key  主键约束
foreign key 外 键约束
unique 唯一约束 
check 检查约束
default 默认约束 
identity(初始值,每次增长的值) 自增
not null  不为空

添加外部约束,便于删除
    
    
--如果存在就删除
if exists (select * from sysobjects where name='stduensspk')
alter table students drop constraint stduensspk
go
--添加外部主键约束
alter table students
add constraint stduensspk primary key(Id)
go
     
     
if exists (select * from sysobjects where name = 'nameul')
alter table students drop constraint nameul
go
if exists (select * from sysobjects where name = 'addressdf')
alter table students drop constraint addressdf
go
 
--添加外部unique,foreign key约束
alter table students
add
constraint nameul unique(s_Name),
constraint addressdf default '湖北武汉' for s_address
go


增加一条数据:
    
    
insert into students(name,age,id) values('hehe','14','10011001');
     
     
--插入多行 插入多行时default没用,多行插入时必须要赋值,如果主键为自增长,则多行插入的顺序会改变
insert into classes(name)
select 'c++'
union select 'c#'
union select 'c^^'

删除一条数据
    
    
--删除
delete from classes where Id > 123265;
truncate table students;
delete students;
 
修改一条数据   
    
    
--修改
update students set age = 10;
update students set age = 25 where stuName = 'hehe';
查询数据  (简单查询)
    
    
--查询
 
select * from students;

 
 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

翅膀君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值