Page 20 - Modular_V2.0_SQL_Flipbook
P. 20
For example,
CREATE TABLE Students
(
StudentID INTEGER, FirstName CHAR(30), LastName CHAR(30) NULL, Marks INT
);
Now, the LastName field can accept NULL values.
NOT NULL Constraint
The NOT NULL constraint specifies that a column cannot accept NULL values. You cannot insert a
new record, or update a record without specifying a value to this column. For example,
CREATE TABLE Students
(
StudentID INTEGER NOT NULL, FirstName CHAR(30), LastName CHAR(30), Marks INT
);
Now, the StudentID column cannot accept NULL values.
Primary Key Constraint
Each record in a table is uniquely identified by the PRIMARY KEY constraint. Primary key must have
unique values and can not contains NULL values. A table can only have one primary key, which can
be composed of single or multiple columns (fields). For example, Here, the value of the StudentID
column is a unique identifier for a row. Similarly, it cannot store NULL value and must be UNIQUE.
CREATE TABLE Students
(
StudentID INTEGER PRIMARY KEY, FirstName CHAR(30), LastName CHAR(30), Marks
INT
);
Foreign Key Constraint
The FOREIGN KEY constraint is used to establish a relationship between two tables. It ensures that
the value in the referencing table column matches a value in the referenced table’s primary key
column. The referenced table is the one that has the main or original data, and the referencing table
is the one that uses this data.
For example, the following SQL command creates a Courses table where StudentID is a foreign key
referencing the Students table:
CREATE TABLE Students
(
StudentID INTEGER PRIMARY KEY, FirstName CHAR(30), LastName CHAR(30),
Marks INT
);
CREATE TABLE Courses
(
18
Touchpad MODULAR (Ver. 2.0)

