Oracle建表

1.oracle数据库中的多种数据结构:

1.表结构            存储数据

2.视图 一张表或多张表中数据的字节

3.sequence 主要用来生成主键值

4.index 提高检索性能

  我们需要学会创建数据结构

2.表结构:

1.表结构可以随意创建

2.表结构不需要预先申请空间

3.可以在线修改。

3.创建语法:

创建表的释放有两种:基本操作  子查询

 

  3.1基本操作

3.1.1 语法:

create table [schema.]tb_name(

col_name datatype [default value] [colum_constraints],

...,

[table_constraint],

...

    );

  note :

1.create table 关键字,固定写法,

  schema,在oracle数据库中代表用户名

2.tb_name代表表名,可以自定义:但是需要遵循命名规则(详见3.1.2命名规则)

3.列名一般也要求遵循明明规则(详见3.1.2命名规则)

4.dataType,列所属的数据类型,详见(3.1.3 oracle支持的数据类型)

3.1.2 命名规则

1.字母开头

2.长度为1-30

3.只能有大小写英文,数字和_ $ #

4.同一个用户下的对象名不能重复

5.不能使用关键词作为表名(如:select group等等)

3.1.3 oracle支持的数据类型:

类型名 描述

VARCHAR2(size)  可变长字符串,

CHAR(size)  定长字符串

NUMBER  数字类型

NUMBER(p,s)  数字类型

DATE  日期类型

CLOB  字符大数据对象

BLOB  二进制大数据对象

 note

1.charvarchar2varchar

  用法:

char(size),varchar2(size) varchar(size)

size用来指明所能保存字符值的上限。

  区别:

char:定长字符

即一旦确定了()中的字符个数,在保存数据的时候,不论你保存的字符个数为多少个,所占空间大小为固定的()中的字符个数。

char(2)   保存 a  ab都占用2个字符空间

 

varchar , varchar2:不定长字符

即在保存数据的时候,会先判断字符个数,然后再分配对应的空间进行保存。

varchar(2)

保存a 占用1字符空间

保存ab 占用两2字符空间

 

oracle数据库中,指定变长字符串首选varchar2.

2.number(p,s):

p确定数字的有效位数

s确定数字的小数点位数

number(4,2)最大值和最小值为多少?

number(1,3)

-99.99~99.99

3.date: 日期类型

系统默认日期类型:'DD-MON-YY'

操作字符类型和日期类型数据的时候,一定要放到''中间

3.1.4 default:设置默认值

1.作用:设置在往表中插入数据时,如果没有指定该列的值,默认插入的值。

2.默认值可以是合法的字面值(根据定义的列的数据类型来赋值),表达式,或者是sysdateuser等合法的sql函数。

 create table test(

start_date date default sysdate);

3.默认值不能使用其他表的列或者不存在的列/伪列

3.1.5 约束

定义:所谓约束就是强制表中的数据列必须遵循的一些规则。

  而且如果表中存在依赖约束,可以阻止一些不合理的删除操作。

分类:

表级约束:定义在表级别的约束(即在列的完整定义完成后,才定义的约束)

column dataType ,

unique(column)

列级约束:直接跟在列完整性定义后边的约束

column dataType unique,

种类:

   约束名   描述 分类

NOT NULL    :非空     列级

UNIQUE :唯一 列级/表级

PRIMARY KEY :主键 列级/表级

FOREIGN KEY :外键 列级/表级

CHECK :自定义 列级/表级

创建时间:

1.创建表的同时定义约束

2.表创建完成之后,通过修改表结构(后期章节描述)

创建语法:

列级:

column [CONSTRAINT constraint_name] constraint_type,

表级:

column,...(列完整定义结束)

    [CONSTRAINT constraint_name] constraint_type (column, ...),....

详细介绍:

1.not Null:值不允许为null,阻止null值输入

note:只能是列级约束

例如:

create table test( id number constraint test_nn_id not null);

create table test( id number not null);

2.unique:唯一值约束,要求值必须唯一,不能重复。

note

1.可以设置单列唯一,或者组合列唯一

2.如果unique约束单列,此列可以为null

3.可以是列级,也可以是表级约束

4.对于unique列,oracle会自动创建唯一值索引。

例如:

create table test(id number constraint test_un_id unique);

create table test(

id number,

constraint test_un_id unique(id)

);

create table test(id number unique);

create table test(

id number,

name varchar2(10),

constraint test_un_id_name unique(id,name)

);

create table test(

id number,

name varchar2(10),

unique(id,name)

);

3.Primary key:主键

note:

1.主键用来给表中的每一行数据设置唯一标识符。主键只能有一个。

2.主键可以是单列,也可以是组合列。

3.强制非空且唯一,如果由多列组成,组合唯一且列的每一部分都不能为null

4.可以表级,可以列级。

5.自动创建唯一值索引。

例如:

create table test(id number constraint test_pk_id primary key);

create table test(

id number,

constraint test_pk_id primary key(id)

);

create table test(id number primary key);

create table test(

id number,

name varchar2(10),

constraint test_pk_id_name primary key(id,name)

);

create table test(

id number,

name varchar2(10),

 primary key(id,name)

);

4.foreign key:外键

一般在设计表与表之间的关系时,为了减少数据冗余,一般做的操作是在其中一张表中设置一列(组合列),这一列(组合列)的值可以唯一的确定另外一张表中和当前表相关联的一行数据。那么这个列称为外键。

note

1.可以是单列,也可以是组合列

2.引用当前表或者其他表中(只要想和当前表建立关系的表) 的主键列或者unique

3.可以是表级别/列级别

4.值必须是引用的列的值或者为null

5.有外键约束时,如果想要删除的父表(被引用的表)中的某一条数据时,必须保证在子表(引用表)中没有和这条数据相关联的数据存在。

6.ON DELETE CASCADE ,指明在删除父表中数据时可以级联删除子表中数据

例如:

create table emp(id number primary key);---->父表

1:m/m:1

create table test(

id number constraint test_fk_emp_id references emp(id));

1:1

create table test(

id number references emp(id) unique);

create table test(

id number,

constraint test_fk_emp_id foreign key(id) references emp(id)

);

create table test(id number references emp(id));

 

create table emp1(

id number,

name varchar2(10),

primary key(id,name)

);

 

create table test4(

id number,

name varchar2(10),

constraint test_fk_emp_id_name foreign key(id,name)

references emp1(id,name)

);

create table test(

id number,

name varchar2(10),

foreign key(id,name) references emp(id,name) on delete cascade

);

5.check : 定义每一行必须遵循的规则

note

1.可以是表级/列级约束

例如:

create table test(

gender varchar2(2) constraint test_check_gender check(gender in ('F','M')),

age number check (age>=15 and age<=20)

);

create table test(

gender varchar2(2),

constraint test_check_gender check(gender in ('F','M'))

);

create table test(

gender varchar2(2),

check(gender in ('F','M'))

);

 

   3.2. 子查询

一般使用子查询建表,要将另外一张表中的某些数据存放到一张新的表格中。(相当于将原来打印在控制台上的信息,现在直接定义成一张新的表格。)

   语法:

create table tb_name[(column,...)]

as

select ...

   note1.在用子查询建表时,只有not Null约束会被复制。

 2.创建表时可以指定列名,也可以不指定,但是一定不指定列的数据类型

 3.创建表的列跟子查询表的列数要保持一致。

posted @ 2016-09-19 22:02  少帅java  阅读(1107)  评论(0编辑  收藏  举报