该【MySQL数据库的建表】是由【鼠标】上传分享,文档一共【5】页,该文档可以免费在线阅读,需要了解更多关于【MySQL数据库的建表】的内容,可以使用淘豆网的站内搜索功能,选择自己适合的文档,以下文字是截取该文章内的部分文字,如需要获得完整电子版,请下载此文档到您的设备,方便您编辑和打印。一对多关系
订单表
create table orderinfo(
orderid int(4), 订单表 id
ordermc varchar(20), 订单表名称
totalnum int(4), 订单表总数量
totaljg decimal(8,2), 订单表总价格
orderdate date, 订单表日期
primary key(orderid) 订单表的主键
)
订单项表
create table orderitem(
itemid int(4), 产品 id
itemmc varchar(20), 产品名称
price decimal(4,2), 产品单价
num int(4), 产品数量
orderid int(4), 产品外键
primary key(itemid), 产品主键
foreign key(orderid) REFERENCES orderinfo(orderid) 所有订单项的外键对应(关系)着订单的主键。
)
备 注:
Foreign:外的意思。 REFERENCES: 对应、关系
Orderinfo : 订单表的表名
orderid: 具有两层意思,在 orderinfo 订单表里担任主键,在 orderitem 订单项表里担任外键。
inner join(在 12-13Hibernate 复习视频 A 一开始的知识) inner 内部,里面的 join 关联,连接,结合,参加
select * from dept A,emp B where = (条件查询)
select * from dept A inner join emp B on =内 ,(对等关联查询)
select * from dept A left join emp B on =左 查(询)
select * from dept A right join emp B on =右 查(询)
sex char(2) not null default‘男’, 性别不为空,默认值为“男” 。
primary key (empid,eduid), 双主键
foreign key(empid) REFERENCES emp(empid), 当前表的外键(empid)对应 emp
表的主键(empid)。
id int(4) primary key AUTO_INCREMENT,
AUTO:是自动的意思 INCREMENT:是增加,增量的意思 这句话的意思是 id 设为主键,并且是自增的。
多对多关系
create table xtyhb( 系统用户表
yhbh int(4) primary key, 用户编号(id)
yhmc varchar(20), 用户名称
yhmm varchar(20), 用户密码
yhzt tinyint(1) 用户状态
)
create table xtjsb( 系统角色表
jsbh int(4) primary key AUTO_INCREMENT, 角色编号(id)是自动增长的 jsmc varchar(20) 角色名称
)
create table yhjs( 用户角色(中间表)
yhbh int(4), 用户编号(id)
jsbh int(4), 角色编号(id)
primary key(yhbh, jsbh), 主键(用户编号(id),角色编号(id)) 双主键,双外键
foreign key (yhbh) REFERENCES xtyhb(yhbh), 这个表(中间表)的双主键
用户编号(id)对应着系统用户表的主键(用户编号(id))
foreign key (jsbh) REFERENCES xtjsb(jsbh) 这个表(中间表)的双主键
角色编号(id)对应着系统角色表的主键(角色编号(id))
)
into 到...里, 进入到...之内, 成为...状况, 深入...之中 的意思。
MySQL 数据库中删除库的语句是:(drop database 库名)
username varchar(20) unique not null, //确定唯一性的。
Oracle数据库:
Unique:唯一的意思。(唯一约束)
Primary 第一位,主要的意思。(主键约束)
Foreige 外交,外来的意思。(外键约束)
Check 阻止,制止,控制,检查,核对,证明无误,核对无误,的意思。(Check 约束)是有条件限制的。
以下两者都是对年龄段的效验,均可:
age>=18 and age<=60 和 age between 18 and 60 效果都是一样的。 Between 的意思是: 在...之间
通过语句来创建表(一对多)
create table dept2(
deptid number(10),
deptname varchar2(20),
deptnum number(3),
deptdesc varchar2(50),
constraint dept2_PK primary key(deptid) 主键约束
)
Constraint: 约束, 强制的意思。 dept2_PK:约束的名称
drop table emp2; 删除一个表
create table emp2(
empid number(10),
empname varchar2(20),
sex char(2),
age number(3),
phone varchar2(50),
birthday date,
deptid number(10), 外键
constraint emp2_PK primary key(empid), 主键约束
constraint emp2_Fk foreign key(deptid)references dept2(deptid), 这个表
的外键映射或着对应上面表的主键
constraint emp2_Uni unique(phone), 唯一性约束
constraint emp2_check check(age between 18 and 60 and sex 男in',' 女(''))
) Check 约束,就是条件约束。
References:引用,映射的意思。
create table dept1 (
deptid number(10),
deptname varchar2 (20),
deptnum number(3),
deptdesc varchar2 (50),
constraint dept1_pk primary key (deptid ))
create table emp1(
empid number(10),
empname varchar2(20),
empsex char(2) default ' 男' not null, 默认为"男",不许为空
empage number(3),
phone number(12),
address varchar2(50),
deptid number(10),
constraint emp1_pk primary key(empid), 主键约束
constraint emp1_fk foreign key(deptid) references
dept1(deptid),
外键约束,对应另一个表的主键
constraint emp1_uni unique(phone),
电话约束
下面这个是年龄
约束
constraint emp1_check check(empage between 18 and 60),
constraint emp1_check1 check(empsex in(' 男',' 女')) 性别约
束
)
(多对多语句建表):
create table xtyhb2(
yhbh number(10),
yhmc varchar2(20),
yhmm varchar2(20),
yhzt number(1),
constraint xtyhb2_PK primary key(yhbh)
)
create table xtjsb2(
jsbh number(10),
jsmc varchar2(20),
constraint xtjsb2_PK primary key(jsbh))
create table yhjs2(
yhbh number(10),
jsbh number(10),
constraint yhjs2_PK primary key(yhbh,jsbh),
constraint yhjs2_FK foreign key(yhbh)references xtyhb2(yhbh),
constraint yhjs2_FK2 foreign key(jsbh)references xtjsb2(jsbh))
Constraint: 约束, 强制的意思。 xtyhb2_PK:约束的名称
References:引用,映射的意思。
Primary:主键约束
Foreign:外键约束
(-- 创建表空间)( 详见: 3-18( 表空间 and 大字段) 视频)
create tablespace sp1 logging
datafile 'D:\Oracle\oradata\dbfs\' size 20M default storage (initial 1 next 2 minextents 1 maxextents unlimited pctincrease 10);
tablespace:是创建表空间的关键字 sp1:是给表空间起个名字
logging:,可以通过日志对表数据进行从做恢复和还原.
Datafile:表空间对应的数据文件('D:\Oracle\oradata\dbfs\')。
Dbf:是数据库文件的所写。Db: database的缩写 f: file 的缩写
size 20M:数据库文件对应的尺寸是 20 兆
default:指明如何来使用和合理安排这个数据库文件
storage:存储的意思 initial 1:初始化 1 兆 next 2:下一次初始化 2 兆
minextents:最下扩展 1 兆 maxextents unlimited:最大扩展不限定
min:是最小的意思 extents:是扩展的意思
pct:是百分比(percent)的缩写
pctincrease 10:是数据库文件容量扩展递增的百分比(以 10%在扩展)
increase:是递增的意思
--创建表(记录庞大)同时把表数据划分到不同的表空间中去 create table orders(
orderid number(8),
ordermc varchar2(30),
ordernum number(8),
orderje number(10,2),
orderdate date,
constraint orders_PK primary key(orderid)
)partition by range(orderid)
(
partition "01" values less than(100000) tablespace sp1, 对表数据
partition "02" values less than(200000) tablespace sp2, 进行分区
partition "03" values less than(300000) tablespace sp3
);
MySQL数据库的建表 来自淘豆网m.daumloan.com转载请标明出处.