SQL常用语句收集



 

 1  
    添加外键:
    alter table 表名1
    add constraint  外键名
    foreign key (外键字段)
    references  表名2(外键字段)
  
 2 添加索引(此键一定是汪加了not null后):
    create unique index 约束名
    on 表名(约束字段 desc)
   
 3 添加not null约束 :
   alter table 表名
   alter column  约束列名 类型(大小)  not null
  
 4 添加primary key 约束 :
   alter table  表名
   add constraint  约束名 primary  key  (约束列名1,约束列名2.....)
  
 5 删除整个列:
    alter table 表名
    drop column 列名
   
 6 添加一新列:
   alter table 表名
   add 新列名 类型(大小)
  
 7 删除某表:
    drop table 表名
   
 8 修改表中的字段名:
    exec sp_rename '表名.[旧列名]','新列名','column'
   
 9 主键,自动+1 
   topic_id smallint primary key identity(1,1),--主键,自动+1    
  
 10 dateadd()的使用
   select StuId,dateadd(month,10,getdate()) as [newDate]
   from TableName
  
 11 查询中使用当前系统时间
  select StuId,getDate() as [theDate]
  from TableName
 
 12查询中使用表达式
  declare @thedate datetime
  select @thedate=getdate()
  if year(@thedate)>2000
  begin
  select StuId,dateadd(day,10,getdate()) as [theYear]
  from Test
  end
  else
  print '没有你想要的数据'
 
13条件表达式:case 的使用
   -- 一个简单的试列
  declare @theName varchar(20)
  select @theName=
  case 'jiajia'
  when 'aa' then 'myaa'
  when 'bb'then 'mybb'
  when 'cc'then 'mycc'
  when 'jiajia' then 'myjiajia'
  else '没有此人'
  end
  select @theName

  --下面这种方法是从表中查询
  declare @herName varchar(20)
  select @herName=case StuName
    when '巧巧' then  'qiaoqiao' end
  from Test
  select @herName as [StuName]
  --上面试列也可以这样写,效果是一样的:注:此种方法可
  --用于link通配符查询 eq: when xxx link 'abc%' then......
  declare @herName varchar(20)
  select @herName= case
    when  StuName='巧巧' then  'qiaoqiao' end
  from Test
  select @herName as [StuName]

14   处理  null  值的函数
     nullif(a,b) 如果函数中两个表达式的值一样,nullif就会返回null ,
     如果表达式不等,就返回第一个表达式的值
    
     isnull(a,b) 这个函数会检查a,如果它的值是null,则为函数返回b,如果它
     的值不为null,函数就返回a
    

    
15 convert()用法:105为指定的显示日期的类型
     declare @thedate varchar(20)
  select @thedate=convert(varchar,getdate(),105)
  select @thedate as [thedate]
  结果为:28-05-2008

  declare @thedate varchar(30)
  select @thedate=convert(varchar(30),$12345678.98770,1)
  select @thedate as [themoney]
  结果为:12,345,678.99

16   datepart(datepart,date)与 datename(datepart,date)用法
      select datepart(year,getdate()) as myYear 结果:2008
      select datepart(month,getdate()) as myYear 结果:5
     
      select datename(month,getdate()) as myYear 结果:05
      select datename(year,getdate()) as myYear 结果:2008
     

17  --修改存储过程

if exists(select * from  sysobjects where id=object_id('pr_mypro') and objectproperty(id,'IsProcedure')=1)
drop procedure pr_mypro
Go

create proc pr_mypro
  @theId int
as
  select * from tablename
  where _id=@theId
Go

--修改存储过程
 alter proc pr_mypro
        @theId varchar
 as
   select * from tablename
   where _id=@theId
 Go


18  --使用临时表    

create database mydatabase

use mydatabase

create table mytable
(
 _id int identity primary key,
 fname varchar(20),
 lname varchar(20),
 sex char(2)
)

insert into mytable
values
(
  'wan',
'tingqiang',
'1'
)

insert into mytable
values
(
 'xue',
'yong',
'0'
)

select * from mytable

create table Temptable--创建临时表
(
_id int identity primary key,
fname varchar(20),
lname varchar(20),
sex char(2)
)

insert into Temptable select fname,lname,sex from mytable--为临时表填充数据

drop table mytable  --删除现有表

create table mytable  --创建少一个字段的表
(
   _id int identity primary key,
   fname varchar(20),
   lname varchar(20)
)


insert into mytable select fname,lname from Temptable --将临时表里的内容
--放入新建表。。

drop table Temptable--删除临时表。。。

select * from mytable --查询少了个字段的表


   

    19  左连接,右连接,完全连接的区别:
     LEFT    JOIN    或    LEFT    OUTER    JOIN。    
     左向外联接的结果集包括    LEFT    OUTER    子句中指定的左表的所有行,而不仅
     仅是联接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右
     表的所有选择列表列均为空值。  
   
    RIGHT    JOIN    或    RIGHT    OUTER    JOIN。    
    右向外联接是左向外联接的反向联接。将返回右表的所有行。如果右表的某行在左表中
    没有匹配行,则将为左表返回空值。  
   
    FULL    JOIN    或    FULL    OUTER    JOIN。    
    完整外部联接返回左表和右表中的所有行。当某行在另一个表中没有匹配行时,则另一
   个表的选择列表列包含空值。如果表之间有匹配行,则整个结果集行包含基表的数据值。

         补充进行中。。。。。。。
         希望各位指点。。。。。。
    

 

     


 

原文地址:https://www.cnblogs.com/wantingqiang/p/1209101.html