Orcale 复习知识点

---orcal 复习知识点

--------------------------------------------DDL-------------------------------------------------------
/**
  create table
*/

create table tb_class(
       id number primary key,
       name varchar2(18) not null unique
);
create table tb_student(
       id number primary key,
       name varchar2(18),
       age number not null check(age>18 and age<60),
       sex varchar2(2) not null check(sex='男' or sex='女'),
       address varchar2(80) not null,
       -- email varchar2(18) not null unique
       class_id number references tb_class(id)
);

-- 第二种建表方式
   create table tb_class2
   as
   select * from tb_class;
   
-- 第三种建表方式
   create table tb_class3
   as
  select name from tb_class
  and
  select id from tb_class;
/**
       drop table
*/

drop table tb_student;
drop table tb_class;

/**
     alert

*/
alter table tb_student
drop column age;

alter table tb_student
add(age number check(age>21 and age<60));

alter table tb_student 
rename column age to a;

alter table tb_student
add constraints tb_student_name_unique unique(name);
----------------------------------------------DML----------------------------------------------------

/**

     delete table
*/

delete from tb_class;

/**
       select table
*/

select * from tb_class;
select * from tb_student;

/**

       insert into
*/
insert into tb_class(id,name)values(1,'J1202');
insert into tb_class(id,name)values(2,'J1203');
insert into tb_class(id,name)values(3,'J1204');


insert into tb_student(id,name,sex,birthday,age,class_id)values(1,'sharp','男',sysdate,22,1);
insert into tb_student(id,name,sex,age,class_id)values(2,'sharp1','男', 22 ,2);
insert into tb_student(id,name,sex,age,class_id)values(3,'sharp2','男', 24 ,3);
/**
       update table

*/

update tb_class set name='hello' where id=1;


update tb_student set name='sharp1'where id = 2;
update tb_student set name = (select name from tb_class where id = 1 )where id = 3;


-------------------------------------约束-------------------------------------

--列约束

--第一种方式

       create table tb_class(
              id number primary key,
              name varchar2(18) not null unique
       )
       create table tb_student(
              id number primary key,
              name varchar2(18),
              age number check(age>18 and age<60),
              sex varchar2(2) check(sex='男'or sex='女'),
              birthday date default sysdate check (birthday > to_date('1990-10-10','yyyy-MM-dd') and birthday < to_date('2012-10-10','yyyy-MM-dd')),
              class_id references tb_class(id)
       )
       
       create table tb_class(
              id number,
              name varchar2(18) not null,
              constraints tb_class_name_unique unique(name),
              constraints tb_class_id_PK primary key(id)
       );
       create table tb_student(
              id number,
              name varchar2(18),
              sex varchar2(2),
              age number not null,
              birthday date default to_date('1987-08-07 12:12:12','yyyy-MM-dd hh:mi:ss'),
              class_id number ,
              constraints tb_student_id_PK primary key (id),
              constraints tb_student_name_unique unique(name),
              constraints tb_student_sex check(sex='男'or sex='女'),
              constraints tb_student_age check(age>18 and age<60),
              constraints class_id foreign key (class_id) references tb_class(id)
       );
       
       

--表约束

-------------------------------------约束-------------------------------------


你可能感兴趣的:(Orcale 复习知识点)