Dbms Module III (Chapter 2(SQL))
Dbms Module III (Chapter 2(SQL))
Introduction to SQL:
SQL is a standard language for storing, manipulating and retrieving data in databases.
What is SQL?
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL became a standard of the American National Standards Institute (ANSI) in
1986, and of the International Organization for Standardization (ISO) in 1987
Why SQL?
Nowadays, SQL is widely used in data science and analytics. Following are the
reasons which explain why it is widely used:
o The basic use of SQL for data professionals and SQL users is to insert, update,
and delete the data from the relational database.
o SQL allows the data professionals and users to retrieve the data from the
relational database management systems.
o It allows SQL users to create, drop, and manipulate the database and its
tables.
o It also helps in creating the view, stored procedure, and functions in the
relational database.
o It allows you to define the data and modify that stored data in the
relational database.
History of SQL
"A Relational Model of Data for Large Shared Data Banks" was a paper which was
published by the great computer scientist "E.F. Codd" in 1970.
The IBM researchers Raymond Boyce and Donald Chamberlin originally developed
the SEQUEL (Structured English Query Language) after learning from the paper given
by E.F. Codd. They both developed the SQL at the San Jose Research laboratory of
IBM Corporation in 1970.
SQL Commands
There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
o DDL changes the structure of the table like creating a table, deleting a table,
altering a table, etc.
o All the command of DDL are auto-committed that means it permanently save
all the changes in the database.
o CREATE
o ALTER
o DROP
o TRUNCATE
o DML commands are used to modify the database. It is responsible for all form
of changes in the database.
o 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.
o INSERT
o UPDATE
o DELETE
o Grant
o Revoke
These operations are automatically committed in the database that's why they
cannot be used while creating tables or dropping them.
o COMMIT
o ROLLBACK
o SAVEPOINT
o SELECT
Data types are used to represent the nature of the data that can be stored in the
database table. For example, in a particular column of a table, if we want to store a
string type of data then we will have to declare a string data type of this column.
Data types mainly classified into three categories for every database.
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.
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each
row in a table
FOREIGN KEY - Prevents actions that would destroy links between tables
CREATE INDEX - Used to create and retrieve data from the database very quickly
Examples on Constraints:
The INSERT statement is used to add new data into a table. It allows you to specify
the columns to which you want to insert data, as well as the values for each column.
INSERT INTO: This clause indicates that you want to insert data into
a table.
table_name: This is the name of the table to which you want to add
data.
columns in which you want to insert data. If you're inserting data into
correspond to the columns specified. The values must match the data
employees in a company. To insert a new employee record, we can use the following
SQL query:
You can also use the INSERT statement to insert multiple rows into a table with a
In this example, we inserted three new employee records into the employees table
The UPDATE statement is used to modify existing records in a table. It allows you to
change the values of specific columns based on certain conditions. The basic syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
table.
table_name: This is the name of the table from which you want to
update data.
SET: This keyword is used to specify the columns and their new
values.
determines which rows to update. If you omit the WHERE clause, all
with employee_id 101 to $65000. We can use the following SQL query:
UPDATE employees
SET salary = 65000
WHERE employee_id = 101;
In this example, we used the WHERE clause to specify the condition that
The DELETE statement is used to remove records from a table based on certain
conditions. It allows you to specify which rows you want to delete. The basic syntax
DELETE FROM: This clause indicates that you want to delete data
from a table.
table_name: This is the name of the table from which you want to
delete data.
determines which rows to delete. If you omit the WHERE clause, all
Example2: You can also delete multiple rows that match a specific
condition. For example, let’s delete all employees with a salary lower than
$55000:
To create a basic SQL query for data retrieval, use the SELECT statement, specifying
the columns you want to retrieve and the table from which you want to retrieve the
data. Optionally, you can include a WHERE clause to filter the data based on specific
conditions.
The SELECT statement in SQL is used to fetch or retrieve data from a database. It
allows users to access the data and retrieve specific data based on specific
conditions.
We can fetch either the entire table or according to some specified rules. The data
returned is stored in a result table. This result table is also called the result set. With
the SELECT clause of a SELECT command statement, we specify the columns that we
want to be displayed in the query result and, optionally, which column headings we
The SELECT clause is the first clause and is one of the last clauses of the select
statement that the database server evaluates. The reason for this is that before we
can determine what to include in the final result set, we need to know all of the
Syntax
The syntax for the SELECT statement is:
CREATE TABLE:
In this example, we will fetch all the fields from the table
Customer:
Query:
Suppose we want to see table values with specific conditions then WHERE Clause is
used with select statement.
Query:
SELECT CustomerName FROM Customer where Age = '21';
Features of SQL
Data modification with ease.
Transaction management.