• Primary Key – is a column or group of columns in a table that uniquely identify every
row in that table.
• Candidate Key – is a set of attributes that uniquely identify tuples in a table.
Candidate Key is a super key with no repeated attributes.
• Alternate Key – is a column or group of columns in a table that uniquely identify
every row in that table.
• Foreign Key – is a column that creates a relationship between two tables. The
purpose of Foreign keys is to maintain data integrity and allow navigation between
two different instances of an entity.
DIFFERENCE BETWEEN ALTER AND UPDATE
ALTER UPDATE
Used to modify the structure of a table (like Used to modify the data (records)
adding, deleting, or changing columns). inside a table.
Changes the existing values of records
Changes the table design, not the actual data.
in the table.
Example: UPDATE student SET age =
Example: ALTER TABLE student ADD age INT;
18 WHERE roll_no = 1;
Part of Data Manipulation Language
Part of Data Definition Language (DDL).
(DML).
Works on columns, constraints, etc. Works on rows (records) of the table.
Difference between UNIQUE and PRIMARY KEY in SQL
UNIQUE Constraint PRIMARY KEY Constraint
Ensures that all values in a column are unique, i.e., no Uniquely identifies each record in a
duplicates. table.
Allows one or more NULL values. Does not allow NULL values.
A table can have only one
A table can have multiple UNIQUE constraints.
PRIMARY KEY.
Difference between NULL and NOT NULL Constraint
NULL Constraint NOT NULL Constraint
Allows a column to have empty or Does not allow empty or unknown values in
unknown values. the column.
Column can remain blank when inserting You must provide a value for the column
data. while inserting data.
Indicates that the value is missing or
Ensures that the value is always present.
unknown, but valid.
Example: email VARCHAR(50) NULL Example: roll_no INT NOT NULL
DDL (Data Definition Language) DML (Data Manipulation Language)
Used to define or change the structure of a Used to modify or manage the data inside
database or table. tables.
Works with inserting, updating, or deleting
Works with table design, not actual data.
data.
Examples of DDL commands: CREATE, Examples of DML commands: INSERT,
ALTER, DROP UPDATE, DELETE
Changes can be saved with COMMIT or
Automatically saves changes (auto-commit).
undone with ROLLBACK.
Defines the database structure (tables,
Manipulates the data stored in tables.
columns).
Difference between COUNT(column_name) and COUNT(*)
COUNT(column_name) COUNT(*)
Counts all rows, including rows with
Counts the non-NULL values of a specific column.
NULL values.
Does not ignore any rows, counts
Ignores NULL values in the specified column.
everything.
COUNT(column_name) COUNT(*)
Example: COUNT(price) counts only rows where Example: COUNT(*) counts all rows
price is NOT NULL. in the table.
Examples
COUNT(column_name) Example
SELECT COUNT(price) FROM product;
Counts the number of products that have a non-NULL price.
COUNT(*) Example
SELECT COUNT(*) FROM product;
Counts the total number of products, including those with NULL values in any column.
DATA INCONSISTENCY
Duplication of data is data redundancy. It leads to the problems like wastage of
space and data inconsistency.
1. Relation
• • Definition: A relation, or table, is a collection of tuples (rows) that
share the same attributes (columns). It represents a specific type of entity or
relationship in the database.
• • Example: A table called Students that includes information about
students, where each row represents a student.
2. Attribute
• • Definition: An attribute is a column in a table that represents a specific
property or characteristic of the entity described by the table. Each attribute has
a name and a data type.
• • Example: In the Students table, attributes might include StudentID,
Name, and DateOfBirth.
3. Tuple
• • Definition: A tuple is a single row in a table. It represents a specific
instance of the entity or relationship described by the table.
• • Example: In the Students table, a tuple might be (123, 'Alice Smith',
'2005-05-15'), representing a specific student.
4. Domain
• • Definition: A domain is the set of permissible values that an attribute
can take. It defines the type of data that can be stored in a column, such as
integer, string, or date.
• • Example: For the attribute DateOfBirth, the domain might be all valid
date values.
5. Degree
• • Definition: The degree of a relation (table) is the number of attributes
(columns) it has. It indicates the number of different pieces of information
stored for each tuple.
• • Example: If the Students table has three attributes (StudentID, Name,
DateOfBirth), its degree is 3.
6. Cardinality
• • Definition: The cardinality of a relation (table) is the number of tuples
(rows) it contains. It represents the size of the table and the number of instances
of the entity.
• • Example: If the Students table has 100 rows, its cardinality is 100.
7. Domain: Domain is set of possible value or range of valid values or set of all
unique values that an attribute/column can hold.
CHAR VARCHAR
CHAR specifies fixed length string VARCHAR specifies variable length
and occupies fixed memory storage. string and occupies variable
memory storage
CHAR can store a maximum of 0 to VARCHAR can store a maximum of
255 characters 65535 characters
CHAR is used when the values of an VARCHAR is used when the values
attribute are expected to be of same of an attribute are expected to be of
size. different size
If the value entered for CHAR is If the value entered for VARCHAR
shorter than its length, blank spaces is shorter than its length, no blank
are added. spaces are added.
CHAR occupies memory space of 1 VARCHAR occupies memory space
byte / character. of 1 byte/character and +2 bytes for
holding length information.
Search operation is faster than Search operation is slower than
VARCHAR CHAR
CHAR(X) – takes x characters of memory space even if you enter less than X
characters.
Example: City Char(10)
City=‘Delhi’
City column occupies 10 bytes with first 5 bytes with the given value ‘Delhi’
and rest with blankspaces.
VARCHAR(X) – takes only the required memory space for the actual number
of characters entered to the column
Example: City Varchar(20)
City=‘Hyderabad’
City column occupies 9 (value) + 2 (hold length info.) = 11bytes of memory
space
NOTE:
A table can have only ONE primary key, but that primary key can consist of one or multiple
columns (also called a composite primary key).
Explanation:
• A primary key uniquely identifies each record (row) in a table.
• Each table can define only one primary key constraint.
This primary key can be made up of: A single column (e.g., StudentID)
Or a combination of multiple columns (e.g., CourseID + StudentID) — this is called a
composite primary key.
Examples:
Single-column Primary Key:
CREATE TABLE Students ( StudentID INT PRIMARY KEY,
Name VARCHAR(50),
Class VARCHAR(10));
Composite Primary Key:
CREATE TABLE Enrollments ( StudentID INT,
CourseID INT,
EnrollDate DATE,
PRIMARY KEY (StudentID, CourseID) );
Here, StudentID + CourseID together uniquely identify each enrollment record.