SQL - RDBMS Concepts
SQL - RDBMS Concepts
What is a Table?
The data in an RDBMS is stored in database objects known
as tables. This table is basically a collection of related data entries
and it consists of numerous columns and rows.
What is a Field?
Every table is broken up into smaller entities called fields. A field
is a column in a table that is designed to maintain specific
information about every record in the table.
For example, our CUSTOMERS table consists of different fields
like ID, Name, Age, Salary, City and Country.
What is a Column?
A column is a vertical entity in a table that contains all
information associated with a specific field in a table.
SQL Constraints
Constraints are the rules enforced on data columns on a table.
These are used to limit the type of data that can go into a table.
This ensures the accuracy and reliability of the data in the
database.
S.No. Constraints
DEFAULT Constraint
2
Provides a default value for a column when none is specified.
UNIQUE Key
3
Ensures that all the values in a column are different.
PRIMARY Key
4
Uniquely identifies each row/record in a database table.
FOREIGN Key
5
Uniquely identifies a row/record in any another database table.
CHECK Constraint
6
Ensures that all values in a column satisfy certain conditions.
INDEX Constraint
7
Used to create and retrieve data from the database very quickly.
Data Integrity
The following categories of data integrity exist with each RDBMS
−
Database Normalization
Database normalization is the process of efficiently organizing
data in a database. There are two reasons of this normalization
process −
Both these reasons are worthy goals as they reduce the amount
of space a database consumes and ensures that data is logically
stored. Normalization consists of a series of guidelines that help
guide you in creating a good database structure.
CUSTOMERS Table
ID Name Age Salary
1 Ramesh 32 2000.00
2 Mukesh 40 5000.00
3 Sumit 45 4500.00
4 Kaushik 25 2500.00
CUSTOMERS_ADDRESS Table
ID City Country
1 Hyderabad India
1 Delhi India
3 Muscat Oman
4 Kolkata India
This table is not in first normal form because we have City column
repeated two times and you can see some problems in the
current table. This table always reserves space on the disk for
two cities, whether the person stays in two cities or not.
To eliminate the repeating columns and bring the table to the first
normal form, separate the table into two tables. Put the repeating
columns into one of the tables as below −
CUSTOMERS Table
ID Name Age Salary
1 Ramesh 32 2000.00
2 Mukesh 40 5000.00
3 Sumit 45 4500.00
4 Kaushik 25 2500.00
CUSTOMERS_ADDRESS Table
ID City Country
1 Hyderabad India
1 Delhi India
3 Muscat Oman
4 Kolkata India
The Second Normal Form states that it should meet all the rules
for 1NF and there must be no partial dependences of any of the
columns on the primary key −
This table is in the first normal form; in that it obeys all the rules
of the first normal form. In this table, the primary key consists of
the CUST_ID and the ORDER_ID. Combined, they are unique
assuming the same customer would hardly order the same thing.
However, the table is not in the second normal form because
there are partial dependencies of primary keys and columns.
CUST_NAME is dependent on CUST_ID and there's no real link
between a customer's name and what he purchased. The order
detail and purchase date are also dependent on the ORDER_ID,
but they are not dependent on the CUST_ID, because there is no
link between a CUST_ID and an ORDER_DETAIL or their
SALE_DATE.
To make this table comply with the second normal form, you
need to separate the columns into three tables.
Finally, create a third table storing just the CUST_ID and the
ORDER_ID to keep a track of all the orders for a customer −
The dependency between the zip code and the address is called
as a transitive dependency. To comply with the third normal
form, all you need to do is to move the Street, City and the State
fields into their own table, which you can call as the Zip Code
table. −
For example, if the address and the zip code data were stored in
three or four different tables, then any changes in the zip codes
would need to ripple out to every record in those three or four
tables.