DBMS
DBMS
A primary key is a field in a database table that uniquely identifies each row/record.
This is also one type of Integrity Constraint. Primary keys must have distinct values.
A table can only have one primary key, which can be made up of one or more fields.
It creates a composite key when several fields are used as a primary key
Syntax:
1
Column_name2 datatype,
.....
Column_namendatatype,
Code
The image depicted below demonstrates the table created with id, name, marks, and grade
fields.
Input:
Output:
2
Explanation:
By using the above query, it will create the table named student with appropriate fields.
Id belongs to the not-null constraints, the name belongs to varchar datatype, marks
belong to varchar datatype, and finally, the id field is set to primary key constraints.
Foreign key:
Foreign keys help ensure the consistency of your data while providing some ease.
This is also a type of integrity constraint. You are responsible for keeping track of inter-
table dependencies and preserving their consistency from within your applications if
you don't use international keys.
In certain situations, doing so isn't even that difficult. It’s only a matter of adding a few
more delete sentences to ensure that all is in order.
Syntax
Column_name2 datatype ,
...
Column_nameNdatatype ,
3
CREATE TABLE table_name2
Column_name3 datatype ,
....
Column_nameNdatatype ,
Code
stu_name varchar(20),
stu_class Varchar(20),
4
CREATE TABLE classnew
input:
5
OUTPUT:
The not null constraint tells a column that it can't have any null values in it.
This is also a type of integrity constraint. This forces a field to always have a value,
meaning you can't create a new record or change an existing one without adding a value
to it.
Syntax
Column_name2 datatype,
......
Column_namendatatype,
6
)
Code:
Input:
Output:
Unique Key
7
A collection of one or more table fields/columns that uniquely identify a record in a
database table is known as a unique key.
This is also a type of integrity constraint. It’s similar to a primary key, but it can only
accept one null value and cannot have duplicate values.
Both the special key and the primary key ensure that a column or group of columns is
unique. A Unique key is generated automatically.
Syntax
Column_Name3 DataType,
.......
Column_NameNDataType
);
Code
F_Name varchar(255),
Age int);
8
Input:
Output: