RDBMS UNIT 4 (1)
RDBMS UNIT 4 (1)
SQL commands are very used to interact with the database. These commands allow
users to perform various actions on a database.
SQL commands or SQL sublanguage commands like DDL, DML, DCL, and TCL.
3. Alter: Alter is a reserve word which modifies the structure of the table.
Syntax:
(*) ALTER TABLE Table_name ADD (column_name
data_type);
Eg:
ALTER TABLE Stu_details ADD (adhar_num varchar2 (12));
5. TRUNCATE: It is used to delete all the rows from the table and free the
space containing the table.
Syntax:
TRUNCATE TABLE table_name;
Eg:
TRUNCATE TABLE Employee;
DML(Data Manipulation Language)
DML commands are used to modify the database. It is responsible for all
form of changes in the database.
The SQL Commands are
1. Insert:
This command is generally used after the create command to insert a set of
values into the table.
Syntax:
INSERT INTO table_name (column1, column2, ...) VALUES (value1,
value2, ...);
Eg:
INSERT INTO Book_det(Author, Subject) VALUES ("Sonoo", "DBMS");
2. Delete:
It is used to remove one or more row from a table. To delete rows from
the table, it must be in your schema or you must have delete privilege.
Syntax:
DELETE FROM table_name WHERE condition;
Eg:
DELETE FROM Book_det WHERE Author="Sonoo";
3. Update:
This command is used to update or modify the value of a column in
the table.
Syntax:
UPDATE table_name SET column1 = value1, column2 =
value2 WHERE condition;
Eg:
UPDATE studentsSET User_Name = 'Sonoo' WHERE
Student_Id = '3';
4. 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.
It is used for retrieve the records from table.
It is also known as Data Query Language(DQL).
It including the ability to filter, sort, group, and join data from
multiple tables.
Syntax:
SELECT expressions FROM Tables_Name WHERE conditions;
Eg:
a. SELECT * FROM employees;
b. SELECT Emp_name FROM employee WHERE age > 20;
DCL commands are used to grant and take back authority from any database user.
The commands that comes under DCL are Grant and Revoke
In the above syntax, <obj_priv> is the DML statement like Insert, Delete , update and
Select and <obj_name> is a table, view etc. and username is the name of the authorized user.
Eg:
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_US
ER;
2. Revoke:
It is used to take back permissions from the user.
Syntax:
REVOKE <obj_priv> ON <obj_name> FROM <username>;
In the above syntax, obj_priv> is the DML statement like Insert, Delete , update and
Select and <obj_name> is a table, view etc. and username is the name of the user from whom
the permission is revoked.
Example
Syntax:
COMMIT;
1. Following is an example which would delete those records from the table which have age
= 20 and then COMMIT the changes in the database.
Query
DELETE FROM Student WHERE AGE = 20;
COMMIT;
Output
Thus, two rows from the table would be deleted and the SELECT statement would look like,
2. Delete those records from the table which have age = 20 and then ROLLBACK the
changes in the database.
Query
DELETE FROM Student WHERE AGE = 20;
ROLLBACK;
5. SAVEPOINT Command:
It is used to roll the transaction back to a certain point without rolling back the entire
transaction.
Syntax:
SAVEPOINT SAVEPOINT_NAME;
Rollback TO <save_point_name>
Example:
DELETE FROM CUSTOMERS WHERE AGE = 15;
SAVEPOINT A;
DELETE FROM CUSTOMERS WHERE AGE = 35;
ROLLBCAK TO A;
SELECT Statement
It is used to access records from one or more database tables and views.
The SELECT statement retrieves selected data based on specified conditions.
The result of a SELECT statement is stored in a result set or result table.
The SELECT statement can be used to access specific columns or all columns from a
table.
It can be combined with clauses like WHERE, GROUP BY, HAVING, and ORDER
BY for more refined data retrieval.
The SELECT statement is versatile and allows users to fetch data based on various
criteria efficiently.
Syntax
The syntax for the SELECT statement is:
SELECT column1,column2…. FROM table_name ;
Examples:
Query:
SELECT Department, sum(Salary) as Salary
FROM employee
GROUP BY department
HAVING SUM(Salary) >= 50000;
Output:
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.
2. UNIQUE Constraint
The UNIQUE constraint ensures that all values in a column are different.
Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for
a column or set of columns.
A PRIMARY KEY constraint automatically has a UNIQUE constraint.
But you can have many UNIQUE constraints per table, but only one PRIMARY
KEY constraint per table.
Eg:
CREATE TABLE Persons (ID int NOT NULL, LastName varchar(20) NOT NULL,
FirstName varchar(20), Age int, CONSTRAINT UC_Person UNIQUE (ID,LastName));
Eg:
CREATE TABLE Persons (ID int NOT NULL PRIMARY KEY, LastName varchar(10) NOT NULL,
FirstName varchar(10), Age int);
A FOREIGN KEY is a field (or collection of fields) in one table, that refers to
the PRIMARY KEY in another table.
The table with the foreign key is called the child table, and the table with the
primary key is called the referenced or parent table.
Eg:
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
CONSTRAINT FK_PersonOrder FOREIGN KEY (PersonID)
REFERENCES Persons(PersonID)
);
5. CHECK Constraint
The CHECK constraint is used to limit the value range that can be placed in a column.
A CHECK constraint on a column it will allow only certain values for this column.
A CHECK constraint on a table it can limit the values in certain columns based on
values in other columns in the row.
Eg:
CREATE TABLE Persons (ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int CHECK (Age>=18)
);
6. DEFAULT Constraint
7. INDEX Statement
Eg:
DISTINCT clause is used to remove the duplicate records from the result set. It is
only used with SELECT statement.
Syntax:
SELECT DISTINCT expressions FROM tables WHERE conditions;
Eg:
Customer table:
Query:
SELECT DISTINCT name, age, salary FROM customers WHERE age >= '60'
ORDER BY Clause
ORDER BY Clause is used to sort or re-arrange the records in the result set. The
ORDER BY clause is only used with SELECT statement.
Syntax:
Eg:
GROUP BY Clause
GROUP BY clause is used with SELECT statement to collect data from multiple
records and group the results by one or more columns.
Syntax:
A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.
Table 1: Students
Table 2: Teachers
INNER JOIN
The INNER JOIN keyword selects records that have matching values in both tables.
SQL INNER JOIN also known as simple join is the most common type of join.
Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Eg:
Natural Join
It is a type of inner type that joins two or more tables based on the same column name and
has the same data type present on both tables.
Syntax:
Select * from tablename1 Natural JOIN tablename_2;
Eg:
Select * from Students Natural JOIN Teachers;
The LEFT JOIN is used to retrieve all records from the left table (table1) and the matched
rows or columns from the right table (table2). If both tables do not contain any matched rows
or columns, it returns the NULL.
Syntax:
Select column_1, column_2, column(s) FROM table_1 LEFT JOIN table_2 ON
table_1.column_name = table_2.column_name;
Eg:
Table 1: Product_Details
Table 2: Customer_Details
CustomerName CustomerAddress CustomerAge ProductID
The RIGHT JOIN is used to retrieve all records from the right table (table2) and the
matched rows or columns from the left table (table1). If both tables do not contain any
matched rows or columns, it returns the NULL.
Syntax:
Select column_1, column_2, column(s) FROM table_1 RIGHT JOIN table_2 ON table_1.c
olumn_name = table_2.column_name;
Table 1: Product_Details
ID ProductName Amount
Pro101 Laptop 56000
Table 2: Customer_Details
It is a combination result set of both LEFT JOIN and RIGHT JOIN. The joined tables
return all records from both the tables and if no matches are found in the table, it places
NULL. It is also called a FULL OUTER JOIN.
Syntax:
table_1.column_name = table_2.column_name;
Table 1: Product_Details
ID ProductName Amount
Table 2: Customer_Details
We have two tables: Product_Details and the Customer_Details Tables. Let's write the
SQL
CROSS JOIN
It is also known as CARTESIAN JOIN, which returns the Cartesian product of two
or more joined tables. The CROSS JOIN produces a table that merges each row from the
first table with each second table row. It is not required to include any condition in CROSS
JOIN.
Syntax:
Or
We have two tables: Product_Details and the Customer_Details Tables. Let's write the
SQL Queries to join the table using the FULL JOIN as follows:
Customer_Details;
It is a SELF JOIN used to create a table by joining itself as there were two tables. It makes
temporary naming of at least one table in an SQL statement.
Syntax:
Select column1, column2, column(s) FROM table_1 Tbl1, table_2 Tbl2 WHERE condition;
Tbl1 and Tbl2 are two different table aliases for the same table.
Table 1: Product_Details
ID ProductName Amount