SQL Data Types
SQL Data Types
SQL Data Types
1. Binary Datatypes :
There are four subtypes of this datatype which are given below :
SQL | Constraints
Constraints are the rules that we can apply on the type of data in a table.
That is, we can specify the limit on the type of data that can be stored in a
particular column in a table using constraints.
The available constraints in SQL are:
NOT NULL: This constraint tells that we cannot store a null value in a
column. That is, if a column is specified as NOT NULL then we will not be
able to store null in this particular column any more.
UNIQUE: This constraint when specified with a column, tells that all the
values in the column must be unique. That is, the values in any row of a
column must not be repeated.
PRIMARY KEY: A primary key is a field which can uniquely identify each
row in a table. And this constraint is used to specify a field in a table as
primary key.
FOREIGN KEY: A Foreign key is a field which can uniquely identify each
row in a another table. And this constraint is used to specify a field as
Foreign key.
CHECK: This constraint helps to validate the values of a column to meet
a particular condition. That is, it helps to ensure that the value stored in
a column meets a specific condition.
DEFAULT: This constraint specifies a default value for the column when
no value is specified by the user.
How to specify constraints?
We can specify constraints at the time of creating the table using CREATE
TABLE statement. We can also specify the constraints after creating a table
using ALTER TABLE statement.
Syntax:
Below is the syntax to create constraints using CREATE TABLE statement at
the time of creating the table.
1. NOT NULL –
If we specify a field in a table to be NOT NULL. Then the field will never
accept null value. That is, you will be not allowed to insert a new row in the
table without specifying any value to this field.
For example, the below query creates a table Student with the fields ID and
NAME as NOT NULL. That is, we are bound to specify values for these two
fields every time we wish to insert a new row.
Orders
O_IDORDER_NOC_ID
1 2253 3
2 3325 3
3 4521 2
4 8532 1
Customers
C_IDNAME ADDRESS
1 RAMESH DELHI
2 SURESH NOIDA
3 DHARMESHGURGAON
As we can see clearly that the field C_ID in Orders table is the primary key
in Customers table, i.e. it uniquely identifies each row in the Customers
table. Therefore, it is a Foreign Key in Orders table.
Syntax: