-- 事务
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是设计建表模型的软件
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是设计建表模型的软件