Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Keys and Constraints

Similar presentations


Presentation on theme: "Database Keys and Constraints"— Presentation transcript:

1 Database Keys and Constraints

2 What? A database is for storing information.
It is comprised of tables, views and other entities. Each entity has “columns”, and can store “rows” of data.

3 Constraints For maintaining data integrity. Yup.

4 Constraints Primary Keys Unique Keys Foreign Keys

5 Primary Keys Sequences AUTO_INCREMENT in MySQL. IDENTITY in MSSQL.
Sequences (independent entities, not column attributes) in PostgreSQL, Oracle. id INT(11) title VARCHAR(255) content TEXT slug

6 Primary Keys Natural Keys email VARCHAR(255) interests TEXT location
likes_plums TINYINT(1)

7 Primary Keys Natural Keys lang_code VARCHAR(100) fte_app_id INT(11)
text TEXT likes_apricots TINYINT(1)

8 Unique Keys Forces, well... uniqueness across all rows in a table. id
INT(11) VARCHAR(255) location likes_anchovies TINYINT(1)

9 Foreign Keys “Relationships”. id INT(11) email VARCHAR(255) location
likes_anchovies TINYINT(1) id INT(11) user_id title VARCHAR(255) comment TEXT

10 Foreign Keys Enforces integrity at all times; no comments record can exist without a corresponding user record. id INT(11) VARCHAR(255) location likes_anchovies TINYINT(1) id INT(11) user_id title VARCHAR(255) comment TEXT

11 Foreign Keys When deleting and updating a “parent” table, we can choose how “child” rows will react. CASCADE RESTRICT SET NULL SET DEFAULT (Broken in MySQL)

12 Why Foreign Keys? Consistency.
No broken relationships, eg. child rows without parents. Putting these checks in the database ensure that, no matter what tool is being used, or if that tool is buggy or not, consistency is maintained.

13 Why Foreign Keys? Discoverability That's not even a word, man.
Define the relationship in the DB, and tools other than VanillaCMS can use the relationships. eg. other PHP apps, general MySQL clients, RedMine, RecordFerry, etc. At present, you either need to include the entirety of the CMS to discover the relationships, or try to break out the code that handles them.

14 Downsides No “God Tables”
(“Generic” Tables that store many types of things with many types of column.) ie. gen_content_item has: ncci_id ncci_class (type) ncci_flag1 ncci_flag2 ncci_text1 ncci_text2 ncci_fkey1

15 Downsides There's no way to enforce any sort of constraints on something that will have different rules depending on the value of “ncci_class”. No conditional foreign keys. ncci_varchar1 may be an address today, or someone's lunch-order tomorrow.

16

17 Soft-Deletion Mark a row as “deleted” without removing it from its table. … And therefore keeping relationships intact, and a discoverable record of activity. Retrieve active records (by default) with “deleted = 0”. id INT(11) VARCHAR(255) location deleted INT(11) DEFAULT 0

18 Soft-Deletion Example
PRIMARY id location deleted 1 Down the side of the couch. 2 Neither here nor there.

19 Soft-Deletion Example
PRIMARY id location deleted 1 Down the side of the couch. 2 Neither here nor there.

20 Soft-Deletion Example
PRIMARY id location deleted 1 Down the side of the couch. 2 Neither here nor there.

21 Soft-Deletion + Uniqueness
Bring our uniqueness constraint back in. How do we keep addresses unique when we have soft-deleted records hanging around? id INT(11) VARCHAR(255) location deleted INT(11) DEFAULT 0

22 Soft-Deletion Example
PRIMARY UNIQUE id location deleted 1 Down the side of the couch. 2 Neither here nor there.

23 Soft-Deletion Example
PRIMARY UNIQUE id location deleted 1 Down the side of the couch. 2 Neither here nor there. 3 Over here. 4 Over there.

24 Soft-Deletion + Uniqueness
Put the uniqueness constraint on and deleted. id INT(11) VARCHAR(255) location deleted INT(11) DEFAULT 0

25 Soft-Deletion + Uniqueness
When soft-deleting a record, copy its ID to the deleted field. Any “deleted = 0” queries will not pick it up. Only one “active” record allowed with that address at one time. id INT(11) VARCHAR(255) location deleted INT(11) DEFAULT 0

26 Soft-Deletion Example
PRIMARY UNIQUE id location deleted 1 Down the side of the couch. 2 Neither here nor there.

27 Soft-Deletion Example
PRIMARY UNIQUE id location deleted 1 Down the side of the couch. 2 Neither here nor there.

28 Soft-Deletion Example
PRIMARY UNIQUE id location deleted 1 Down the side of the couch. 2 Neither here nor there.

29 Soft-Deletion Example
PRIMARY UNIQUE id location deleted 1 Down the side of the couch. 2 Neither here nor there. 3 Over here.

30 Soft-Deletion Example
PRIMARY UNIQUE id location deleted 1 Down the side of the couch. 2 Neither here nor there. 3 Over here. 4 Over there.

31 Gooooooo Planet! Users with auto-incrementing ID.
Unique addresses. Comments that are always associated with a user. Records of deleted users and comments. id INT(11) VARCHAR(255) location deleted INT(11) DEFAULT 0 id INT(11) user_id comment TEXT deleted INT(11) DEFAULT 0

32 NO UNICORNS TODAY


Download ppt "Database Keys and Constraints"

Similar presentations


Ads by Google