当前位置:操作系统 > Unix/Linux >>

DML语句和表的连接

DML语句和表的连接
 
笛卡尔乘积的写法:(1992)
SQL>select ename ,dname from emp,dept;
 
(1999)
SQL>select ename,dname from emp cross join dept;
 
查询emp和dept中部门号相等的:
SQL>select ename,dname from emp,dept where emp.deptno = dept.deptno; 
 
SQL>select ename,dname from emp join dept on(emp.deptno = dept.deptno);
 
SQL>select ename,dnam from emp join dept using (deptno);
 
查询工资的等级;
SQL>select ename,grade from emp e join salgrade s on(e.sal between s.losal and s.hisal);
 
SQL>select ename,dname ,grade from emp e
join dept d on(e.deptno = d.deptno)
join salgrade s on(e.sal between s.losal and s.hisal)
where ename not like '_A%';
 
查询每一个员工的经理:
SQL>select e1.ename ,e2.ename from emp e1 join emp e2 
on (e1,mgr = e2.mgr);
 
外连接:
左连接:
SQL e1.ename ,e2.ename from emp e1 left join emp e2 on (e1.mgr = e2.mgr);
右连接:
SQL>select ename ,dname from emp e right outer join dept d on (e.deptno = d.deptno);
 
全连接:
SQL>select ename ,dname from emp e full join dept d on (e.deptno = d.deptno);
 
简单连接:
SQL>select e1.ename ,e2.ename from emp e1 left join emp e2 on(e1.mgr = e2.empno);
 
DML 语句
select查询现有的数据:
insert向数据的表中插入数据:
update修改现有的存在数据库中的语句:
delete删除数据库中的数据;
 
只有超级管理员才能添加用户:
conn sys/**** as sysdba;
(删除一个用户)
drop user xiaoming  cascade;
 
1.     backup scott(备份scott用户的相关数据)
        exp  把scott的数据导入到一个新的用户中;
       
2     创建一个新用户:
create user xiaoming identified by xiaoming default tablespace users
quota 10M on users;
给用户赋权限:
grant create session, create table ,create view  to xiaoming;
 
3     import the data 
 
rollback(回滚到刚才的操作);
 
对emp表进行备份:
create table emp2 as select * from emp;
 
对dept2中插入数据:
insert into dept2 values (50,'game','bj');
插入部门字段的数据:
insert into dept2(deptno ,dname)values (60,'game2');
插入子查询拿来的一张表,但这两张表的结构要相同:
insert into dept2 select * from dept;
 
update 的应用:
update emp2 set sal = sal*2, ename = ename||'-' where deptno = 10;
select ename,sal from emp2 where deptno = 10;
 
delete的应用:
delete from emp2;
delete from dept2  where deptno;
 
rollback
 
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,