oracle笔记07

来源:互联网 发布:网络风暴检测工具 编辑:程序博客网 时间:2024/06/03 19:07

–创建表空间

   create tablespace htf_tp   datafile 'D:\htf.dbf'   size 20m   autoextend on next 5   maxsize unlimited;

–创建用户 ,如果没有指定用户表空间,默认表空间为users

SQL> create user htf identified by 123;User created

–修改用户默认表空间

SQL> alter user htf default tablespace htf_tp;User altered

–授权

SQL> grant connect to htf;Grant succeededSQL> grant resource to htf;Grant succeededSQL> show user;User is "htf"

–建表t1三个字段

SQL> create table t1(id number,name varchar2(32),birth date);Table created

–插入数据 date类型注意格式:’3-11月-2017’

SQL> insert into t1 values(1001,'htf','3-11月-2017');1 row inserted

–查询数据

SQL> select * from t1;        ID NAME                             BIRTH---------- -------------------------------- -----------      1001 htf                              2017/11/3   

–添加列 在t1表添加 性别列

alter table t1 add(sex char(2));

–修改列
1.修改列名birth -》 birthday

  alter table t1 rename column birth to birthday;

2.name 32位-》128位

  alter table t1 modify (name varchar2(128)) ;
SQL> desc t1;Name     Type         Nullable Default Comments -------- ------------ -------- ------- -------- ID       NUMBER       Y                         NAME     VARCHAR2(32) Y                         BIRTHDAY DATE         Y                         SEX      CHAR(2)      Y  

–删除列:删除sex列

alter table t1 drop (sex,id);Name     Type          Nullable Default Comments -------- ------------- -------- ------- -------- NAME     VARCHAR2(128) Y                         BIRTHDAY DATE          Y   

–重命名表名

rename t1 to students;SQL> desc students;Name     Type          Nullable Default Comments -------- ------------- -------- ------- -------- NAME     VARCHAR2(128) Y                         BIRTHDAY DATE          Y 

–插入全部

insert into students values('hantengfei',to_date('2017/11/03','yyyy/mm/dd'));  

–只插入 name字段

insert into students(name) values('hant');                       NAME                    BIRTHDAY---------------------- htf                     2017/11/3hant                    hantengfei              2017/11/3     

–按sal薪水排列,取第6到10名

1.把所有查询的信息全部列出来并按照规定的字段排序 ename ,sal ,dname

     (select ename,sal,dname       from emp,dept       where emp.deptno=dept.deptno       order by sal desc) t;

2.构建rownum rn字段

   (select t.*,rownum rn from (select ename,sal,dname                               from emp,dept                               where emp.deptno=dept.deptno                               order by sal desc) t) t1;

3.添加限制条件 区间(rn>=M and rn<=N);

   select t1.*    from (select t.*,rownum rn from (select ename,sal,dname                               from emp,dept                               where emp.deptno=dept.deptno                               order by sal desc) t) t1                                 where rn<=10 and rn>6;     

–视图:虚表;

–语法:create view v1 as select 语句;

create view v1 as select ename,sal,deptno from emp;

–通过查询结果建表

create table test asselect e.ename,e.deptno,d.dnamefrom emp e,dept dwhere e.deptno=d.deptno;

–视图与表的区别
1、表需要占用磁盘空间,而视图不需要;
2、视图不能添加索引;
3.使用视图可以简化复杂查询;
4.使用视图利于提高安全性。
5.对视图的修改将会影响实际的数据表

原创粉丝点击