PLSQL 常用语

首先,

#创建(新)用户:create user username identified by password;username:新用户名的用户名
password: 新用户的密码
也可以不创建新用户,而仍然用以前的用户,如:继续用scott用户

#创建表空间:
create tablespace tablespacename datafile 'd:\data.dbf' size xxxm;
tablespacename:表空间的名字
d:\data.dbf':表空间的存储位置
xxx表空间的大小,m单位为兆(M)

#将空间分配给用户:
alert user username default tablespace tablespacename;
将名字为tablespacename的表空间分配给username

#给用户授权:
grant create session,create table,unlimited tablespace to username;

#用户登录:登录之后创建表即可。

conn username/password;

#展示所有表:

select * from all_Tables ;

all_tables显示用户有权限看到的所有的表,包括系统表
dba_tables是管理员可以看到的数据库中所有的表
user_tables是当前用户的所有表,用这个可以缩小查找的范围
如果知道表所在的用户可以
select * from all_tables where owner='用户名';
如果知道字段的名称可以
select * from all_tab_columns where owner='用户' and column_name='字段名'
如果用户、表、字段的名称都不知道,最好找下开发人员或者售后支持。

#显示当前的数据库名称:

select name from v$database;

#查看所有的表名称:

 Select   table_name   From   user_tables;

 Select   table_name   From   user_all_tables;

#查看user表中排序的前5条记录:

select   *   from   users   where   rownum <=5   order   by   userid   desc;

#查看user表中排序后的前5条记录:

select   *   from   (select   *   from   users   order   by   userid   desc)   where   rownum <=5; 

#查询表中line_no为p12的并只显示前10行并倒序展示:

select * from(select * from yb.ti_les_inner_sheet where line_no = 'P12' order by ti_les_inner_sheet_id desc) where rownum <=10 ;

#创建表

create table test01(id int not null,name varchar(8) not null,gender varchar2(2) not null,age int not null,address varchar2(20) default '地址不详' not null,regdata date);

#修改表

alter table test01 add constraint s_id primary key;

alter table test01 add constraint CK_INFOS_GENDER check(gender=’男’ or gender=’女’)

alter table test01 add constraint CK_INFOS_AGE(age>=0 and age<=50)

alter table 表名 modify 字段名 default 默认值; //更改字段类型

alter table 表名 add 列名 字段类型; //增加字段类型

alter table 表名 drop column 字段名; //删除字段名

alter table 表名 rename column 列名 to 列名 //修改字段名

rename 表名 to 表名 //修改表名

#删除表

truncate table 表名 //删除表中的所有数据,速度比delete快很多,截断表

delete from table 条件//

drop table 表名 //删除表

#插入语句

insert into 表名(值1,值2) values(值1,值2);

#修改语句

update 表名 set 字段=值 [修改条件]

查询语句

带条件的查询

where

模糊查询

like % _

范围查询

in

对查询结果进行排序

oeder by desc||asc

case…when

select username,case username when ‘aaa’ then ‘计算机部门’ when ‘bbb’ then ‘市场部门’ else ‘其他部门’ end as 部门 from users;

select username,case username=’aaa’ then ‘计算机部门’ when username=’bbb’ then ‘市场部门’ else ‘其他部门’ as 部门 from users;

#运算符和表达符

算数运算符和比较运算符

distinct 去除多余的行

column 可以为字段设置别名 比如 column column_name heading new_name

decode 函数的使用 类似于case…when

select username,decode(username,’aaa’,’计算机部门’,’bbb’,’市场部门’,’其他’) as 部门 from users;

#复制表

create table 表名 as 一个查询结果 //复制查询结果

insert into 表名 值 一个查询结果 //添加时查询

#查看表信息

desc test01;

#创建表空间

永久表空间

create tablespace test1_tablespace datafile ‘testfile.dbf’ size 10m;

临时表空间

create temporary tablespace temptest1_tablespace tempfile ‘tempfile.dbf’ size 10m;

desc dba_data_files;

select file_name from dba_data_files where tablespace_name=’TEST1_TABLESPACE’;

#约束

非空约束 |主键约束|外键约束|唯一约束|检查约束

not null |primary key|unique|check|

联合主键 constraint pk_id_username primary key(id,username);

查看数据字典 desc user_constraint

修改表时重命名 rename constraint a to b;

修改表删除约束

禁用约束 disable constraint 约束名字;

删除约束 drop constraint 约束名字; drop primary key;直接删除主键

外键约束

create table typeinfo(typeid varchar2(20) primary key, typename varchar2(20));

create table userinfo_f( id varchar2(10) primary key,username varchar2(20),typeid_new varchar2(10) references typeinfo(typeid));

insert into typeinfo values(1,1);

创建表时设置外键约束

constraint 名字 foregin

create table userinfo_f2 (id varchar2(20) primary key,username varchar2(20),typeid_new varchar2(10),constraint fk_typeid_new foreign key(typeid_new) references typeinfo(typeid));

create table userinfo_f3 (id varchar2(20) primary key,username varchar2(20),typeid_new varchar2(10),constraint fk_typeid_new1 foreign key(typeid_new) references typeinfo(typeid) on delete cascade);

外键约束包含

删除外键约束

禁用约束 disable constraint 约束名字;

删除约束 drop constraint 约束名字;

唯一约束 与主键区别 唯一约束可以有多个,只能有一个null

create table userinfo_u( id varchar2(20) primary key,username varchar2(20) unique,userpwd varchar2(20));

创建表时添加约束

constraint 约束名字 unique(列名);

修改表时添加唯一约束 add constraint 约束名字 unique(列名);

检查约束

create table userinfo_c( id varchar2(20) primary key,username varchar2(20), salary number(5,0) check(salary>50));

constraint ck_salary check(salary>50);

/* 获取表:*/

select table_name from user_tables; //当前用户的表

select table_name from all_tables; //所有用户的表

select table_name from dba_tables; //包括系统

select table_name from dba_tables where owner=’zfxfzb’

/*

猜你喜欢

转载自www.cnblogs.com/luanyulin/p/10764349.html