当前位置:网站首页>7. Oracle table structure

7. Oracle table structure

2022-07-05 06:28:00 Cold leaves elegant_

  1. Press the list structure to use SQL Sentence creation class,student Two tables .

Class

Name data type constraint remarks
CNONUMBER(2) Primary key Class number
CNAMEVARCHAR(20) Class name
NUMNUMBER(3) The number of

Student

Name data type constraint remarks
SNONUMBER(4) Primary key Student number
SNAMEVARCHAR(10) only full name
SAGENUMBER Age
SEXCHAR(2) Gender
CNONUMBER(2) Class number

create table class(
cno number(2) primary key,
cname varchar(20),
num number(3)
)
 
create table student(
sno number(4) primary key,
sname varchar(10),
sage number,
sex char(2),
cno number(2), 
unique(sname)
)

 Insert picture description here
 Insert picture description here

  1. by student Add a delayed foreign key constraint to the table , Its CNO Column reference class Tabular CNO Column .
alter table student
add constraint P_FK foreign key(cno) references class(cno) on delete cascade deferrable

 Insert picture description here

  1. by student Add a delayed foreign key constraint to the table , Ensure that the value of this list is 0~100 Between .
alter table student
add check (sage between 0 and 100)

 Insert picture description here

  1. by student Tabular SAGE Add a check constraint to the column , Ensure that the value is “M ” or “F”, And the default value is “M ”.
alter table student modify(sex char(2) default('m'));
alter table student add check(sex in(‘m’,’f’));

 Insert picture description here

  1. Create a transaction level temporary table and a session level temporary table by using sub queries , Its structure and student The table structure is the same
create global temporary table tran_temp_student on commit delete rows as select * from student;
create global temporary table sess_temp_student on commit preserve rows as select * from student;

 Insert picture description here

  1. Create a student_range surface ( Column 、 The type and student Columns of the table 、 The same type ), It is divided into three districts according to the age of students , lower than 20 Put the information of year-old students into part1 District , Stored in EXAMPLE Tablespace ;20-30 Years old on part2 District , Store in ORCLTBS1 Tablespace ; Other data are placed in part3 District , Store in ORCLTBS2 Tablespace
create tablespace ORCLTBS1 datafile 'D:\app\Snakewood\oradata\testdba\ORCLTBS1' size 20m;
create tablespace ORCLTBS2 datafile 'D:\app\Snakewood\oradata\testdba\ORCLTBS2' size 20m;
create tablespace EXAMPLE datafile 'D:\app\Snakewood\oradata\testdba\EXAMPLE' size 20m;
create table student_range partition by range(sage)(partition part1 values less than (20) tablespace example,partition part2 values less than(30) tablespace ORCLTBS1,partition part3 values less than(maxvalue) tablespace ORCLTBS2)as select * from student;

 Insert picture description here
 Insert picture description here

  1. Create a student_list surface ( Column 、 The type and student Columns of the table 、 The same type ), It is divided into two districts according to students' gender
create table student_list
    partition by list(sex)
    (
    partition student_male values('M') tablespace ORCLTBS1,
    partition student_female values('F') tablespace ORCLTBS2
    ) as
    select * from student;

 Insert picture description here

  1. Will save a student information .xls The file is converted to .txt file , according to .txt The structure of the data in the file creates an external table , Realize to .txt Read operation of
     Insert picture description here
原网站

版权声明
本文为[Cold leaves elegant_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050620029437.html