一、基本命令
-- 创建用户名和密码,用户名qiang,密码123456
create user qiang identified by 123456;
-- 创建表空间,表空间名字qiangspace,数据文件名qiangspace.dbf,大小10M
create tablespace qiangspace datafile '/u01/app/oracle/oradata/XE/qiangspace.dbf' size 10m;
-- 修改用户默认的表空间
ALTER USER qiang DEFAULT TABLESPACE qiangspace;
-- 修改创建用户默认的表空间
alter database default tablespace qiangspace;
-- 查看指定用户的默认表空间
select username,default_tablespace from dba_users where username = 'ZHU';
-- 给用户授权。
grant connect,resource,dba to qiang;
-- 查看数据库版本号
select * from v$version;
-- 查询oracle下的所有表空间
select * from dba_tablespaces;
-- 查询所有表空间的文件
select tablespace_name, file_name from dba_data_files;
-- 查看当前用户的表空间
select * from user_users;
select default_tablespace from user_users;
-- 修改用户密码
alter user qiang identified by 123456;
-- 查询当前用户的所有表
select table_name from tabs;
select table_name from user_tables;
select tname from tab;
-- 开启表空间自动扩展
alter database datafile '/u01/app/oracle/oradata/XE/qiangspace.dbf' autoextend on;
-- 关闭表空间自动扩展
alter database datafile '/u01/app/oracle/oradata/XE/qiangspace.dbf' autoextend off;
-- 表空间每次扩展10M无限制扩展
alter database datafile '/u01/app/oracle/oradata/XE/qiangspace.dbf' autoextend on next 10M maxsize unlimited;
-- 查看表空间文件
select name from v$datafile;
-- 查看所有用户
select * from all_users;
-- 创建表
create table employee(
id number primary key,
name varchar2(30),
address VARCHAR2(50)
)
-- 插入数据
insert into employee (id,name,address) values (1,'小强','马尔代夫');
-- 删除数据
delete from employee where id=2;
-- 修改数据
update employee set name='强崽',address='马尔代夫' where id=1;
-- 查询数据
select id,name,address from employee where id=1;
-- 清空表
truncate table employee;
-- 删除表
drop table employee;
-- 修改表所属的表空间
ALTER TABLE "QIANG"."ACCOUNT" MOVE TABLESPACE "QIANGSPACE"
-- 批量插入
insert all
into JOB_USER(id,username,password,phone,mail) values(1,'21','3','12','23')
into JOB_USER(id,username,password,phone,mail) values(2,'','','','')
select 1 from dual;
-- 插入数据
insert into JOB_CCGG (VALUE,CREATE_DATE,UPDATE_DATE) values ('asd',to_char(systimestamp, 'yyyy-mm-dd hh24:mi:ss.ff3'),to_char(systimestamp, 'yyyy-mm-dd hh24:mi:ss.ff3'));
commit;
-- 删除用户名
drop user 用户名;
-- 删除用户名下的所有表和视图
drop user 用户名 cascade;
-- 返回一个[0,1)的随机数
select dbms_random.value from dual;
-- 返回一个大于或等于1且小于50的随机数
select dbms_random.value(1,50) from dual
-- 返回的值可以使用TRUNC取整,即可实现随机产生某区间的整数
select trunc(dbms_random.value(1,50)) from dual;
-- 返回一个指定长度的随机字符串
select dbms_random.string(opt, length) from dual;
select dbms_random.string('A',20) from dual;
--------------------------------------------
opt可取值如下:
'u','U' : 大写字母
'l','L' : 小写字母
'a','A' : 大、小写字母
'x','X' : 数字、大写字母
'p','P' : 可打印字符
--------------------------------------------
二、常用命令
表信息
FLAG默认值:0
CREADED,UPDATED,DELETED默认值:SYSDATE
-- 创建主键自增
CREATE SEQUENCE TEST_SEQUENCE
INCREMENT BY 1
START WITH 1
NOMAXVALUE
NOCYCLE
NOCACHE
-- 创建触发器
CREATE TRIGGER TEST_TRIGGER BEFORE
INSERT ON TEST_USER FOR EACH ROW WHEN (NEW.ID IS NULL)
BEGIN
SELECT TEST_SEQUENCE.NEXTVAL INTO:NEW.ID FROM DUAL;
END;
-- 单条数据插入
INSERT INTO TEST_USER (USERNAME,PASSWORD,SEX,ADDRESS,PHONE,MAIL,PROFESSION,DEPOSIT) VALUES ('小强','8756***01','男','马尔代夫','189*****664','8756***01@qq.com','混泥土瞬间移动工程师',10000);
-- 批量数据插入
BEGIN
INSERT INTO TEST_USER (USERNAME,PASSWORD,SEX,ADDRESS,PHONE,MAIL,PROFESSION,DEPOSIT) VALUES ('小强','8756***01','男','马尔代夫','189*****664','8756***01@qq.com','混泥土瞬间移动工程师',10000);
INSERT INTO TEST_USER (USERNAME,PASSWORD,SEX,ADDRESS,PHONE,MAIL,PROFESSION,DEPOSIT) VALUES ('小强','8756***01','男','马尔代夫','189*****664','8756***01@qq.com','混泥土瞬间移动工程师',10000);
END;
INSERT ALL
INTO TEST_USER (USERNAME,PASSWORD,SEX,ADDRESS,PHONE,MAIL,PROFESSION,DEPOSIT) VALUES ('小强','8756***01','男','马尔代夫','189*****664','8756***01@qq.com','混泥土瞬间移动程师',10000)
INTO TEST_USER (USERNAME,PASSWORD,SEX,ADDRESS,PHONE,MAIL,PROFESSION,DEPOSIT) VALUES ('小强','8756***01','男','马尔代夫','189*****664','8756***01@qq.com','混泥土瞬间移动工程师',10000)
INTO TEST_USER (USERNAME,PASSWORD,SEX,ADDRESS,PHONE,MAIL,PROFESSION,DEPOSIT) VALUES ('小强','8756***01','男','马尔代夫','189*****664','8756***01@qq.com','混泥土瞬间移动工程师',10000)
INTO TEST_USER (USERNAME,PASSWORD,SEX,ADDRESS,PHONE,MAIL,PROFESSION,DEPOSIT) VALUES ('小强','8756***01','男','马尔代夫','189*****664','8756***01@qq.com','混泥土瞬间移动工程师',10000)
INTO TEST_USER (USERNAME,PASSWORD,SEX,ADDRESS,PHONE,MAIL,PROFESSION,DEPOSIT) VALUES ('小强','8756***01','男','马尔代夫','189*****664','8756***01@qq.com','混泥土瞬间移动工程师',10000)
SELECT 1 FROM DUAL;
-- 单条数据删除
DELETE FROM TEST_USER WHERE ID = 12
-- 批量数据删除
DELETE FROM TEST_USER WHERE ID = 10 OR ID = 11
DELETE FROM TEST_USER WHERE ID >= 5 AND ID <= 10
-- 单条数据修改
UPDATE TEST_USER SET USERNAME='阿强',PASSWORD='8756***02',SEX='男',ADDRESS='马尔代夫',PHONE='189*****123',MAIL='8756***01@pp.com',PROFESSION='混泥土瞬间移动工程师',DEPOSIT=30000,FLAG=1 WHERE ID = 17
-- 批量数据修改
UPDATE TEST_USER SET USERNAME='阿强',PASSWORD='8756***02',SEX='男',ADDRESS='马尔代夫',PHONE='189*****123',MAIL='8756***01@pp.com',PROFESSION='混泥土瞬间移动工程师',DEPOSIT=30000,FLAG=1 WHERE FLAG = 0
-- 查询所有
SELECT ID,USERNAME,PASSWORD,SEX,ADDRESS,PHONE,MAIL,PROFESSION,DEPOSIT,FLAG,CREADED,UPDATED,DELETED FROM TEST_USER
-- 条件查询
SELECT ID,USERNAME,PASSWORD,SEX,ADDRESS,PHONE,MAIL,PROFESSION,DEPOSIT,FLAG,CREADED,UPDATED,DELETED FROM TEST_USER WHERE ID = 22
-- 升序查询
SELECT ID,USERNAME,PASSWORD,SEX,ADDRESS,PHONE,MAIL,PROFESSION,DEPOSIT,FLAG,CREADED,UPDATED,DELETED FROM TEST_USER ORDER BY ID ASC
-- 降序查询
SELECT ID,USERNAME,PASSWORD,SEX,ADDRESS,PHONE,MAIL,PROFESSION,DEPOSIT,FLAG,CREADED,UPDATED,DELETED FROM TEST_USER ORDER BY ID DESC
-- 查询符合条件的第一条数据
SELECT ID,USERNAME,PASSWORD,SEX,ADDRESS,PHONE,MAIL,PROFESSION,DEPOSIT,FLAG,CREADED,UPDATED,DELETED FROM TEST_USER WHERE ROWNUM = 1
-- 查询符合条件的前五条数据
SELECT ID,USERNAME,PASSWORD,SEX,ADDRESS,PHONE,MAIL,PROFESSION,DEPOSIT,FLAG,CREADED,UPDATED,DELETED FROM TEST_USER WHERE ROWNUM < 6
-- 查询符合条件的前五条数据并降序排序
SELECT ID,USERNAME,PASSWORD,SEX,ADDRESS,PHONE,MAIL,PROFESSION,DEPOSIT,FLAG,CREADED,UPDATED,DELETED FROM TEST_USER WHERE ROWNUM < 6 ORDER BY ID DESC
-- 查询字段数据相同的个数
SELECT ID,COUNT(*) FROM JOB_USER GROUP BY ID HAVING COUNT(*) > 1
Oracle中分页使用rownum ,相对复杂一点。Oracle中rownum是虚拟列,是得出结果后,再进行计算的。所以,只能是小于的,无法大于,要使用的大于,就必须使用别名,如上代码中的rn就是使用别名的写法。
SELECT
*
FROM
( SELECT t.*, ROWNUM rn FROM ( SELECT * FROM USER; ) t )
WHERE
rn > ( currentPage - 1 ) * pageSize
AND rn <= currentPage * pageSize;
-- 分页查询
SELECT
ID,
USERNAME,
PASSWORD,
SEX
FROM
(
SELECT
T.ID,
T.USERNAME,
T.PASSWORD,
T.SEX,
ROWNUM RN
FROM
(
SELECT
ID,
USERNAME,
PASSWORD,
SEX
FROM
TEST_USER
) T
)
WHERE
RN > ( 1-1 ) * 10
AND RN <= 1 * 10;
三、服务器常用操作
# 连接数据库
sqlplus /nolog
# 登录数据库
conn system/oracle
conn username/password