SQL Plus操作指令:
用户操作:
show user -查看当前登录用户
connect sys/admin -登陆之后可用connect指令登录到其他用户
desc dba_users -查看数据字典
alter user scott account unlock -解锁scott用户
create user tom identified by 123456 default tablespace hjb_data; -创建用户并指定表空间
***************************************************************************************************************************************************************
表空间:(永久、临时、undo三种)
desc dba_tablespaces -查看系统表空间
desc user_tablespaces -查看用户表空间
desc dba_users
desc user_users
***************************************************************************************************************************************************************
表操作:
rename userinfo to new_userinfo; 修改表名
truncate table userinfo; 删除表中全部数据(比delete效率高),不删除表
drop table userinfo; 删除表,包括数据
字段操作:
alter table userinfo add username varchar2(500); 添加字段
alter table userinfo drop username varchar2(500); 删除字段
alter table userinfo rename username to new_username; 修改字段名称
alter table userinfo modify username varchar2(400); 修改字段 类型
***************************************************************************************************************************************************************
数据操作:
插入:
insert into userinfo (id,username,password,date) values (1,'tom','123',sysdate); -插入数据
复制:
create table userinfo as select * from oldtable;-创建表时把另一个表的所有数据复制过去,也可以只复制某些字段的数据
insert into userinfo select * from old_table; 插入数据时复制其他表中的数据
insert into userinfo(username,password) select username,password from old_table;复制数据时,前后类型要一致,名称可以不一样
修改:
update userinfo set id = 2,username = 'tom' where age = 30;有条件修改数据
删除:
delete from userinfo; 删除所有数据
delete from useinfo where age = 30;有条件删除数据
***************************************************************************************************************************************************************
约束:
非空约束:null / not null
主键约束:
alter table userinfo add constraint pk_id primary(id); -添加主键
alter table userinfo add constraint pk_id_name primary key (id,username); -添加联合主键
alter table userinfo rename constraint pk_id to pk_newid; -修改约束名字
alter table userinfo disable / enable / drop constraint pk_id; -禁用、启用、删除约束
alter table userinfo drop primary key; -删除约束
外键约束:
create table userinfo(
id number(8,0) primary key,
username varchar2(20),
cid references userinfo2(id)
);
create table userinfo(
id number(8,0) primary key,
username varchar2(20),
cid number(8,0),
constraint fk_cid foreign key(cid) references userinfo2(id)
);
create table userinfo(
id number(8,0) primary key,
username varchar2(20),
cid number(8,0),
constraint fk_cid foreign key(cid) references userinfo2(id) on delete cascade -级联删除,表示当主表被删除时,对应的从表也会被删除
);
alter table userinfo
add constraint fk_cid foreign key(cid) references userinfo2(id);
禁用、启用、删除同主键约束唯一约束:unique
add,drop,disable,enable
检查约束:
create table userinfo(
id number(8,0) primary key,
usename varchar2(20),
salary number(5,0) check(salary>0)
);
alter table test_demo add constraint check_sex check(sex>=0 and sex<=1);
查询语句:
在SQL PLUS中设置格式:
column column_name heading new_name; -设置结果显示的字段名(column可以简写为col)
col username heading 用户名;
col salary heading 工资;
col column_name format dataformat; -设置结果显示的格式
col username format a10; -a10表示设置字符长度为10位
col salary format 9999.9;表示设置数值类型的格式,一个9代表一位数字
col column_name clear; -清除掉原来设置的格式
简单查询:
select * from userinfo;
select username,salary from userinfo;
select username,salary+200 from userinfo;
select username from userinfo where salary>800 and salary <>1500.5;
select * from userinfo where username='aaa';
select * from userinfo where not(username='aaa');
select * from userinfo where username like '%a%';
select * from userinfo where username like '_a%';
select * from userinfo where salary between 800 and 2000;
select * from userinfo where salary not(between 800 and 2000);
select * from userinfo where salary in(800,2000,3000);
select * from userinfo where salary not in(800,2000,3000);
select * from userinfo order by id desc/asc;
select * from userinfo order by id desc,username asc;
select username,case username when 'aaa' then '计算机部门'
when 'bbb' then '市场部'
else '其他部门'
end as 部门
from userinfo;
select username,case when username= 'aaa' then '计算机部门'
when username= 'bbb' then '市场部'
else '其他部门'
end as 部门
from userinfo;
decode应用:同case when 语句
设置别名:
select id as 编号,username as 用户名,salary as 工资 from userinfo; -设置字段别名 (as可以省略)
select u.id,u.username,u.salary from userinfo u; -设置表别名