自己总结一些操作数据库的方法

1、拷贝一个新表

create table test2 as (select * from test where 1=2);

2、建立新表,并把数据拷贝入新表中(适应SqlServer数据库)

select * into book2 from book;

 

3、自动增长列

--SqlServer

create table student(
  --自动增长列
  id int identity(1,1),
  name varchar(50)
);

--Oracle

--序列
create sequence seq_stu
increment by 1
start with 1
/
--触发器
create trigger tri_stu
before insert on student
referencing
new as new_value
for each row
begin
  select seq_stu.NEXTVAL
  into :new_value.stuId
  from dual;
end;
/

4、更改列

--增加列
alter table stu2
add stuName varchar(20);
--删除列
alter table stu2
drop column stuName;
--修改列
alter table stu2
alter column stuName varchar(50);

5、函数 Null值判读

--如果是Null则是0 (SQL Server / MS Access)
select isNull(stuAge,0) from student;
--Oracle NVL()
select NVL(stuAge,0) from student;
--MySql ifNull()
select ifNull(stuAge,0) from student;

6、行号

Oracle:     select RowNum,id from student;

Sql Server: select row_number()over(order by id) as RowNum,id from student;

 

 

posted on 2013-12-10 14:52  wcq  阅读(221)  评论(0编辑  收藏  举报

导航