Unit 3
Unit 3
d. TRUNCATE: It is used to delete all the rows from the table and free the space containing
the table.
Syntax:
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE EMPLOYEE;
DML
• COMMANDS
2. Data Manipulation Language
• DML commands are used to modify the database. It is
responsible for all form of changes in the database.
• The command of DML is not auto-committed that means
it can't permanently save all the changes in the database.
They can be rollback.
• Here are some commands that come under DML:
• INSERT
• UPDATE
• DELETE
a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the row of a table.
Syntax:
1.INSERT INTO TABLE_NAME
2.(col1, col2, col3,.... col N)
3.VALUES (value1, value2, value3, .... valueN);
Or
1. INSERT INTO TABLE_NAME
2. VALUES (value1, value2, value3, .... valueN);
For example:
1.INSERT INTO javatpoint (Author, Subject)
VALUES ("Sonoo", "DBMS");
b. UPDATE: This command is used to update or modify the value of a column in the
table.
Syntax:
1.UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [W
HERE CONDITION]
For example:
3. UPDATE students
4.SET User_Name = 'Sonoo'
3.WHERE Student_Id = '3'
c. DELETE: It is used to remove one or more row from a
table.
Syntax:
1.DELETE FROM table_name [WHERE condition];
For example:
1.DELETE FROM javatpoint
2.WHERE Author="Sonoo";
3. Data Control Language
b. Rollback: Rollback command is used to undo transactions that have not already
been saved to the database.
• Syntax:
ROLLBACK;
• Example:
1. DELETE FROM CUSTOMERS
2. WHERE AGE = 25;
3. ROLLBACK;
• c. SAVEPOINT: It is used to roll the transaction
back to a certain point without rolling back the
entire transaction.
Syntax:
1.SAVEPOINT SAVEPOINT_NAME;
5. Data Query Language
• DQL is used to fetch the data from the database.
• It uses only one command:
• SELECT
• a. SELECT: This is the same as the projection operation of relational algebra. It is
used to select the attribute based on the condition described by WHERE clause.
• Syntax:
1. SELECT expressions
2. FROM TABLES
3. WHERE conditions;
• For example:
1. SELECT emp_name
2. FROM employee
-- Insert some
sample data
INSERT INTO
book_loans
VALUES (1,
TO_DATE('2024-
01-20', 'YYYY-MM-
DD'),
TO_DATE('2024-
01-25', 'YYYY-MM-
DD'));
INSERT INTO book_loans VALUES (2, TO_DATE('2024-01-15', 'YYYY-MM-DD'),
TO_DATE('2024-01-22', 'YYYY-MM-DD'));
INSERT INTO book_loans VALUES (3, TO_DATE('2024-01-10', 'YYYY-MM-DD'),
TO_DATE('2024-01-18', 'YYYY-MM-DD'));
CLOSE book_cursor;
END
calculate_late_fee;
Explanation:
1.We create a table book_loans to store information about
book loans.
2. We insert some sample data into the table.
3.We create a stored procedure calculate_late_fee that
uses a cursor to iterate through each book loan record.
4.Inside the loop, we calculate the number of days the
book is overdue.
5.If the book is overdue, we print a message with the
book ID, days late, and the late fee.
6. If the book is returned on time, we print a message
indicating that.
Constraints with examples
• SQL constraints are used to specify rules for the data in a table.
• Constraints are used to limit the type of data that can go into a table. This ensures the
accuracy and reliability of the data in the table. If there is any violation between the constraint
and the data action, the action is aborted.
• Constraints can be column level or table level. Column level constraints apply to a column,
and table level constraints apply to the whole table.
CREATE TABLE
Student (
ID int(6) NOT NULL
UNIQUE,
NAME varchar(10),
ADDRESS
varchar(20)
);
3. PRIMARY KEY –
• Primary Key is a field which uniquely identifies each row in the table.
If a field in a table as primary key, then the field will not be able to
contain NULL values as well as all the rows should have unique values
for this field. So, in other words we can say that this is combination of
NOT NULL and UNIQUE constraints.
•
A table can have only one field as primary key. Below query will
create a table named Student and specifies the field ID as primary
key.
CREATE TABLE
Student (
ID int(6) NOT NULL
UNIQUE,
NAME varchar(10),
ADDRESS
varchar(20),
PRIMARY KEY(ID)
);
4. FOREIGN KEY –
Foreign Key is a field in a table which uniquely identifies each row of a another table. That is, this
field points to primary key of another table. This usually creates a kind of link between the
tables.
Consider the two tables as shown below:
O_ID ORDER_NO C_ID
1 2253 3
2 3325 3
3 4521 2
4 8532 1
CREATE TABLE
Orders (
O_ID int NOT NULL,
ORDER_NO int NOT NULL,
C_ID int, PRIMARY KEY
(O_ID),
FOREIGN KEY (C_ID)
REFERENCES
Customers(C_ID)
)
(i) CHECK –
Using the CHECK constraint we can specify a condition for a field,
which should be satisfied at the time of entering values for this
field.
For example, the below query creates a table Student and specifies
the condition for the field AGE as (AGE >= 18 ). That is, the user will
not be allowed to enter any record in the table with A GE < 18. Check
constraint in detail
CREATE TABLE
Student (
ID int(6) NOT
NULL,
NAME varchar(10)
NOT NULL,
AGE int NOT NULL
CHECK (AGE >= 18)
);
(ii) DEFAULT –
This constraint is used to provide a default value for the fields. That is, if at
the time of entering new records in the table if the user does not specify
any value for these fields then the default value will be assigned to them.
For example, the below query will create a table named Student and
specify the
default value for the field AGE as 18.
CREATE TABLE
Student ( ID
int(6) NOT NULL,
NAME varchar(10)
NOT NULL,
AGE int DEFAULT 18
);
J O I N S AND ITS TYPES
INSERT: This operation allows you to add new records or rows to a table.
UPDATE: The UPDATE operation enables you to modify existing records in a table.
DELETE: The DELETE operation allows you to remove records from a table.
ALTER: The ALTER operation is used to add,deleteor modify coloums in an existing table.
1. Using INSERT Statement
INSERT statement in the PL/SQL is used to add the new records or rows of data into the table. It allows u to specify the table name, column
s where the data will be inserted and the corresponding values to be inserted into the columns.
Syntax for INSERT Statement:
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Explanation:
2.table_name is the name of the table, which we want to insert the data
2.(column1, column2, ….) are optional lists of the columns in table. If specified, you need to be provide the values for those columns in the
same order.
3.VALUES (value1, value2, …) are values to be inserted into specified columns. These values must match the data types of the provided
columns.
student_id
Examples of INSERT Statement first_name last_name
Example 1:
1 John Doe
Let’ insert some data into the students table.
2
INSERT INTO students (student_id, Jane
first_name, last_name) Smith
VALUES (1, 'John', 'Doe');
UPDATE statement in the PL/SQL is used to the modify the existing records in table. It is allowed us to specify the table name,
columns to updated new values for these columns and an optional conditional to the filter which rows are to be updated.
Syntax for UPDATE Statement:
UPDATE table_name
Setcolumn1=value1,column2=value2,...
WHERE condition;
Explanation:
1.table_name is the name of the table, which we want to update.
2.SET column1 = value1, column2 = value2, … are specified the columns to the updated with their new values.
3.WHERE condition is specify which rows are to be update. if we not use where condition all rows in the table will be update.
Example 1:
Let’s update the last name of the students as Anderson whose student_id is 3.
UPDATEstudents
SET last_name = ‘Anderson’
WHERE student_id = 3;
Output:
student_id first_name last_name
1 John Doe
2 Jane Smith
3 Michael Anderson
4 Emily Williams
3. Using DELETE Statement
DELETE statement in the PL/SQL is used to remove the one or more records from the table. It is allowed you to the specify the table name and
optional condition to the filter which rows are to be deleted.
Syntax for DELETE Statement:
DELETE FROM table_ name
WHERE condition;
Explanation:
1.table_name is the name of the table, which we want to delete the rows of the table.
2.WHERE condition is specify which rows are to be delete. if we not use where condition all rows in the table will be delete.
Example 1:
Let’s delete the student from the table whose student_id is 4.
DELETE FROM students
WHERE student_id = 4;
Output:
4.SQL ALTER TABLE Examples
Below are the examples of ALTER TABLE statement. These examples demonstrates different use cases and shows how to use ALTER
TABLE statement in SQL.
The following query deletes the “Email” column from “Students” table: