SQL事务和表的关系

-- 事务
create table t_account(
id int(8) primary key,
money double
)
-- rollback 是回滚即删除事务
begin;
update t_account set money=money-100 where id=1;
update t_account set money=money+100 where id=2;
COMMIT;


-- 表的关系
-- 建立学生表,成绩表,课程表
create table t_student(
id int(8) primary KEY, 
name varchar(200) not null,
age int(4));


create table t_subject(
id int PRIMARY KEY,
name VARCHAR(200) not null);


create table t_score(
id int(2) PRIMARY KEY,
score int(4),
s_id int(3),
sub_id int(4),
CONSTRAINT f_k2 foreign key (s_id) REFERENCES t_student (id),
constraint f_k3 foreign key (sub_id) references t_subject (id));


-- 建立身份证表和人表(一对一)
-- 第一种方式:
create table t_person(
id int primary key,
name varchar(200),
age int);


create table t_idcard(
card_number VARCHAR(18) primary key,
create_date date,
f_id int unique,
foreign key (f_id) references t_person (id)
);


-- 第二种方式更简单
create table t_person2(
id int primary key,
name varchar(200),
age int);


create table t_idcard2(
id int(4) primary key,
card_number VARCHAR(18) unique,
create_date date,
foreign key (id) references t_person2 (id)
); 
-- 总结:一对一的关联关系,只需要想办法让外键字段同时有唯一约束,外键字段在任意一个表中都可以。






-- 一对多 关联关系,只需要在多的那张表中增加一个外键字段就可以了。


-- 多对多 关联冠南西,需要找一个中间表,转化成两个多对一关系


-- PowerDesigner是设计建表模型的软件
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值