資料庫表設計最終方案__資料庫

來源:互聯網
上載者:User
每天睡覺前必做兩件事 資料庫表的設計 一對多 多對一 多對多 一對一 一對多或多對一:多的一方加外鍵,描述資料的關係 多對多:加中間表來描述關係 一對多或多對一的對象存到資料庫時,表的設計方案(部門和員工)

create table department
(
id int primary key,
name varchar( 40)
);
create table employee
(
id int primary key,
name varchar( 40),
salary decimal( 8,2),
department_id int,
constraint department_id_FK foreign key( department_id) references department(id)
); 多對多個物件的表的設計( 老師和學生)

create table teacher
(
id int primary key,
name varchar( 40),
salary decimal( 8,2)
);
create table student
(
id int primary key,
name varchar( 40)
);
create table teacher_student
(
teacher_id int,
student_id int,
primary key( teacher_id,student_id),
constraint teacher_id_FK foreign key( teacher_id) references teacher( id),
constraint student_id_FK foreign key( student_id) references student( id)
); 一對一表的設計( 人和身份證)

主從關係,從加外鍵,外鍵約束,唯一約束,非空約束
create table person
(
id int primary key,
name varchar( 40)
);
create table idcard
(
id int primary key,
city varchar( 40),
constraint id_FK foreign key(id) references person(id)
); 自串連表的設計( 家族管理系統)

create table person
(
id int primary key,
name varchar( 40),
parent_id int,
constraint parent_id_Fk foreign key( parent_id) references person( id)
);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.