oracle建表语句

---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.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);

添加约束

--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);

 

 

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值