登录or启动关闭dba sqlplus / as sysdba直接在cmd里面连接到sys账户不需要listener进程 sqlplus /nolog:不登录到数据库服务器 conn sys/sys_psw as sysdba;以sys的身份连接到数据库 lsnrctl start:启动监听 lsnrctl stop:关闭监听 lsnrctl status:查看监听状态 shutdown immediate;关闭数据库 startup;启动数据库 用户 show user;显示当前连接用户 create user moxy identified by 520527;创建一个新用户 alter user scott account unlock;解锁scoot账户 drop user 用户名 cascade;删除用户并同时删除用户的所有表 alter user 用户名identified by 密码 修改用户密码 授权 grant resource to moxy;赋予改账户resource角色 grant select on scott.emp to stu_user;授予stu_user访问scott表的权限 grant create session to moxy;授权连接数据库 revoke select uodate on emp from 用户名;回收用户访问表的权限 grant create view to moxy;授权它创建试图 查找or保存 select distinct job from emp;查不重复的emp表职位 select count(*) as 工资大于1500的人 from emp2 where sal>1500;工资大于1500 select deptno,avg(sal) from emp group by deptno haeing avg(sal)>1000; select name from v$controlfile; 查看控制文件 select member from v$logfile;查看日志文件 select * from v$instance:查看实例状态 select *from 表单名;查找当前所有名为....的表或试图 save 文件名 创建一个sql start 文件名 装载并执行sql脚本文件 get 文件名 加载sql文件 commit;提交 select * from USER_TRIGGERs;查询触发器 表 create table stu( id varchar2(10) unique,name varchar2(20) not null,age number);创建一张表并给出约束 insert into 表名(sid,sname,age)values('1002','李四',12);存数据 添加一行 comment on column 表名.列名 is '列注释';添加表的注释 comment on table 表名 is'注释';给表添加注释 comment on table 表名.列名 is'注释';给表中的列添加注释 alter table stu add unique(sid);约束id值不能相同 alter table 表名 modify 列名 not null;给表not null (设置非空约束时值为列的值不能为空,设置空约束同) alter table 表名 add constraint 自定义名 foreign key(要设置外键约束的列名)references 要链接的另一张表的表名(表的列名);外键约束 alter table 表名 add constraint 自定义名 primary key(要设置主键约束的列名);主键约束 ALTER TABLE 表名 DROP CONSTRAINT 要删除的约束名;删除约束 alter table 表名 add constraint 唯一约束名 unique(列名); alter table <表名> add constraint <检查约束名> check(<条件>);检查约束 alter table <表名> rename constraint <旧约束名> to <新约束名>;更改约束的名称 update 表名 set 要修改的属性 = 属性值 where 加条件;更改表的数据 rollback;撤回 create table emp_copy as select * from emp;复制一张表 delete from tablename where 条件 删除一行记录 create or replace trigger 触发器名称 before insert on "需要应用的表" for each row begin select 序列名.nextval into :new.id(要自加的字段) from dual; end 触发器名称; 创建触发器 注意要加的字段是否大写 视图 grant create view to moxy;授权它创建试图 create view sal_mid as select ename,job,sal from emp where sal between 1000 and 3000 with check option;创建带约束的视图 且增加修改都要符合约束条件 ....with read only;只读 create or replace view 视图名 as select empno,ename,job,sal from emp;修改视图 rename 需要重命名的视图 to 新视图名;视图重命名 drop view 视图名;删除视图 create sequence 序列名 increment(每次加1) by 1 start with(从1开始) 1 maxvalue(最大排序到100) 100; nextval alter sequence 序列名 要修改的属性 ;更改序列 create synonym 名 for 要修改的同义词; 同义词 host copy(del) 文件地址 目标地址; copy or delete file 查询 多行比较符有:in 与查询返回值中的一个值相等,any 与子查询返回的任何一个值比较,all 与返回的所有值比较 select e.empno,e.ename,e.mgr,b.empno,b.ename from emp e,emp b where e.mgr=b.empno;外连接查询 select *from emp e left join dept d on e.deptno=d.deptno;左连接查询 right 右连接查询 full 全外连接 select c.CLASSID,c.CLASSNAME,s.CLASSID,s.id,s.name,s.age,s.phone from t_student s INNER JOIN t_class c on s.classid =1702 and s.classid=c.classid select deptno,ename,job,sal,hiredate from emp where deptno=(select deptno from emp where empno=7369);查询与7369员工同一个部门的所有职员的信息 select * from emp where sal>(select avg(sal) from emp);查询工资比平均工资高的员工 select deptno,ename,sal,job from emp where sal>any(select sal from emp where deptno=20); select ename,empno,job,sal from emp where sal in(select max(sal) from emp group by job);查询双十一在任职位上工资最高的员工, select ename,job,sal from emp where sal>all(select max(sal) from emp where deptno=20); imp 用户名/密码@orcl file=E:\daochu.dmp rows=y full=y tables=(表名) 导入表 exp 用户名/密码@orcl file=E:\daochu.dmp owner(用户名)