您的位置:首页 > 数据库 > Oracle

Oracle 入门基础,Oracle常用Sql语句

2013-06-01 16:05 591 查看
1、创建表
create table test01
(
id number(10,0),
age number(10,0),
name varchar2(30)
)
2、查询表结构
desc test01;
3、更改表的名称
alter table test01 rename to test02;
4、给表添加注释
comment on table test01 is '测试表';
5、检查表的注释是否添加成功
select table_name,table_type,comments from user_tab_comments where table_name='TEST01';
6、给表中的ID添加注释
comment on column test01.id is '编号';
7、检查表中ID列的注释是否添加成功
select * from user_col_comments where table_name='TEST01';
8、查询表中各字段类型
select column_name,data_type from user_tab_columns where table_name='TEST01';
9、删除表
drop table test01;
10、查询表中所有数据
select * from test01;
11、清空表中所有数据
truncate table test01;
12、给表添加主键约束
alter table test01 add constraint pk_id primary key(id);
13、删除表中主键约束
alter table test01 drop primary key;
14、给表添加唯一约束
alter table test01 add constraint uk_age unique(age);
15、删除表中唯一约束
alter table test01 drop constraint uk_age;
16、给表添加外键约束
alter table test01 add constraint fk_name foreign key(name) references test02(name);
17、删除表中外键约束
alter table test01 drop constraint fk_name;
18、给表添加check约束
alter table test01 add constraint chk_name check(length(id)<=6);
19、删除表中check约束
alter table test01 drop constraint chk_name;
20、向表中插入一条数据
insert into test01 (id,age,name) values (1,21,'admin');
21、向表中循环插入多条数据语句
declare
i NUMBER;
begin
for i in 1..1000 loop
INSERT INTO test01(id,age,name) VALUES (i,i,'admin'||TO_CHAR(i));
end LOOP;
END;
22、查询表中一共有多少条数据
select count(*) from test01;
23、删除一行数据(表中ID为1的数据)
delete from test01 where id=1;
24、删除前N行数据(表中ID小于100的数据)
delete from test01 where id<100;
25、给表添加一列
alter table test01 add sex varchar2(2);
26、删除表的一列
alter table test01 drop column sex;
27、修改表的列名
alter table test01 rename column sex to sex01
28、修改列的类型
alter table test01 modify sex01 varchar2(20);
29、更新表中一列的值
update test01 set sex='男';
30、对表中数据进行排序(降序desc,升序asc)
select * from test01 order by id asc;
31、删除表中重复的数据(本条数据是根据ID是否重复而删除)
delete test01 where test01.rowid not in (select min(test01.rowid)from test01 group by test01.id);
32、给表添加多列
declare
v_sql varchar2(2000);
V_Col Varchar2(20);
begin for i in 1..10
Loop
V_Col:='列'||i;
v_sql:='alter table test01 add '||v_col||' varchar2(30)';
execute immediate v_sql;
end loop;
end;
33、给test01表增加备份表
create table test000000 as (select * from test01);
34、如何使查出来的结果自动生成序号
select rownum,name from test01;
35、查询本数据库有哪些用户正在使用
select username from V$session
36、给test01表增加备份表
create table test02 as (select * from test01);
37、如何使查出来的结果自动生成序号
select rownum,name from test01;
38、查询test01表在哪个空间内
select tablespace_name from user_tables where table_name='TEST01';
39、怎样查得数据库的sid
select name from v$database;
40、查询当前用户对象
select * from user_objects;
41、查看当前时间
select to_char(sysdate,'yyyy-mm-dd,hh24:mi:ss') from dual;
42、如何查询数据库有多少个表
select * from v$instance;
43、查询当前用户所拥有的权限
select * from dba_sys_privs;
44、从外表取出数据插入本表中(本条语句为从test02表中取出id,age数据插入test01表中)
insert into test01(id)select id from test02;
45、查询出本表与外表相同的数据(本查询查询出id在两表中相同的数据)
select * from test01 where id in (select id from test02);
46、查询表在哪个空间内
select tablespace_name from user_tables where table_name='TEST01';
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: