创建数据库命令:
查询工资大于12000的员工姓名和工资
select last_name,salary
fromemployees
wheresalary>12000
选择雇用时间在1998-02-01到1998-05-01之间的员工姓名,job_id和雇用时间
select last_name,job_id,hire_date
fromemployees
whereto_char(hire_date,'yyyy-mm-dd') between '1998-02-01 'and' 1998-05-01'
显示所有员工的姓名,部门号和部门名称。
selectlast_name,e.department_id,department_name
fromemployeeseleftouterjoindepartmentsd
one.department_id=d.department_id
分组函数
查询各job_id的员工工资的最大值,最小值,平均值,总和
selectjob_id,max(salary),min(salary),avg(salary),sum(salary)
fromemployees
groupbyjob_id
子查询实例:
查询和姓名中包含字母u的员工在相同部门的员工的员工号和姓名
selectemployee_id,last_name
fromemployees
wheredepartment_idin(
selectdepartment_id
fromemployees
wherelast_namelike'%u%'
)
) andlast_namenotlike'%u%'
将表departments中的数据插入新表dept2中
createtabledept2
as
select*fromdepartments
删除表emp5
drop table emp5;
将表employees2重命名为emp5
rename employees2 to emp5
在表dept和emp5中添加新列test_column,并检查所作的操作
alter table dept
add(test_column number(10));
约束
向表emp2的id列中添加PRIMARYKEY约束(my_emp_id_pk)
ALTER table emp2
ADD constraint my_emp_id_pk primary key(id)
视图改变成只读视图
createorreplaceviewemployee_vu
as
selectlast_name,employee_id,department_id
fromemployees
wheredepartment_id=80
withreadonly