UNIT2
UNIT2
A)1)Integrity constraints in a database are rules or conditions that ensure the accuracy, consistency,
and validity of the data stored in tables. They define restrictions and relationships that data must
adhere to. Here are the different types of integrity constraints commonly used in database
management systems:
A primary key constraint ensures that a column or combination of columns uniquely identifies each
row in a table. It ensures that the primary key values are unique and not null. Examples:
sql
Name VARCHAR(50),
Age INT
);
In the above example, the `StudentID` column is defined as the primary key, guaranteeing that each
student has a unique ID.
A foreign key constraint establishes a relationship between two tables, ensuring referential integrity.
It enforces that values in a column (foreign key) of one table correspond to the primary key values in
another table (referenced table). Examples:
sql
CustomerID INT,
OrderDate DATE,
);
In this example, the `CustomerID` column in the `Orders` table is a foreign key referencing the
`CustomerID` column in the `Customers` table.
3. Unique Constraint:
A unique constraint ensures that the values in a column or combination of columns are unique
within a table. It allows null values, but if a non-null value exists, it must be unique. Examples:
sql
Name VARCHAR(50)
);
In this example, the `Email` column is defined with a unique constraint, ensuring that each email
address in the table is unique.
4. Check Constraint:
A check constraint specifies a condition that must be satisfied for the values in a column. It allows or
disallows specific values based on the defined condition. Examples:
sql
ProductName VARCHAR(50),
);
In this example, the `Price` column has a check constraint that ensures the price is greater than zero.
A not null constraint ensures that a column cannot have null values. It requires that the column
always has a value. Examples:
sql
City VARCHAR(50)
);
In this example, the `CustomerName` column is defined as not null, ensuring that every customer
has a name.
These integrity constraints play a crucial role in maintaining the consistency and reliability of data in
a database. They enforce data accuracy, prevent data inconsistencies, and improve the overall data
quality.
2. Explain Data Manipulation Language (DML)(insert,update,delete) commands with examples
A) 2)Data Manipulation Language (DML) commands in SQL are used to manipulate data stored in the
database tables. The three primary DML commands are INSERT, UPDATE, and DELETE. Here's an
explanation of each command with examples:
1. INSERT:
The INSERT command is used to insert new rows of data into a table.
Syntax:
sql
Example:
Consider a table named "Students" with columns: StudentID, Name, and Age. To insert a new row
into the table:
sql
This command inserts a new row with StudentID as 1, Name as 'John Doe', and Age as 20 into the
"Students" table.
2. UPDATE:
Syntax:
sql
UPDATE table_name
WHERE condition;
Example:
Continuing with the "Students" table, suppose we want to update the age of a student with
StudentID 1:
sql
UPDATE Students
SET Age = 21
WHERE StudentID = 1;
This command updates the Age column to 21 for the row where StudentID is 1 in the "Students"
table.
3. DELETE:
The DELETE command is used to delete one or more rows from a table.
Syntax:
sql
WHERE condition;
Example:
Let's say we want to delete a student with StudentID 1 from the "Students" table:
sql
WHERE StudentID = 1;
This command deletes the row where StudentID is 1 from the "Students" table.
These DML commands allow you to manipulate data within the database tables. INSERT is used to
add new data, UPDATE is used to modify existing data, and DELETE is used to remove data from
tables. These commands are fundamental for managing and modifying data in SQL databases.
3. Explain Data Definition Language (DDL) commands (create,alter,drop,truncate) with examples?
3)Data Definition Language (DDL) commands in SQL are used to define and manage the structure
and schema of the database. DDL commands are responsible for creating, altering, and deleting
database objects such as tables, indexes, views, and constraints. Here's an explanation of the
commonly used DDL commands with examples:
1. CREATE:
The CREATE command is used to create a new database object, such as a table, index, or view.
a) CREATE TABLE:
Syntax:
sql
...
);
Example:
sql
EmployeeID INT,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Age INT,
);
In this example, the CREATE TABLE command creates a table named "Employees" with columns
EmployeeID, FirstName, LastName, and Age. The EmployeeID column is defined as the primary key.
b) CREATE INDEX:
Syntax:
sql
Example:
sql
This command creates an index named "idx_last_name" on the "Employees" table's "LastName"
column.
2. ALTER:
The ALTER command is used to modify the structure of an existing database object, such as a table.
It allows adding, modifying, or dropping columns, constraints, or indexes.
a) ALTER TABLE:
Syntax:
sql
Example:
sql
This command adds a new column "City" with the VARCHAR data type to the "Employees" table.
Syntax:
sql
sql
This command removes the "Age" column from the "Employees" table.
3. DROP:
The DROP command is used to remove an existing database object, such as a table, index, or view.
a) DROP TABLE:
Syntax:
sql
Example:
sql
This command deletes the "Employees" table and removes it from the database.
b) DROP INDEX:
Syntax:
sql
Example:
sql
4. TRUNCATE:
The TRUNCATE command is used to remove all the data from a table, but keeps the table structure
intact.
Syntax:
sql
Example:
sql
This command removes all the data from the "Employees" table, but the table structure remains.
These DDL commands allow you to define, modify, and delete database objects, providing control
over the database schema and structure. CREATE is used to create new objects, ALTER is used to
modify existing objects, DROP is used to delete objects, and TRUNCATE is used to remove all data
from a table. These commands are essential for managing the database's structure and schema.
1. CURRENT_DATE():
- Syntax: `CURRENT_DATE()`
2. CURRENT_TIME():
- Syntax: `CURRENT_TIME()`
3. CURRENT_TIMESTAMP():
- Syntax: `CURRENT_TIMESTAMP()`
- Description: Returns the current date and time.
4. EXTRACT():
- Description: Extracts a specific part (year, month, day, hour, minute, etc.) from a date or
timestamp.
5. DATEPART():
- Description: Returns a specific part (year, month, day, hour, minute, etc.) from a date or
timestamp.
6. DATEADD():
- Description: Adds a specified number of units (year, month, day, hour, minute, etc.) to a date or
timestamp.
7. DATEDIFF():
- Description: Calculates the difference between two dates or timestamps in the specified units
(year, month, day, hour, minute, etc.).
8. DATE_FORMAT():
5a)In SQL, arithmetic and logical operations are used to perform calculations and make logical
comparisons on data. Here's an explanation of the various arithmetic and logical operations in SQL:
Arithmetic Operations:
Logical Operations:
3. Greater than (>), Less than (<): Performs a greater than or less than comparison.
4. Greater than or equal to (>=), Less than or equal to (<=): Performs a greater than or equal to or less
than or equal to comparison.
5. Logical AND (AND): Combines multiple conditions, all of which must be true.
These arithmetic and logical operations allow you to perform calculations, comparisons, and logical
evaluations on data in SQL queries. They are fundamental for filtering and manipulating data based on
specific conditions and performing mathematical operations on numeric values.
6.Explain about different constraints in SQL?
- The NOT NULL constraint ensures that a column cannot have a NULL value.
- Example: `CREATE TABLE Employees (EmployeeID INT NOT NULL, Name VARCHAR(50));`
2. UNIQUE Constraint:
- The UNIQUE constraint ensures that the values in a column are unique and cannot have
duplicates.
- The PRIMARY KEY constraint is a combination of NOT NULL and UNIQUE constraints. It uniquely
identifies each row in a table.
- Example: `CREATE TABLE Employees (EmployeeID INT PRIMARY KEY, Name VARCHAR(50));`
- The FOREIGN KEY constraint establishes a relationship between two tables, ensuring referential
integrity.
- Example:
sql
ProductID INT,
);
5. CHECK Constraint:
- The CHECK constraint defines a condition that must be satisfied for the values in a column.
- Example: `CREATE TABLE Employees (EmployeeID INT, Age INT CHECK (Age >= 18));`
6. DEFAULT Constraint:
- The DEFAULT constraint sets a default value for a column when no value is specified during an
INSERT operation.
7. INDEX Constraint:
- The INDEX constraint is used to create an index on one or more columns of a table. It improves
query performance.
These constraints play a crucial role in maintaining data integrity, defining relationships between
tables, and enforcing business rules. They ensure that the data stored in the database follows
certain rules and restrictions, providing consistency and reliability.
7. Explain about various string functions in sql?
7)Certainly! SQL provides various string functions that allow you to manipulate and work with string
values. Here are some commonly used string functions in SQL:
1. CONCAT():
2. LENGTH():
3. UPPER():
4. LOWER():
5. SUBSTRING():
6. REPLACE():
7. TRIM():
8. LEFT():
9. RIGHT():
10. CHARINDEX():
These string functions allow you to manipulate and extract information from strings in SQL queries. They
can be used for various tasks such as concatenation, case conversion, substring extraction, replacing
text, and more. String functions are essential for working with textual data in SQL.