一、SQL语句
1. 环境配置与准备
- linux端oracle用户打开监听
//查看监听状态与开始监听 lsnrctl status lsnrctl start
- 开启数据库
sqlplus / as sysdba startup;
- 解锁hr用户样例数据库,给hr用户设置密码并且连接
alter user hr account unlock; alter user hr identified by hr; conn hr/hr;
- 配置界面化连接
- 关闭数据库时取消监听退出数据库
lsnrctl stop sqlplus / as sysdba shutdown immediately;
2. 使用SELECT语句检索数据
2.1 基础的SELECT语句
--第一条SELECT语句。意思为从EMPLOYEES这张表中去检索所有数据(*),FROM语句发生在SELECT语句之前
--也就是说先去定位到EMPLOYEES这张表后,再去检索表中所有的数据
--上班小经验:千万不要直接使用SELECT * FROM 表名;这条语句 数据太多了
SELECT * FROM EMPLOYEES;
SELECT COUNT(1) FROM EMPLOYEES;--查看表结构,这里能看到列名以及相关的约束条件(not null),并且还能看到列的数据结构
DESC EMPLOYEES;--检索特定的列
SELECT EMPLOYEE_ID,LAST_NAME FROM EMPLOYEES;--单引号的主要作用就是:去认定你被单引号引起来的内容为字符并非某个列的名称
SELECT 'HELLO WORLD' FROM DUAL;SELECT 99*99 FROM DUAL;
SELECT (1+2)*3 FROM DUAL;SELECT last_name,salary,salary+300 FROM employees;SELECT last_name,job_id,salary,commission_pct FROM employees;
--任何值和空值计算都为空值
SELECT last_name,12*salary+commission_pct, commission_pct FROM employees;--不加双引号,不区分大小写,双引号只能用在列别名,单引号代表字符
SELECT last_name AS name, commission_pct comm, salary "工资" FROM employees;
SELECT last_name 姓名,12*salary 年薪 FROM employees;SELECT last_name||' : '||job_id AS "Employees" FROM employees;
SELECT last_name||' is a '||job_id AS "Employees Details" FROM employees;SELECT '''' FROM DUAL;--四个单引号代表一个单引号
-- 最前面的和最后的两个单引号代表界限,中间两个表示转义后的一个单引号
SELECT '''Hello World!''' FROM DUAL;
SELECT '"Hello World!"' FROM DUAL;SELECT department_name||q'[ Department's Manager Id: ]'||manager_id AS "Department and Manager" FROM departments;
--去重运算符
SELECT DISTINCT department_id FROM employees;