• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

小白学Oracle第二关:第oracle数据库表的创建

武飞扬头像
PHP中文网
帮助9

学新通技术网

我们本次创建表的需求是:创建一张班级表,和一张学生表。

学新通技术网

1.首先班级表作为主表也就是所谓的主键。在主表中我们这里使用的约束是primarykey 和not null

create table classinfo(
       classid number(2) primary key,
       classname varchar(10) not null       
       );

sql解析:

--create table 创建表的关键字

--classinfo 是创建的表的名字

--classid 是班级表的id 数据类型是number(2)类型,我们默认给了2个长度,我们将班级id设置为主键方便其他外键关联

--classname 是班级名字 数据类型是字符型varchar(10),我们给了默认10个字符长度,班级名的约束是不能为空

执行sql语句:

学新通技术网

classinfo表创建成功。

学新通技术网

2.然后我们建立一个外键,也就是关联到主键的一个表,使用的数据类型和约束请看下面的sql语句。

create table studentinfo(
       studentid number(2) primary key,
       studentname varchar(10) not null,
       studentsex char(2) check(studentsex='男' or studentsex='女'),
       studentage number(2) not null,
       studenttel number(11) unique,
       studentaddress varchar(50) default '上海',
       classid number(2) references classinfo(classid)
       );

sql语句解析:

--create table 创建表的关键字

--studentinfo();是创建学生信息表的表名

--studentid(学生id) 约束是主键 primary key

--studentname(学生姓名) 约束是 not null

--studentsex(学生性别) 约束是 check

--studentage(学生年龄) 约束是 not null

--studenttel(学生电话) 约束是 unique

--studentaddress(学生地址) 分别为学生表中的列名。

学生表studentinfo建立完成。

学新通技术网

完整的sql语句如下:

create table classinfo(
       classid number(2) primary key,
       classname varchar(10) not null       
       );
       
create table studentinfo(
       studentid number(2) primary key,
       studentname varchar(10) not null,
       studentsex char(2) check(studentsex='男' or studentsex='女'),
       studentage number(2) not null,
       studenttel number(11) unique,
       studentaddress varchar(50) default '上海',
       classid number(2) references classinfo(classid)
       );

到此,我们创建的班级表和学生表就演示完了,是不是很简单呢?

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tangbhifk
系列文章
更多 icon
同类精品
更多 icon
继续加载