SQL Constraints
SQL Constraints
Constraints are the rules enforced on data columns on 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. Constraints could be column level or table
level. Column level constraints are applied only to one column, whereas table level constraints are applied to the whole
table.
Following are commonly used constraints available in SQL:
NOT NULL Constraint: Ensures that a column cannot have NULL value.
DEFAULT Constraint: Provides a default value for a column when none is specified.
UNIQUE Constraint: Ensures that all values in a column are different.
PRIMARY Key: Uniquely identified each rows/records in a database table.
FOREIGN Key: Uniquely identified a rows/records in any another database table.
CHECK Constraint: The CHECK constraint ensures that all values in a column satisfy certain conditions.
INDEX: Use to create and retrieve data from the database very quickly.
NOT NULL Constraint:
By default, a column can hold NULL values. If you do not want a column to have a NULL value, then you need to define
such constraint on this column specifying that NULL is now not allowed for that column.
A NULL is not the same as no data, rather, it represents unknown data.
For example, the following SQL creates a new table called CUSTOMERS and adds five columns, three of which, ID and
NAME and AGE, specify not to accept NULLs:
DEFAULT Constraint:
The DEFAULT constraint provides a default value to a column when the INSERT INTO statement does not provide a
specific value.
For example, the following SQL creates a new table called CUSTOMERS and adds five columns. Here, SALARY column is
set to 5000.00 by default, so in case INSERT INTO statement does not provide a value for this column. then by default
this column would be set to 5000.00.
UNIQUE Constraint:
The UNIQUE Constraint prevents two records from having identical values in a particular column. In the CUSTOMERS
table, for example, you might want to prevent two or more people from having identical age.
PRIMARY Key:
A primary key is a field in a table which uniquely identifies each row/record in a database table. Primary keys must
contain unique values. A primary key column cannot have NULL values.
A table can have only one primary key, which may consist of single or multiple fields. When multiple fields are used as a
primary key, they are called a composite key.
If a table has a primary key defined on any field(s), then you can not have two records having the same value of that
field(s).
Note: You would use these concepts while creating database tables.
Create Primary Key:
Here is the syntax to define ID attribute as a primary key in a CUSTOMERS table.
For defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax:
FOREIGN Key:
A foreign key is a key used to link two tables together. This is sometimes called a referencing key.
Foreign Key is a column or a combination of columns whose values match a Primary Key in a different table.
The relationship between 2 tables matches the Primary Key in one of the tables with a Foreign Key in the second
table.
If a table has a primary key defined on any field(s), then you can not have two records having the same value of that
field(s).
CHECK Constraint:
The CHECK Constraint enables a condition to check the value being entered into a record. If the condition evaluates to
false, the record violates the constraint and isn’t entered into the table.
For example, the following SQL creates a new table called CUSTOMERS and adds five columns. Here, we add a CHECK
with AGE column, so that you can not have any CUSTOMER below 18 years:
INDEX:
The INDEX is used to create and retrieve data from the database very quickly. Index can be created by using single or
group of columns in a table. When index is created, it is assigned a ROWID for each row before it sorts out the data.
Proper indexes are good for performance in large databases, but you need to be careful while creating index. Selection
of fields depends on what you are using in your SQL queries.
For example, the following SQL creates a new table called CUSTOMERS and adds five columns:
Now, you can create index on single or multiple columns using the following syntax:
To create an INDEX on AGE column, to optimize the search on customers for a particular age, following is the SQL syntax:
Database Normalization
Database normalization is the process of efficiently organizing data in a database. There are two reasons of the
normalization process:
Eliminating redundant data, for example, storing the same data in more than one table.
Ensuring data dependencies make sense.
Both of these are worthy goals as they reduce the amount of space a database consumes and ensure that data is
logically stored. Normalization consists of a series of guidelines that help guide you in creating a good database
structure.
Normalization guidelines are divided into normal forms; think of form as the format or the way a database structure is
laid out. The aim of normal forms is to organize the database structure so that it complies with the rules of first normal
form, then second normal form, and finally third normal form.
It's your choice to take it further and go to fourth normal form, fifth normal form, and so on, but generally speaking,
third normal form is enough.
First Normal Form (1NF) Second Normal Form (2NF) Third Normal Form (3NF)
First Normal Form
First normal form (1NF) sets the very basic rules for an organized database:
Define the data items required, because they become the columns in a table. Place related data items in a table.
Ensure that there are no repeating groups of data.
Ensure that there is a primary key.
First Rule of 1NF:
You must define the data items. This means looking at the data to be stored, organizing the data into columns, defining
what type of data each column contains, and finally putting related columns into their own table.
For example, you put all the columns relating to locations of meetings in the Location table, those relating to members
in the MemberDetails table, and so on.
Second Rule of 1NF:
The next step is ensuring that there are no repeating groups of data. Consider we have the following table:
So if we populate this table for a single customer having multiple orders, then it would be something as follows:
But as per 1NF, we need to ensure that there are no repeating groups of data. So let us break above table into two parts
and join them using a key as follows:
This table is in first normal form, in that it obeys all the rules of first normal form. In this table, the primary key consists
of CUST_ID and ORDER_ID. Combined, they are unique assuming same customer would hardly order same thing.
However, the table is not in 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.
Order detail and purchase date are also dependent on ORDER_ID, but they are not dependent on CUST_ID, because
there's no link between a CUST_ID and an ORDER_DETAIL or their SALE_DATE.
To make this table comply with second normal form, you need to separate the columns into three tables.
First, create a table to store the customer details as follows:
Finally, create a third table storing just CUST_ID and ORDER_ID to keep track of all the orders for a customer:
Third Normal Form
A table is in third normal form when the following conditions are met:
It is in second normal form.
All nonprimary fields are dependent on the primary key.
The dependency of nonprimary fields is between the data. For example, in the below table, street name, city, and state
are unbreakably bound to the zip code.
The dependency between zip code and address is called a transitive dependency. To comply with third normal form, all
you need to do is move the Street, City, and State fields into their own table, which you can call the Zip Code table:
The advantages of removing transitive dependencies are mainly twofold. First, the amount of data duplication is
reduced and therefore your database becomes smaller.
The second advantage is data integrity. When duplicated data changes, there's a big risk of updating only some of the
data, especially if it's spread out in a number of different places in the database. For example, if address and zip code
data were stored in three or four different tables, then any changes in zip codes would need to ripple out to every
record in those three or four tables.
SQL Syntax
Important point to be noted is that SQL is case insensitive, which means SELECT and select have same meaning in SQL
statements, but MySQL makes difference in table names. So if you are working with MySQL, then you need to give
table names as they exist in the database.
\
SQL Operators
An operator is a reserved word or a character used primarily in an SQL statement's WHERE clause to perform
operation(s), such as comparisons and arithmetic operations.
Operators are used to specify conditions in an SQL statement and to serve as conjunctions for multiple conditions in a
statement.
Arithmetic operators
Comparison operators
Logical operators
Operators used to negate conditions
SQL Expressions
An expression is a combination of one or more values, operators, and SQL functions that evaluate to a value.
SQL EXPRESSIONs are like formulas and they are written in query language. You can also use them to query the
database for specific set of data.
There are different types of SQL expressions, which are mentioned below:
Here numerical_expression is used for mathematical expression or any formula. Following is a simple examples showing
usage of SQL Numeric Expressions:
There are several built-in functions like avg(), sum(), count(), etc., to perform what is known as aggregate data
calculations against a table or a specific table column.
SQL INSERT Query
The SQL INSERT INTO Statement is used to add new rows of data to a table in the database.
Here, column1, column2,...columnN are the names of the columns in the table into which you want to insert data.
You may not need to specify the column(s) name in the SQL query if you are adding values for all the columns of the
table. But make sure the order of the values is in the same order as the columns in the table. The SQL INSERT INTO
syntax would be as follows:
You can create a record in CUSTOMERS table using second syntax as follows:
All the above statements would produce the following records in CUSTOMERS table:
If this is the use case of the ball game that you have created, create tables and columns using SQL. Define all necessary
details like data types and constraints. Then add two records for each created tables also by using SQL (Insert
command).
Login
Select level
View scores
stat