mssql之从头再来

来源:互联网 发布:怎么寻找淘宝达人 编辑:程序博客网 时间:2024/06/10 19:20
去了baidu,每周都得到数据库以及C#这一块的专家,便自以为.....才来到csdn两天,才明白自己是多么的.....,好在我觉悟了,从头再来!

if exists(select * from master..sysdatabases where name='mydb')
-- if (select db_id('mydb')  )is null
print '数据库已经存在'
else
begin
create database mydb
end


use mydb

--if exists(select 1 from sysobjects where name='test')
create table test
(
id int primary key identity(1,1),
name varchar(30) not null unique,
age tinyint,
notetime smalldatetime
)

--为了多运用,比如说notetime smalldatetime default getdate(),但是为了累积自己的知识,因此创建约束以及默认值

--约束阿,默认值等都是对象,检验他是否存在和表一样
create default GetCurrentTime as getdate()
--如果要修改,那么直接alter default getcurrenttime as '2000-01-11'
sp_bindefault getcurrenttime,'test.notetime'

create rule checkage as  @age<=120

sp_bindrule checkage,'test.age'



insert into test values ('张三',15,getdate())
insert into test (name,age)values ('李四',15)


insert into test (name,age)values ('王五',19)
insert into test values ('赵六',40,'2001-4-6')


insert into test (name,age)values ('小二',50)
insert into test values ('老三',70,getdate())


select * from test

1    张三    15    2008-02-05 14:39:00
2    李四    15    2008-02-05 14:39:00
3    王五    19    2008-02-05 14:39:00
4    赵六    40    2001-04-06 00:00:00
5    小二    50    2008-02-05 14:39:00
6    老三    70    2008-02-05 14:39:00


select * from test


select * from test where id=1

select * from test where id between 3 and 5

select top 5 * from test except select top  2* from test

select * from test where id=1 or id=2

select * from test where in in (1,2,3)

select name,age from test where name like '%二%'

select * from test where age !=10 order by  name,age desc


select * from test where notetime between '2001-04-06 00:00:00' and '2008-01-05 14:39:00'

select month(notetime) from test
--day,year都是一样的,如果要获取月日的话,那么要先转换成文本类型,否则会被识别为数字



alter table test add sex bit default 0

select * from test

update test set sex=0


update test set age=age+3 where age=15

select * from test


select name,min(id)as 'id' from test group by name

delete test where id in(
select min(id) from test group by age having count(age)>1)

--drop table test



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝账号处于下单保护状态怎么办 淘宝卖家物流单号写错了怎么办 有个人给我发直播消息怎么办 网贷申请多了现在秒拒怎么办 顺丰快递寄的瓜果坏了怎么办 淘宝退货快递公司填错了怎么办 不小心把淘宝账号注销了怎么办 腾讯视频会员开通一个月贵怎么办 微交易买美国指数输了四千块怎么办 淘宝地址中包含了违禁词怎么办 微信支付失败但是钱扣了怎么办 支付宝向别人收款交易关闭了怎么办 从淘宝充的晋江币充值异常怎么办 接手转让店铺会员要求退卡怎么办 转转买手机卖家拒绝退款怎么办 淘宝买的东西电话号码留错了怎么办 平板电脑没电关机没保存文件怎么办 恢复出厂设置需要谷歌账号怎么办 华为手机云端里照片删除了怎么办 客户退货卖家一直没收到货怎么办 在淘宝买到假货投诉不管用怎么办 差评不接电话不回旺旺不要钱怎么办 饿了么同行恶意差评怎么办 苹果手机更新后淘宝用不了怎么办 淘宝网快递丢件了买家怎么办 评价后忘了截图五星好评怎么办 在淘宝被骗了好评返现怎么办 苹果4s微信版本过低怎么办 微信版本太低无法登录怎么办 苹果4微信版本低登录不了怎么办 安卓手机微信版本低登录不了怎么办 苹果微信版本低登录不了怎么办 安装包与当前版本不兼容怎么办 游戏与苹果手机系统不兼容怎么办 闲鱼买家申请退款不退货怎么办 在淘宝买的战地1登录不了怎么办 支付宝转账到注销的账号怎么办 支付宝用户支付密码被锁定怎么办 淘宝提现需要的手机宝令怎么办 手机换号码了支付宝账号怎么办 卖家收到货迟迟不给退款怎么办