--创建文件夹
exec sp_configure 'show advanced options',1
go
reconfigure
exec sp_configure 'xp_cmdshell',1
go
reconfigure
go
exec xp_cmdshell 'mkdir D:\temp'--创建一个临时文件夹
go
--创建数据库
create database DB
on primary(
name='DB_date',--主文件逻辑名
filename='D:\temp\DB_date.mdf',--物理名称
size=5MB,--大小
maxsize=10MB,--最大值
filegrowth=15%--增长量
)--多加的副数据库在后面加括号,把mdf改成ndf
log on(
name='DB_log',--日志文件
filename='D:\temp\DB_log.ldf',
size=2MB,
filegrowth=10%
)--多加的日志文件,在括号后面继续加括号
go
--删除数据库
use master
go
if exists(select * from sysdatabases where name='DB')
drop database DB
go
--创建表
create table A(
id int not null
)
go
--删除表
if exists(select * from sysobjects where name='A')
drop table a
go
--创建约束
alter table A
add constraint pk_id primary key (id)
add constraint up_cid unique (cid)
add constraint df_address default ('地址不详') for address
add constraint ck_date check (date>='1992')
--主外键
alter table A--外表
add constraint fk_cid foreign key(cid) references B(id)--主表
--删除约束
alter table a
drop constraint pk_id
go
--局部变量
declare @name varchar(8)
declare @age int
--赋值
set @name='123'
select @name='123',@age=12
select @name=name from A
go
--打印
print @name
select @name
--常用全局变量
@@identity
@@error
go
--数据转换
convert(varchat(10),@name)
cast(@name as varchat(10))
go
--逻辑判断
if(@name=='123')
begin
end
else
begin
end
while(1==1)
begin
end
select 成绩=case
where result>60 then 'E'
else 'B'
end from A
--事物
begin transaction
commit transaction
rollback transaction
--视图
create view a
as
select * from a
--查看视图
select * form 视图名称
--删除视图
if exists(select * from sysobjects where name='a')
drop view a
--创建索引
create [unique][clustered|nonclustered] index index_name
on table_name (column_name[,column_name])
[with fillfactor=x]--x=30
--使用
select * from a
with(index=index_name)
where ...
--删除索引
drop index table_name.index_name
--创建储存过程
create proc proc_name
--[{@参数1 数据类型}[=默认值][output],{@参数2 数据类型}[=默认值][output]]
as
--sql语句
--删除储存过程
if exists(select * from sysobjects where name='proc_name')
drop proc proc_name
--调用
exec proc_name
--错误信息
raiserror('mis',16,1)
--orcl常用函数
日期处理
select sysdate as now,extract(year from sysdate)||'年'||extract(month from sysdate) from dual
select sysdate,to_char(sysdate,'YYYY-MM-DD HH:MI:SS') from dual
to_date('2011-2-2')
select name,decode(age,1,'ok',2,'no','or') from stu
nvl(name,'无名')--name是空就放回无名