0% found this document useful (0 votes)
2 views15 pages

MySQL Constraints and Table Creation Guide

The document discusses MySQL constraints, which are restrictions on data values to ensure accuracy and reliability. It covers commonly used constraints such as PRIMARY KEY, NOT NULL, UNIQUE, DEFAULT, and FOREIGN KEY, along with examples of how to define them in SQL. Additionally, it explains how to view the structure of a table using the DESCRIBE statement.

Uploaded by

mohitbhagtani05
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views15 pages

MySQL Constraints and Table Creation Guide

The document discusses MySQL constraints, which are restrictions on data values to ensure accuracy and reliability. It covers commonly used constraints such as PRIMARY KEY, NOT NULL, UNIQUE, DEFAULT, and FOREIGN KEY, along with examples of how to define them in SQL. Additionally, it explains how to view the structure of a table using the DESCRIBE statement.

Uploaded by

mohitbhagtani05
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MySQL Part - 4

To see the available tables


SYNTAX
show tables;
Constraints
Constraints are certain types of restrictions on
the data values that an attribute can have.
They are used to ensure the accuracy and
reliability of data.
However, it is not mandatory to define
constraints on a table. (as in the previous
examples)
Commonly used SQL Constraints
PRIMARY KEY The column which can uniquely identify
each row or record in a table.

NOT NULL Ensures that a column cannot have


NULL values where NULL means
missing/ unknown/not applicable value.

UNIQUE Ensures that all the values in a column are


distinct/unique.

DEFAULT A default value specified for the column


if no value is provided.

FOREIGN KEY The column which refers to value of an


attribute defined as primary key in
another table.
1. Primary key
A primary key denotes that the column is
being used to uniquely identify each record of
the table.
There can be only one primary key in a table.
It cannot be left blank.
It cannot contain any duplicate values.
It can be defined either at column level or at
table level.
Primary key
At Column Level At Table Level

create table student create table student


(Srno int(4) primary key, (Srno int(4),
Name varchar(20) not null, Name varchar(20) not null,
DateOfBirth date not null, DateOfBirth date not null,
Address varchar(40) Address varchar(40),
); primary key(srno)
);
Composite primary key
If a primary key is a Create table boardexams
(rollno integer(7),
composite key
year integer(4),
(combination of name varchar(15),
more than one class varchar(10),
column) then it can school varchar(20),
be defined only at center varchar(20),
table level. primary key (rollno, year));
2. Not null
If a column is set as not null, it ensures that the
column cannot be left blank.

create table student


(Srno int(4) primary key,
Name varchar(20) not null,
DateOfBirth date not null,
Address varchar(40)
);
3. Unique
It ensures that the column cannot contain
duplicate values.

create table student


(Srno int(4) primary key,
Name varchar(20) not null,
DateOfBirth date not null,
AadharNo varchar(12) unique
);
4. default
It is used to set a default value for a column.
Create table results
( srno int(4) primary key,
Name varchar(20) not null,
MaxMarks int(3) default 100,
MarksScored int(3)
);
5. Foreign key
It is used to relate two tables.
It is assigned on a child table where that
particular column derives its values from the
primary key of another table.
The table in which a foreign key is defined is
called a referencing table or child table.
A table to which a foreign key points is called a
referenced table or parent table.
Parent Table Child Table

Create table bank Create table transactions


(accno int(8) primary key, (transid integer,
name varchar(20), transdate date,
address varchar(30), accno int(8),
dateofopening date, type varchar(10),
balance decimal); amount decimal,
foreign key(accno) references
bank(accno));
Example 1 (with constraints)
Write SQL command to Create table transporter
create a table ‘Transporter’ ( OrderId integer primary key,
with the following Drivername varchar(50) not null,
structure:
Itemtransported varchar(50),
Traveldate date,
Destinationcity varchar(50));
Example 2 (with constraints)
Write an SQL command to Create table products
create the table ( Product_id varchar(6) primary key,
“Products” with the Product_name varchar(25) unique,
following structure
Company varchar(30) not null,
Field Type Constraint Packing varchar(20),
Product_id Varchar(6) Primary key
Product_Name Varchar(25)
CP Integer,
Unique
Company Varchar(30) Not null SP Integer,
Packing Varchar(20)
CP Integer Quantity Integer default 100);
SP Integer
Quantity Integer default is 100
Viewing Table structure
We can view the structure of an already
created table using the describe statement.

Syntax:
DESCRIBE tablename; / DESC tablename;
It displays:
Example 1. Name of the columns
2. Their datatype and size
desc products; 3. Null values are allowed or not
4. Constraints

Common questions

Powered by AI

A primary key is utilized to uniquely identify each record within a table and cannot contain duplicate or NULL values; it can be defined either at the column level or the table level . On the other hand, a foreign key is used to establish a relationship between two tables; it is a column in a child table that references the primary key of a parent table, ensuring referential integrity between the tables .

Using the DESCRIBE command provides a quick overview of the table's structure, detailing the names of columns, their datatype and size, whether null values are allowed, and any defined constraints. This information is crucial for understanding the schema and planning queries or modifications without accessing multiple parts of the documentation .

Composite primary keys, which consist of a combination of two or more columns, can only be defined at the table level. They are particularly useful in scenarios where a single column is insufficient to uniquely identify each record, such as a table recording exam results where both roll number and year are used to form the primary key to uniquely identify a student's result .

A foreign key constraint enforces referential integrity by ensuring that the value in a child table's foreign key column matches a primary key value in a parent table. For example, in a database for a banking system, the 'transactions' table has a foreign key referencing the 'bank' table's primary key (account number), ensuring that each transaction is linked to a valid bank account, preserving data consistency between the tables .

Defining both a PRIMARY KEY and a UNIQUE constraint on the same column can be redundant because a primary key inherently provides uniqueness and does not allow null values, effectively covering what a unique constraint would achieve simultaneously. However, it might be potentially useful when designing complex databases where constraint redundancy is used for clarity or documentation purposes, although typically unnecessary .

The DEFAULT constraint is advantageous when a common or standard value is often assumed, reducing the need for repeated manual entries. It can improve efficiency and data integrity by automatically providing a standard value when no value is explicitly provided. For instance, in a results table, a column might default to 100 when no maximum marks are given, which appears in the SQL statement as 'MaxMarks int(3) default 100' .

The SQL command to create a new table called 'Transporter' would be: 'Create table transporter (OrderId integer primary key, Drivername varchar(50) not null, Itemtransported varchar(50), Traveldate date, Destinationcity varchar(50));' . This command includes a PRIMARY KEY constraint on OrderId and a NOT NULL constraint on Drivername to ensure data integrity by uniquely identifying each record and requiring a driver name for each order.

SQL constraints contribute to data accuracy and reliability by imposing rules that restrict the values that can be stored in a table's columns. They ensure data integrity by enforcing uniqueness with UNIQUE, non-null values with NOT NULL, valid relational links with FOREIGN KEY, and accurate defaults with DEFAULT, helping maintain consistent and dependable datasets .

Implementing the NOT NULL constraint is important because it ensures that a column cannot be left empty, which prevents potential data errors and improves data integrity by requiring the presence of a valid data entry for every record in the table . This guarantees that essential data is always available for operations that depend on it.

The UNIQUE constraint ensures that all the values in a specified column are distinct, preventing any duplicate entries within that column . Unlike the PRIMARY KEY constraint, which uniquely identifies each record in a table and imposes both uniqueness and non-null constraints, the UNIQUE constraint can allow NULLs unless it is paired with the NOT NULL constraint.

You might also like