diff --git a/advanced-database-applications b/advanced-database-applications new file mode 160000 index 0000000000000000000000000000000000000000..c1ef95bf152dd2cfa7e4d457f0716f238a27e179 --- /dev/null +++ b/advanced-database-applications @@ -0,0 +1 @@ +Subproject commit c1ef95bf152dd2cfa7e4d457f0716f238a27e179 diff --git "a/\351\273\204\345\256\201/20240904/111.md" "b/\351\273\204\345\256\201/20240904/111.md" new file mode 100644 index 0000000000000000000000000000000000000000..2432701a13a189a74b3a9b0872037e4f88a8150a --- /dev/null +++ "b/\351\273\204\345\256\201/20240904/111.md" @@ -0,0 +1 @@ +SELECT * FROM s \ No newline at end of file diff --git "a/\351\273\204\345\256\201/20240904/cmd.png" "b/\351\273\204\345\256\201/20240904/cmd.png" new file mode 100644 index 0000000000000000000000000000000000000000..36f9222b2f12b3c0b88c6e6c8ca834252dce7cee Binary files /dev/null and "b/\351\273\204\345\256\201/20240904/cmd.png" differ diff --git "a/\351\273\204\345\256\201/20240904/homewoke.sql" "b/\351\273\204\345\256\201/20240904/homewoke.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f69ce73d8d00251775b8ebaa06cede4d16e2ec85 --- /dev/null +++ "b/\351\273\204\345\256\201/20240904/homewoke.sql" @@ -0,0 +1,258 @@ +/*==============================================================*/ +/* DBMS name: Sybase SQL Anywhere 12 */ +/* Created on: 2024/9/4 23:30:41 */ +/*==============================================================*/ + + +if exists(select 1 from sys.sysforeignkey where role='FK_LINK1_LINK1_S') then + alter table link1 + delete foreign key FK_LINK1_LINK1_S +end if; + +if exists(select 1 from sys.sysforeignkey where role='FK_LINK1_LINK2_C') then + alter table link1 + delete foreign key FK_LINK1_LINK2_C +end if; + +if exists(select 1 from sys.sysforeignkey where role='FK_LINK2_LINK3_S') then + alter table link2 + delete foreign key FK_LINK2_LINK3_S +end if; + +if exists(select 1 from sys.sysforeignkey where role='FK_LINK2_LINK4_G') then + alter table link2 + delete foreign key FK_LINK2_LINK4_G +end if; + +if exists(select 1 from sys.sysforeignkey where role='FK_LINK3_LINK5_C') then + alter table link3 + delete foreign key FK_LINK3_LINK5_C +end if; + +if exists(select 1 from sys.sysforeignkey where role='FK_LINK3_LINK6_G') then + alter table link3 + delete foreign key FK_LINK3_LINK6_G +end if; + +drop index if exists c.c_PK; + +drop table if exists c; + +drop index if exists g.g_PK; + +drop table if exists g; + +drop index if exists link1.link2_FK; + +drop index if exists link1.link1_FK; + +drop index if exists link1.link1_PK; + +drop table if exists link1; + +drop index if exists link2.link4_FK; + +drop index if exists link2.link3_FK; + +drop index if exists link2.link2_PK; + +drop table if exists link2; + +drop index if exists link3.link6_FK; + +drop index if exists link3.link5_FK; + +drop index if exists link3.link3_PK; + +drop table if exists link3; + +drop index if exists s.s_PK; + +drop table if exists s; + +/*==============================================================*/ +/* Table: c */ +/*==============================================================*/ +create table c +( + cid integer not null, + cname varchar(255) null, + constraint PK_C primary key (cid) +); + +/*==============================================================*/ +/* Index: c_PK */ +/*==============================================================*/ +create unique index c_PK on c ( +cid ASC +); + +/*==============================================================*/ +/* Table: g */ +/*==============================================================*/ +create table g +( + gid integer not null, + gname varchar(255) null, + gihone integer null, + constraint PK_G primary key (gid) +); + +/*==============================================================*/ +/* Index: g_PK */ +/*==============================================================*/ +create unique index g_PK on g ( +gid ASC +); + +/*==============================================================*/ +/* Table: link1 */ +/*==============================================================*/ +create table link1 +( + sid integer not null, + cid integer not null, + constraint PK_LINK1 primary key clustered (sid, cid) +); + +/*==============================================================*/ +/* Index: link1_PK */ +/*==============================================================*/ +create unique clustered index link1_PK on link1 ( +sid ASC, +cid ASC +); + +/*==============================================================*/ +/* Index: link1_FK */ +/*==============================================================*/ +create index link1_FK on link1 ( +sid ASC +); + +/*==============================================================*/ +/* Index: link2_FK */ +/*==============================================================*/ +create index link2_FK on link1 ( +cid ASC +); + +/*==============================================================*/ +/* Table: link2 */ +/*==============================================================*/ +create table link2 +( + sid integer not null, + gid integer not null, + constraint PK_LINK2 primary key clustered (sid, gid) +); + +/*==============================================================*/ +/* Index: link2_PK */ +/*==============================================================*/ +create unique clustered index link2_PK on link2 ( +sid ASC, +gid ASC +); + +/*==============================================================*/ +/* Index: link3_FK */ +/*==============================================================*/ +create index link3_FK on link2 ( +sid ASC +); + +/*==============================================================*/ +/* Index: link4_FK */ +/*==============================================================*/ +create index link4_FK on link2 ( +gid ASC +); + +/*==============================================================*/ +/* Table: link3 */ +/*==============================================================*/ +create table link3 +( + cid integer not null, + gid integer not null, + constraint PK_LINK3 primary key clustered (cid, gid) +); + +/*==============================================================*/ +/* Index: link3_PK */ +/*==============================================================*/ +create unique clustered index link3_PK on link3 ( +cid ASC, +gid ASC +); + +/*==============================================================*/ +/* Index: link5_FK */ +/*==============================================================*/ +create index link5_FK on link3 ( +cid ASC +); + +/*==============================================================*/ +/* Index: link6_FK */ +/*==============================================================*/ +create index link6_FK on link3 ( +gid ASC +); + +/*==============================================================*/ +/* Table: s */ +/*==============================================================*/ +create table s +( + sid integer not null, + sname varchar(255) null, + constraint PK_S primary key (sid) +); + +/*==============================================================*/ +/* Index: s_PK */ +/*==============================================================*/ +create unique index s_PK on s ( +sid ASC +); + +alter table link1 + add constraint FK_LINK1_LINK1_S foreign key (sid) + references s (sid) + on update restrict + on delete restrict; + +alter table link1 + add constraint FK_LINK1_LINK2_C foreign key (cid) + references c (cid) + on update restrict + on delete restrict; + +alter table link2 + add constraint FK_LINK2_LINK3_S foreign key (sid) + references s (sid) + on update restrict + on delete restrict; + +alter table link2 + add constraint FK_LINK2_LINK4_G foreign key (gid) + references g (gid) + on update restrict + on delete restrict; + +alter table link3 + add constraint FK_LINK3_LINK5_C foreign key (cid) + references c (cid) + on update restrict + on delete restrict; + +alter table link3 + add constraint FK_LINK3_LINK6_G foreign key (gid) + references g (gid) + on update restrict + on delete restrict; + + +SELECT * FROM s \ No newline at end of file diff --git "a/\351\273\204\345\256\201/20240904/lmd.png" "b/\351\273\204\345\256\201/20240904/lmd.png" new file mode 100644 index 0000000000000000000000000000000000000000..2aea8d87d8d063b146fc14220cf8ed3584bc75cb Binary files /dev/null and "b/\351\273\204\345\256\201/20240904/lmd.png" differ diff --git "a/\351\273\204\345\256\201/20240904/pmd.png" "b/\351\273\204\345\256\201/20240904/pmd.png" new file mode 100644 index 0000000000000000000000000000000000000000..6a5bc5389cf2a38bae2e3d6f8a3c1065e64b57a6 Binary files /dev/null and "b/\351\273\204\345\256\201/20240904/pmd.png" differ diff --git "a/\351\273\204\345\256\201/20240904/\347\254\224\350\256\260.md" "b/\351\273\204\345\256\201/20240904/\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..37240255f416eeb748b424d0a472f5d28986fea0 --- /dev/null +++ "b/\351\273\204\345\256\201/20240904/\347\254\224\350\256\260.md" @@ -0,0 +1,33 @@ +# 数据库高级应用第一课笔记 + +## 表与表的关系: + +### 1.一对多: + +​ 一个记录在表A中可以对应多个记录在表B中,但表B中的每个记录只能对应表A中的一个记录 + +### 2.一对一: + +​ 一个记录在表A中只对应一个记录在表B中,反之亦然。 + +### 3.多对多: + +​ 表A中的一个记录可以对应表B中的多个记录,而表B中的一个记录也可以对应表A中的多个记录,通常需要通过一个关联表来实现 + +## 设计数据库的约束(范式): + +1. **第一范式(1NF)**: + - 数据表中的每一列都必须是原子的,即列中的每个值都是不可再分的基本数据项。 + - 例如,一个表中的某一列不能包含多个值或集合。 +2. **第二范式(2NF)**: + - 在满足第一范式的基础上,要求每个非主属性完全函数依赖于主键,即每个非主属性都必须依赖于整个主键(而不是主键的一部分)。 + - 例如,如果主键是复合主键,那么所有非主属性必须依赖于主键的所有部分。 +3. **第三范式(3NF)**: + - 在满足第二范式的基础上,要求每个非主属性直接依赖于主键,而不是依赖于其他非主属性。 + - 也就是说,消除传递依赖。例如,若A -> B和B -> C,那么应有A -> C,以消除B的依赖关系。 + + + + + + \ No newline at end of file