Dưới đây là cách nhận séc bạn muốn nhanh chóng và dễ dàng:
drop database if exists gtest;
create database if not exists gtest;
use gtest;
create table users (
user_id integer unsigned not null auto_increment primary key,
username varchar(32) not null default '',
password varchar(64) not null default '',
unique key ix_username (username)
) Engine=InnoDB auto_increment 10001;
create table owners (
owner_id integer unsigned not null auto_increment primary key,
ownername varchar(32) not null default '',
unique key ix_ownername (ownername)
) Engine=InnoDB auto_increment 5001;
create table users_and_owners (
id integer unsigned not null primary key,
name varchar(32) not null default '',
unique key ix_name(name)
) Engine=InnoDB;
create table p_status (
a_status char(1) not null primary key
) Engine=InnoDB;
create table people (
person_id integer unsigned not null auto_increment primary key,
pid integer unsigned not null,
name varchar(32) not null default '',
status char(1) not null,
unique key ix_name (name),
foreign key people_ibfk_001 (pid) references users_and_owners(id),
foreign key people_ibfk_002 (status) references p_status (a_status)
) Engine=InnoDB;
create or replace view vw_users_and_owners as
select
user_id id,
username name
from users
union
select
owner_id id,
ownername name
from owners
order by id asc
;
create trigger newUser after insert on users for each row replace into users_and_owners select * from vw_users_and_owners;
create trigger newOwner after insert on owners for each row replace into users_and_owners select * from vw_users_and_owners;
insert into users (username, password) values
('fred Smith', password('fredSmith')),
('jack Sparrow', password('jackSparrow')),
('Jim Beam', password('JimBeam')),
('Ted Turner', password('TedTurner'))
;
insert into owners (ownername) values ('Tom Jones'),('Elvis Presley'),('Wally Lewis'),('Ted Turner');
insert into people (pid, name, status) values (5001, 'Tom Jones', 1),(10002,'jack Sparrow',1),(5002,'Elvis Presley',1);
Cảm ơn bạn đã trả lời ngắn gọn và cụ thể –
Đôi khi tôi tự hỏi tại sao ai sử dụng MySQL nữa - "oops, xin lỗi, chúng tôi quyết định không thực hiện toàn vẹn dữ liệu!". Nếu bạn có bất kỳ loại điều khiển nào của RDBMS của mình và bạn muốn có nguồn mở, Postgres là The Way. – Jordan
Đúng vậy. MySQL không phải là một lựa chọn tốt vì nhiều lý do và đây là một trong số đó. – Dmitri