数据库系统复习整理(个人向)

Superkey

Definition:
A superkey of a relation is a set of attributes whose values can uniquely identify every potential tuple of the relation.
关系的超级键是一组属性,这些属性的值可以唯一地标识关系的每个元组。

Some claims:

Every relation has at least one superkey.
Any superset of a superkey is a superkey. //含有超级键的集合也是超级键
From a given state of a relation, we may determine whether a set of attributes of the relation does not form a superkey, but we can not determine if a set of attributes forms a superkey.

【Superkey & key】e.g.
For relation Students (ID, Name, Home_Address, Birthday, GPA)
ID is a key.
{ID, Name} is a superkey but not a key.
{Name, Home_Address, Birthdate} is also a key.

Candidate Key】e.g.
Find all possible candidate keys for the following relation based on its current tuples.

A B C D
a1 b1 c1 d1
a1 b2 c2 d1
a2 b2 c1 d1
a2 b1 c2 d1

Answer:
{A,B}, {A,C}, {B,C}

Primary key
A primary key of a relation is the candidate key chosen by the database designer for a particular application.

  • The primary key of each relation is chosen and declared at the time when the relation is defined. Once chosen, it cannot be changed.
  • The primary key is usually chosen to be the candidate key that has the smallest number of attributes to improve both storage efficiency and query processing efficiency.
  • With the primary key defined, only the values under the attributes in the primary key need to be checked for identifying duplicate when new tuples are inserted (index is often used).
  • The primary key of a relation is often used in references from other relations.

Foreign Key
A set of attributes FK of relation R1 is a foreign key in R1 if it satisfies the following two conditions:

  • There is a relation R2 with the primary key PK such that FK and PK have the same number of attributes with compatible domains.
  • For any tuple t1 in R1, either there exists a tuple t2 in R2 such that t1[FK] = t2[PK] or t1[FK] is null.

Foreign Key】e.g.

Table Employees

EID Name Age Dept-Name
123456789 John 45 Sales
234567891 Mary 42 Service
2345678912 Bob 39 null

Table Departments

Name Location Manager
Sales Shanghai Bill
HR Shenzhen Charles
Service Beijing Maria

Dept_Name of Employees is a foreign key referencing Name of Departments

  • Foreign keys need to be explicitly defined.//需要显式定义外键
  • R1 and R2 in the definition could be the same relation, for e.g.
    Employees(ID, Age, Salary, Position, Manager_ID).
    The primary key in the definition may be replaced by a candidate key.

约束
静态约束:必须始终保持在数据项之间。
e.g.

credits taken each semester of a student must be less than 24
employee.salary < manager.salary
     IN  employee, manager
    WHERE employee.mgr# = manager.mgr#

学生每学期的学分必须少于24学分。
employee.salary<manager.salary
员工、经理
其中employee.mgr=manager.mgr

动态约束:必须在关系更改前后的数据库状态之间保留
每个学生的总学分不得在连续学期内减少。
完整性约束可能与几个表相关。

数据定义语言(DDL)
CREATE
used to create new objects (tables, views, etc.) in the database
ALTER
used to change the structure of an existing object
DROP
used to remove the object from the database, (all its data plus any reference to it in the data dictionary)

E.G.
(1) CREATE

CREATE TABLE emp
(
    empno         NUMBER NOT NULL,
    ename         CHAR(10) ,
    job           CHAR(9),
    mgr           NUMBER (4),
    hiredate      DATE ,
    sal           NUMBER(10,2),
    comm          NUMBER(9,0) ,
    deptno        NUMBER(4) NOT NULL
);

e.g. ADD Some constrains

CREATE TABLE emp
(
  empno     NUMBER NOT NULL primary key,
  ename     CHAR(10) NOT NULL CHECK (ename = UPPER (ename))
  job       CHAR(9),
  mgr       NUMBER REFERENCES emp(empno),
  hiredate  DATE CHECK (hiredate <=SYSDATE),
  sal       NUMBER(10,2)CHECK sal > 500,
  comm      NUMBER(9,0) DEFAULT NULL,
  deptno    NUMBER(2) NOT NULL REFERENCES dept(deptno)
);

(2) ALTER

  • To add a new column
    ALTER TABLE <tablename>ADD (column_name datatype);
ALTER TABLE emp ADD (gender CHAR(1));
  • To change the definition of an existing column.
    ALTER TABLE <tablename> MODIFY (column_name datatype);
 ALTER TABLE emp  MODIFY (deptno NUMBER(6));
  • To drop a column
    ALTER TABLE <tablename> DROP (column_name)
ALTER TABLE student DROP Address;

(3) DROP
If we want to permanently remove the student table that we created:

DROP TABLE student

DML modifying contents

INSERT
allows insertion of records into a table
UPDATE
updates existing rows in a table
DELETE
removes unwanted rows from a table
E.G.
(1)INSERT

INSERT INTO emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
VALUES(7500, 'CAMPBELL', 'ANALYST', 7566, '30-OCT-1992', 24500,0, 40);  

(2)UPDATE

1)  
UPDATE emp SET comm = 0;
To give a 15% raise to all Analysts and Clerks in department 20 could use;
UPDATE emp
SET sal = sal* 1.15
WHERE (job = 'ANALYST' OR job = 'CLERK') AND deptno = 20;

(3)DELETE

  • To remove from EMP all sales staff who made less than 100 commission last month enter:
DELETE FROM emp
WHERE job = 'SALESMAN'
AND comm < 100;
  • To delete everything in a table :
DELETE FROM <tablename>;

数据库范式:
1NF(First Normal Form):当且仅当所有域只包含原子值,即每个分量都是不可再分的数据项,则称实体E满足第一范式

2NF(Second Normal Form):当且仅当实体E满足第一范式,且每一个非键属性完全依赖主键时,满足第二范式

3NF(Third Normal Form):当且仅当实体E是第二范式(2NF),且E中没有非主属性传递依赖时,满足第三范式

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,053评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,527评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,779评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,685评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,699评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,609评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,989评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,654评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,890评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,634评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,716评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,394评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,976评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,950评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,191评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,849评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,458评论 2 342

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,281评论 0 10
  • 光秃秃的枝条拍打着寒冷 冬日残阳苍白地挂在天上 碎了一地的心 再也捡不起 这到底是个怎样的世界? 真真假假,假假真...
    芳草兮阅读 276评论 8 8
  • 布谷鸟的啼鸣会驱走冬日的严寒,带来春日的温暖与生机;和熙的春风也终将会把夏日的艳阳升起,带来令人难以忍受的酷暑...
    吃蔬菜的狮子阅读 287评论 0 0
  • 霏霏阴雨,连月不开。 我和弟弟吵架了。他是姑姑家的儿子,即表弟。有人说,外面的兄弟不比自家的亲,我不同意。自家兄弟...
    玩笑_2b58阅读 545评论 1 2
  • 你,眼睛里装满了糖,甜蜜地望着我很是期待我说出那三个字那三个只是汉字啊中华文化博大精深,而它只是无限大分之一我,身...
    寡言者平阅读 479评论 10 7