约束-保证数据完整性

create database 库名
on primary( , )
log on(           )

create table 表名

id int identity(1,1) primary key,
gender bit default(1),
birthday datetime

insert into 表名(gender,birthday)
values(0,'1990-10-10')

set identity_insert 表名 on/off  --s设置自增长的id

常量 name    N'张三'

update表名 set name=‘张红’where name='张三'


————————————————————————————————————————————————————————————————-————————————

设置的问题
1、设置文件夹权限
 文件夹(数据表)右单击---属性---权限---搜索---对象类型---浏览
2、生成脚本
右击数据库--任务---生成脚本
3、数据库备份与还原
右击数据库--任务---备份/还原

-- 备份数据:讲一个表中的数据备份到另一个表中
--将TblTeacher表中的数据备份到另外一个表TblTeacher表中表中,前提是

TblTeacher表中不存在,如果它存在就会报错
select * from TblTeacher
select * into NewTblTeacher from TblTeacher


命令实现
4、数据表的复制
SQL:复制数据库某一个表到另一个数据库中
SELECT * INTO 表1 FROM 表2 --复制表2如果只复制结构而不复制内容或只复制某一列只要加WHERE条件就好了
例子:SELECT * INTO [IMCDB].[dbo].[SysLog] FROM [AimManageDB].[dbo].[SysLog]

(将数据库AimManageDB中的SysLog表复制到数据库IMCDB中)

 新增方法:跨服务器复制表

select * INTO [SMSDB].[dbo].[SysLog] FROM openrowset('sqloledb',‘目标服务器’;'账号';'密码',[SMSDB].[dbo].[SysLog])

(将数据库目标服务器中的SysLog表复制本地的数据库SMSDB中)

eg:如果出现以下错误:

(SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服务器安全配置的一部分而被关闭。系统管理员可以通过使用 sp_configure 启用 'Ad Hoc Distributed Queries'。有关启用 'Ad Hoc Distributed Queries' 的详细信息,请参阅 SQL Server 联机丛书中的 "外围应用配置器"。)

解决方法:

启用Ad Hoc Distributed Queries: exec sp_configure 'show advanced options',1 reconfigure exec sp_configure 'Ad Hoc Distributed Queries',1 reconfigure

使用完成后,关闭Ad Hoc Distributed Queries:exec sp_configure 'Ad Hoc Distributed Queries',0 reconfigure exec sp_configure 'show advanced options',0 reconfigure

其他导入方法
select * from table1 into table2      table2必须不存在
insert into table2 select * from table1          table2必须存在

5、一次插入多条数据
insert into TblTeacher
select 'Chris',1,20,1500,'1999-9-9' union
select 'James',1,20,1600,'1999-10-11' union
select 'Tom',1,20,1700,'1999-9-20' union
select 'Marry',0,20,1500,'1999-9-9'
一次插入一条数据
insert into TblScore(tSId,tEnglish,tMath)
values(11,86,94);

————————————————————————————————————————————————————————————————————————-----

create table Class
(
sClassID int identity(1,1) primary key,
sName nvarchar(50) not null,
sDacr nvarchar(1000)
)

身份证为 varchar
出生年月 datatime

性别       nchar

 

select * from 表名

更新
--把表中TblTeacher表中所有年龄为19岁得姓名两边加上★,性别设为女,工资都叫上500
--这里能否使用+=:TblTeacher+=500可以;但是不推荐这个只有2008版本才能用。
update TblTeacher set tTName='★'+tTName+'★',tTGender=0,tTSalary=tTSalary+500 where tTAge=19
--将年龄为19,并性别为女的人的姓名两边叫上两个☆

update TblTeacher set tTName='☆'+tTNmae+'☆' where tTAge=19 and tTGender=0
--&&,||.or
--把name中的★替换为☆
update TblTeacher set tTName=replace(tTName,'★','☆') where tTAge=19 and tTGender=0

增删改查:
insert
delete
update
select

逻辑运算符的优先级别问题 not>and>or

--delect from 表名  where 逻辑表达式,truncate table 表名
--区别:
1.delete不便使自动编号返回为起始值。但是truncate能是自动增长的列的值返回默认的种子。
2.truncate只能一次清空,不能按条件删除。但是delete可以按条件清除部分记录。
3.truncate清空数据表性能(速度)比delete快的多的多
相同点:
truncate table 表名;delect from 表名 --删除表中的数据不删除表结构
drop table 表名 --删除表中的数据同时删除表结构


考试题偏难,所有的成绩加5分
updata Score set english=english+5,nath=math+5
--分情况
updata Score set english=100 where english+5>=100
updata Score set math=100 where math+5>=100

updata Score
set english
(
case when english+5>=100 then english+5
when english+5>=100 then 100
end
),
math=
(
case when math+5>=100 then math+5
when math+5>=100 then 100
end
)


约束-保证数据完整性
create database Test
create table Employee
(
EmpId int identity(1,1) primary key,
EmpName nvatchat(20) not null,
EmpGander nchar(2),--性别
EmpAge int,
EmpEmail varchar(20),
EmpAddress nvarchar(50)
)
go
----------------------------------
create table Department
(
DepId int identity(1,1) primary key,
DepName nvatchat(20) not null,
)
go
-----------------------------------
1、在 设计界面 右击添加 check约束
EmpGender='男'or EmpGender='女'

2、在 设计界面 右击添加 唯一键---------唯一约束

3、设置默认值:
在 设计界面 单击要设置默认值得列名,看下面的列属性“默认值或绑定”改成默认值
4、非空约束------列属性“允许null值”  是/否
5、主键约束------在 设计界面 右击设为主键

6、关系约束
在 设计界面 右击添加关系
在设置关系约束的时候,有一个叫主键,一个叫外键,他们是分别位于2个表中,表一中的主键和表二中的外键发生关系,是一对多的关系,表一中的主键必须是设置为主键的!(和唯一键不一样!!)
关系设为“级联”时两个表中的有关系的列都删除

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值