SQL Server 2012基本知识

1:图形化界面设置外键

解决:table->选中表->design->选中需要设置外键的字段->选择“关系”->选择"添加"->在表和列规范处选择右边省略号(见图一)->再选择相应关系。

图一:

\

2:常用命令

1)SQL Server 查询表的主键、外键和被谁引用的表: exec  sp_helpconstraint '表名'

2)GETDATE() 函数从 SQL Server 返回当前的时间和日期

   例:SELECT GETDATE() AS CurrentDateTime

year或yy  表示年

month或mm表示月

day或dd表示日

hour或hh表示小时

minute或mi表示分钟

second或ss表示秒

select getdate() as today
select dateadd(hour,-1,getdate()) as yesterday
select dateadd(day,1,getdate()) as tomorrow
select datepart(yy,getdate()) as year
select datepart(mm,getdate()) as month
select datepart(dd,getdate()) as day
select datepart(hh,getdate()) as hour
select datepart(mi,getdate()) as min
select datepart(ss,getdate()) as sec

 3)SQL 中的 substring 函数是用来抓出一个栏位资料中的其中一部分

SUBSTRING (str, pos, len)

由 <str> 中的第 <pos> 位置开始,选出接下去的 <len> 个字元。

假设我们有以下的表格:

Geography 表格

Region_Name Store_Name
East Boston
East New York
West Los Angeles
West San Diego

 

 

 

 

 

例1

SELECT SUBSTR (Store_Name, 3) 
FROM Geography 
WHERE Store_Name = 'Los Angeles';

结果:

's Angeles'

例2

SELECT SUBSTR (Store_Name, 2, 4) 
FROM Geography 
WHERE Store_Name = 'San Diego';

结果:

'an D'

3:check约束

alter table student
add constraint [CK_STUDENT_Balance]
check (Sage > 15 and Sage<45);

 

posted on 2016-11-13 11:01  IT小新手  阅读(485)  评论(0编辑  收藏  举报

导航