Oracle数据库的一些常用命令
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;
總結
以上是生活随笔為你收集整理的Oracle数据库的一些常用命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最全面详细讲解Oracle入门
- 下一篇: 使用Java方法实现 Double和St