SQL SERVER SQL

一、基础分类

查询:SELECT

DDL:CREATE,ALTER,DROP,RENAME,TRUNCATE

DML:INSERT,UPDATE,DELETE

DCL:GRANT, REVOKE

TC: 事物控制


1、事务
begin transaction;

rollback transaction/commit transaction;

2、事务点
save transaction savepoint_name
….
rollback to savepoint savepoint_name

3、权限控制
grant/revoke select on Pine.Agents to/from Elm

二、数据类型:
1、字符串:CHAR(8000),NCHAR(4000), VARCHAR(8000), NVARCHAR(4000), TEXT会淘汰

2、二进制:BINARY(8000), VARBINARY(8000),IMAGE

3、数值:  

精确值:tinyint(255),smallint(32767),int(2*10, 6) bigint(2,63)  ;  

           bit(0 or 1 );

           decimal(10,38), numeric(10,38);

           smallmoney(214748.3647),money

eg:
due number(6) ————-可保存最大值:999999  
due numberic(7,2)  ———可保存最大值99999.99

 

近似值:FLOAT, REAL

4、日期与时间: DATETIME, SMALLDATETIME

5、其他数据类型:CURSOR,SQL_VARIANT,TIMESTAMP,UNIQUEIDENYIFIER,XML
 

三、约束分类
约束类型:CHECK(ck)、外键(fk)、非空(nn)、主键(pk)、唯一性约束(un)
命名规范:{pk|fk}_<primary_key_table_name>_<foreign_key_table_name>

{nn|un|cc}_<table_name>_<columnname>

四、常用帮助

exec sp_help Customers;
exec sp_helpconstraint ‘Customers’
exec sp_columns Customers;可以从这些系统表获取相关信息:

sys.check_constraints
sys.databases
sys.default_constraints
sys.foreign_keys
sys.key_constraints 主键和唯一约束
sys.procedures
sys.servers
sys.sql_logins 在服务器上拥有账户的用户
sys.tables
sys.triggers
sys.views

重命名表:exec sp_rename ‘Tablename1′ ‘Tablename2′

以其他表为基础创建表:
select * 
into <new_tablename>
from <existing-tablename>

查询某个外键:

select
f.name as indexName,
t.name as indexTableName,
t2.name as referencedTableName
from sys.foreign_keys f
join sys.tables t
on f.parent_object_id=t.object_id

join sys.tables t2
on f.referenced_object_id=t2.object_id
where f.name=’FK1_DATA_INPT_MTHD’

发表评论

电子邮件地址不会被公开。 必填项已用*标注

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>