Understanding advanced SQL features

Primary key:

1. No two rows may have the same primary key value;

2. Every row must have a primary key value(NULL is not allowed)

3. The column containing primary key values can never be modified or updated

4. Primary key value can never be reused. If a row is deleted from the table, its primary key must not be assigned to any new rows.

Foreign Key:

A foreign key is a column in a table whose values must be listed in a primary key in another table.

1. Foreign keys can help prevent accidental deletion.

2. After a foreign key is defined, the database will not allow the deletion of rows that have related rows in other tables.

3. Some database(ACCESS for example) support a feature called cascading delete. If enabled, this feature deletes all related data when a row is deleted from a table.

Unique Constraints:

1. A table can contain multiple unique constraints, but only one primary key is allowed is allowed per table.

2. Unique constraint columns can contain NULL

3. Unique constrain column can be modified or updated

4. Unique constrain column values can be reused

5. Unlike primary keys, unique constraints cannot be used to define foreign keys

Check Constraints:

Check constaints are used to ensure that data in a column meets a set of criteria that you specify.

1. Checking minimun or maximum values - e.g. preventing an order of zero items

2. Specifying ranges - e.g. making sure that a ship date is greater than or equal to today's date and not greater than a year from now

3. Allowing only specific values - e.g. allowing only M or F in a gender field

Index

Indexes are used to sort data logically to improve the speed of searching and sorting operations. The fact that makes index work is the data is sorted correctly.

Trigger

Triggers are special stored procedures taht are executed automatically when a specified database activity occurs. Triggers might be associated with INSERT, UPDATE, and DELETE operations(or combination thereof) on sepcific tables.

Unlike stores procedures(which are simple SQL statements), triggers are tied to individual tables. E.g.  A trigger associated with INSERT operation on Orders table will be executed only when a row is inserted into the Orders table.

Example:

CREATE TRIGGER Customer_state

ON Customers

FOR INSERT, UPDATE

AS

UPDATE Customers

SET cust_state = UPPER(cust_state)

WHERE Customers.cust_id = inserted.cust_id;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值