sqlplus常用命令:
进入sqlplus模式:sqlplus /nolog 管理员登录: conn / as sysdba 登录本机的数据库 conn sys/123456 as sysdba // 用户名/密码 普通用户登录 conn scott/tiger@orcl 普通用户登录指定的数据库 conn scott/tiger 普通用户登录默认数据库 解锁用户: alter user scott account unlock; 锁定用户: alter user scott account lock; 注意:只有管理员才有权限解锁和锁定用户!!! 显示当前登录用户: show user; 查询Scott用户下emp表的数据 select * from emp;
DBA:数据库管理员 DB:database 数据库 DBMS:数据库管理系统 Oracle数据库的用户:
管理员: sys:超级管理员 system:普通管理员 普通用户: scott:系统提供的 创建用户:普通用户没有操作用户的权限【创建修改删除】 create user lxixi identified by 123456; 修改密码:必须在cmd下面修改,在plsql中没有效果 password lxixi然后根据提示修改就可以了 删除用户:drop user lxixi cascade; cascade 级联删除 授权:创建的用户任何权限【我们需要对用户授权】 角色: connect resource dba grant connect,resource,dba to lxixi 撤销权限: revoke connect,resourc,dba from lxixi; 创建表:
CREATE TABLE 表名( 字段1 字段类型, 字段2 字段类型, 字段3 字段类型, ..... )
列如:public class User{ private Integer id; private String name; ..... } 对表结构中的字段的修改
1.删除表 drop table t_student4; 2.添加字段 alter table t_student add sex char(3); 3.修改字段类型 alter table t_student modify sex varchar2(20) 4.修改字段名称 alter table t_student rename column sex1 to sex2; 5.删除字段 alter table t_student drop column sex2; 数据操作
INSERT(insert):添加 语法: insert into tableName(column1,column2,...,columnN)values(value1,value2,...,valueN) 列如:
向学生表中添加一条记录 insert into t_student(id,sname,age)values(11,'江立',25); commit; --提交 rollback;--回滚 UPDATE (update) :更新 语法:update tableName set column1=value1,column2=value2 ...[where columnN=valueN] 更新学生表中编号为1的学生的年龄为20 update t_student set age=20 where id=1; commit; DELETE (delete) :删除
语法: delete from 表名 [where 条件] 1.删除t_student10中的所有的数据 delete from t_student10 2.删除t_student中id为3的记录 delete from t_student where id=3; commit;
truncate:直接将数据从硬盘中删除,不会缓存,效率很高,但是没法撤销,慎用 truncate table t_student4 ; 单表查询
1.语法: select 字段列表 from 表名 [where 查询条件] [group by 分组] [having 分组条件] [order by 排序] 2.案例: 2.1 查询t_student表中的所有字段的所有数据 * 表示所有的字段 select * from t_student; 2.2 查询t_student表中所有学生的编号和姓名 select id,sname,id from t_student; 2.3 查询t_student表中所有记录的 id[编号] sname[姓名] age[年龄] select id as "编号",sname 学生姓名,age "【年龄】" from t_student --注意 别名中有特殊符号的时候 ""不能省略 2.4 给表取别名: select t_student.* from t_student select t.* from t_student t -- 给表取别名 select t.id,t.sname,t.age from t_student t 2.5 查询学生表中的id,sname,[id]sname. select id,sname,'['||id||']'||sname 信息 --'['+id+']'+sname ||等价于+ from t_student 带条件的查询 2.6 查询学生表中id为1的学生的所有信息 select * from t_student where id=1 2.7 查询学生表中班级编号为空的学生信息 select * from t_student -- where classid is not null 不为空 where classid is null -- 为空 2.8 查询学生表中年龄不等于20的学生的信息 select * from t_student where --age 20 --age != 20 age not in (20) 2.9 查询年龄在20到25之间学生同时班级id编号小于100的学生信息 select * from t_student where age between 20 and 25 and id < 100 2.10 查询出学生表中所有的姓江的学生信息 select * from t_student where --sname like '江%' -- '江%' 以江开头 '%江' 以江结尾 '%江%' 包含江 sname not like '%江%' 2.11 查询出学生表中姓江并且只有两个字符的学生的信息 select * from t_student where sname like '江__' -- 一个'_'表示一个占位符 2.12 查询出学生表中的所有的学生信息,并以学生年龄升序展现 select * from t_student order by age -- asc 升序 desc降序 默认升序 2.13 查询出学生表中的所有的学生信息,并以学生年龄升序展现,年龄相同的以id编号降序排序 select * from t_student order by age asc,id desc; 2.14 查询出学生表中不同的年龄有哪些 select distinct age,sex from t_student --distinct 去掉重复记录 统计函数
(若有group by,统计的是分组之后的数据) count:统计条数 select count(*) from t_student select count(id) from t_student select count(classid) from t_student -- 统计的是该列中非空的记录的个数 select count(1) from t_student; select id,sname,age,sex,classid,1,2,3 from t_student sum:求和 select sum(age) from t_student; min:取最小值 select min(age) from t_student; max:取最大值 select max(age) from t_student; avg:取平均值 select avg(age) from t_student; select sum(age),min(age),max(age),avg(age) from t_student;