SQL语句大全
求助编辑百科名片
本词条从基础知识、判断对象和应用技巧等方面,介绍了SQL(Structured Query Language)结构化查询语言的应用方法。
目录
展开
编辑本段基础
创建之前判断该数据库是否存在
if exists (select * from sysdatabases where name='databaseName')
drop database databaseName
go
Create DATABASE database-name
drop database dbname
--- 创建备份数据的 device
USE master
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'
--- 开始备份
BACKUP DATABASE pubs TO testBack
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
根据已有的表创建新表:
A:go
use 原数据库名
go
select * into 目的数据库名.dbo.目的表名 from 原表名(使用旧表创建新表)
B:create table tab_new as select col1,col2… from tab_old definition only
create sequence SIMON_SEQUENCE
minvalue 1 -- 最小值
maxvalue 999999999999999999999999999 -- 最大值
start with 1 -- 开始值
increment by 1 -- 每次加几
cache 20;
drop table tabname
Alter table tabname add column col type
Alter table tabname drop column colname
Alter table tabname add primary key(col)
说明:删除主键:Alter table tabname drop primary key(col)
create [unique] index idxname on tabname(col…。)
删除索引:drop index idxname on tabname
注:索引是不可更改的,想更改必须删除重新建。
create view viewname as select statement
删除视图:drop view viewname
(1) 数据记录筛选:
sql="select * from 数据表 where 字段名=字段值 order by 字段名 [desc]"
sql="select * from 数据表 where 字段名 like '%字段值%' order by 字段名 [desc]"
sql="select top 10 * from 数据表 where 字段名 order by 字段名 [desc]"
sql="select * from 数据表 where 字段名 in ('值1','值2','值3')"
sql="select * from 数据表 where 字段名 between 值1 and 值2"
(2) 更新数据记录:
sql="update 数据表 set 字段名=字段值 where 条件表达式"
sql="update 数据表 set 字段1=值1,字段2=值2 …… 字段n=值n where 条件表达式"
(3) 删除数据记录:
sql="delete from 数据表 where 条件表达式"
sql="delete from 数据表" (将数据表所有记录删除)
(4) 添加数据记录:
sql="insert into 数据表 (字段1,字段2,字段3 …) values (值1,值2,值3 …)"