/*
说明:若已经存在相应的用户和表空间,则需要先删除相应的用户和表空间
然后再全部重新建立
*/--删除用户dropuser test cascade;--删除表空间drop tablespace test_data_temp including contents and datafiles;drop tablespace test_data including contents and datafiles;--创建临时表空间createtemporary tablespace test_data_temp tempfile 'test_data_temp.dbf'size100m autoextend on;--创建表空间create tablespace test_data logging datafile 'test_data.dbf'size100m autoextend on;--创建用户并指定表空间createuser test
identified by test
default tablespace test_data
temporary tablespace test_data_temp
profile default;--给用户授予角色权限grantconnectto test;grant resource to test;--给用户授予系统权限grant unlimited tablespace to test;--给用户授予管理员权限grant dba to test;
exit;
帐号相关
--重置密码alteruser test identified by password;--锁定/解锁用户alteruser test account lock;alteruser test account unlock;
创建表及修改表相关信息
--删除表droptable t_persion;--创建表createtable t_persion(
pid varchar2(32) notnull,
pname varchar(32) notnull,
age number,
sex char(1)
);--修改表名altertable t_persion rename to t_persion2;--修改列名altertable t_persion rename column pid to ppid;--增改删表字段altertable t_persion add (asd number);altertable t_persion modify (asd char(1) default'0'notnull);altertable t_persion drop (asd);--增删主键约束altertable t_persion addconstraint pk_t_persion primarykey(pid);altertable t_persion dropconstraint pk_t_persion;--增删外键约束altertable t_persion addconstraint fk_t_persion foreignkey (sex) references t_sex(sid);altertable t_persion addconstraint fk_t_persion foreignkey (sex) references t_sex(sid) ONDELETECASCADE;--外键约束,级联删除altertable t_persion dropconstraint fk_t_persion;--添加表注释
COMMENT ON table t_persion IS '个人信息表';
--添加字段注释
comment on column t_persion.pid is 'id';
comment on column t_persion.pname is '姓名';
comment on column t_persion.age is '年龄';
comment on column t_persion.sex is '性别';
查询表相关信息
查询表名,表注释
-- 查询表名,表注释select rpad(t.TABLE_NAME,32),concat('--',t.COMMENTS)
from user_tab_comments t
where t.table_name like'T_ACCT_%';-- 查询表及字段的注释创建语句select'comment on table '||table_name||' is '||''''||comments||''';'from user_tab_comments t where t.table_name in ('TABLE_NAME')
unionallselect'comment on column '||table_name||'.'||column_name||' is '||''''||comments||''';'from user_col_comments t2 where t2.table_name in ('TABLE_NAME');
查询所有表的记录数
-- 查询所有表的记录数select t.table_name,t.num_rows from user_tables t orderby t.num_rows desc
-- 如果以上语句查询不到,则创建下面的方法,再执行最后的sqlcreateorreplace function count_rows(table_name in varchar2,
owner in varchar2 defaultnull)
return number authid current_userIS
num_rows number;
stmt varchar2(2000);
beginif owner isnullthen
stmt := 'select count(*) from "' || table_name || '"';
else
stmt := 'selectcount(*) from"' || owner || '"."' || table_name || '"';
end if;
execute immediate stmt
into num_rows;
return num_rows;
end;
select table_name, count_rows(table_name) nrows from user_tables;
查询含有大字段的表
-- 查询含有大字段的表select table_name from User_Tab_Columns
where data_type = 'CLOB'groupby table_name
orderby table_name
存储过程
无参+游标
--无参+游标createorreplaceprocedure demo1
ascursor cur_data is -- //(游标:一个可以遍历的结果集)
select * from t_persion t;beginfor cur_row in cur_data loop
dbms_output.put_line(cur_row.pid||':'||cur_row.pname);end loop;
exception
when others then
dbms_output.put_line(sqlcode);
dbms_output.put_line(sqlerrm);
end;insertinto t_persion values('1','张三',18,1);insertinto t_persion values('2','李四',17,0);call demo1();
入参
--入参createorreplaceprocedure demo2(
p_pid in test.t_persion.pid%type,
p_pname in varchar2,
p_age innumber,
p_sex inchar
)
isbegininsertinto t_persion(pid,pname,age,sex) values(p_pid,p_pname,p_age,p_sex);
exception
when others then
dbms_output.put_line(sqlcode);
dbms_output.put_line(sqlerrm);
end;call demo2('3','王五',18,0);
入参+出参
--入参+出参createorreplaceprocedure demo3(
p_pid in test.t_persion.pid%type,
p_pname out varchar2
)
isbeginselect pname into p_pname from t_persion t where t.pid = p_pid;delete t_persion t where t.pid = p_pid;
exception
when others then
dbms_output.put_line(sqlcode);
dbms_output.put_line(sqlerrm);
end;--在sqlplus中调用
var pname varchar2(32);
call demo3(3,:pname);
print :pname;
创建表空间与用户/* 说明:若已经存在相应的用户和表空间,则需要先删除相应的用户和表空间 然后再全部重新建立*/--删除用户drop user test cascade;--删除表空间drop tablespace test_data_temp including contents and datafiles;drop tablespace test...