Oracle建表语法

时间:2022-07-25
本文章向大家介绍Oracle建表语法,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1、创建表的语句

---1、创建模拟的数据表 ---
  --1.1.创建学生表Student
  create table Student(
         StuId NUMBER NOT NULL,     --学生ID
         StuName VARCHAR2(10) NOT NULL, --名称
         Gender VARCHAR2(10)NOT NULL,  -- 性别
         Age NUMBER(2) NOT NULL,    -- 年龄     
         JoinDate DATE NULL,       --入学时间
         ClassId NUMBER NOT NULL,   --班级ID
         Address VARCHAR2(50) NULL   --家庭住址           
  ); 
  --1.2、创建班级表StuClass 
  create table StuClass(
       classId NUMBER not null,     -- 班级ID
       ClassName varchar2(20) not null,  --班级名称
       Notes varchar2(50) null default'班级信息',  --备注,默认班级信息
  );

2、创建约束的语句

----2、创建数据表的约束---
  --2.1)创建主键约束--
  alter table Student add constraint PK_Student_StuId primary key(StuId);
  alter table StuClass add constraint PK_StuClass_ClassId primary key(ClassId);
  --2.2) 创建检查约束--
  alter table Student add constraint CK_Student_Gender check(gender='男' or gender='女');
  alter table Student add constraint CK_Student_Age check(Age>=0 and Age<=100);
  --2.3)创建唯一约束--
  alter table Student add constraint UQ_Student_StuName unique(StuName);
  --2.4)创建默认约束--
  --alter table Student add constraint DF_Student_Address default('地址不详');
  alter table Student Modify Address varchar(50) default '地址不详';
  alter table Student Modify JoinDate Date default sysdate;
  --2.5)创建外键约束--
  alter table Student add constraint FK_Student_StuCLass_ClassId 
  foreign key(ClassId) references StuClass(ClassId);

注意:创建表还是约束,与SQL Server基本相同,注意:在Oracle中default是一个值,而SQL Server中default是一个约束,

因此Oracle的default设置可以在建表的时候创建或者通过Modify函数创建

3、添加模拟的数据

--3、添加模拟的数据--
  --3.1)添加班级信息
  insert into StuClass(ClassId,Classname) values(1,'一班');
  insert into StuClass(ClassId,Classname) values(2,'二班');
  insert into StuClass(ClassId,Classname) values(3,'三班');
   --3.2)添加学生信息
  insert into Student(StuId,Stuname,Gender,Age,ClassId)
  values(1,'关羽','男',17,1);
  insert into Student(StuId,Stuname,Gender,Age,ClassId)
  values(2,'张飞','男',16,2);
   insert into Student(StuId,Stuname,Gender,Age,ClassId)
  values(3,'刘备','男',18,3);

Oracle 建表语句

    create table table_name(
    	id numner(12),		
        text verchar2(255 CHAR) not null,   	--char类型,一个汉字占一个长度
        PID varchar2(32 BYTE) NOT NULL,   	--byte类型,UTF8一个汉字占大约两个长度
        status number(1) DEFAULT 0 null  		--添加默认值 如果为空默认值就为0
    )
    --添加主键
    ALTER TABLE "test"."table_name" ADD PRIMARY KEY ("ID");
    
    --添加注释
    comment on column table_name.id is '主键';
    comment on column table_name.text is '说明';
    comment on column table_name.status is '状态';


--主键自增 ,1新建一个序列
	CREATE SEQUENCE cw_bl_id_increment  
	INCREMENT BY 1  
	START WITH 1  
	MAXVALUE 1.0E20  
	MINVALUE 1  
	NOCYCLE  
	CACHE 20  
	NOORDER  
--主键自增 ,2创建一个触发器
	create or replace trigger 触发器名
	before insert on 表名
	for each row
	begin
	select 序列名.nextval into :new.id from dual;
	end;

--添加字段
ALTER TABLE table_name ADD (
	RS_SFTG NUMBER (1),
	RS_TGJE VARCHAR2 (255 CHAR)
);
--删除字段
alter table table_name drop column RS_SFTG ;

序列参数说明

CREATE SEQUENCE SEQNAME    	      //序列名字         
INCREMENT BY 1                    //每次自增1, 也可写非0的任何整数,表示自增,或自减  
START WITH 1                       //以该值开始自增或自减  
MAXVALUE 1.0E20                   //最大值;设置NOMAXVALUE表示无最大值  
MINVALUE 1                           //最小值;设置NOMINVALUE表示无最大值  
CYCLE or NOCYCLE                  //设置到最大值后是否循环;  
CACHE 20                              //指定可以缓存 20 个值在内存里;如果设置不缓存序列,则写NOCACHE  
ORDER or NOORDER                  //设置是否按照请求的顺序产生序列  

本文整理自以下两篇博文: https://blog.csdn.net/qq_40147275/article/details/85242496 https://www.cnblogs.com/xielong/p/9121680.html