Unit 3 Basics of SQL
Unit 3 Basics of SQL
Unit 3 Basics of SQL
SQL
SQL stands for Structured Query Language. It is used for storing and managing data in relational
database management system (RDMS).
It is a standard language for Relational Database System. It enables a user to create, update and
delete relational databases.
All the RDBMS like PostgreSQL, MySQL, Oracle and SQL Server use SQL as their standard
database language.
SQL allows users to query the database in a number of ways, using English-like statements.
Characteristics of SQL
SQL is easy to learn.
SQL is used to access data from relational database management systems.
SQL can execute queries against the database.
SQL is used to describe the data.
SQL is used to define the data in the database and manipulate it when needed.
SQL is used to create and drop the database and table.
SQL is used to create a view, stored procedure, function in a database.
SQL allows users to set permissions on tables, procedures, and views.
Advantages of SQL
High speed
Using the SQL queries, the user can quickly and efficiently retrieve a large amount of records from a
database.
No coding needed
In the standard SQL, it is very easy to manage the database system. It doesn't require a substantial amount of
code to manage the database system.
Portability
SQL can be used in laptop, PCs, server and even some mobile phones.
Interactive language
SQL is a domain language used to communicate with the database. It is also used to receive answers to the
complex questions in seconds.
Multiple data view
Using the SQL language, the users can make different views of the database structure.
SQL Datatype
SQL Datatype is used to define the values that a column can contain.
Every column is required to have a name and data type in the database table.
Datatype(size) Description
INT(size) The INT data type stores integer values. It can hold whole numbers, such
as items in stock or orders placed. INT values can range from -
2147483648 to 2147483647.
CHAR(Size) The CHAR data type stores fixed-length character strings. It can hold a
fixed number of characters, specified by the field's length. For example, a
CHAR(10) field can store a string of up to 10 characters. If the inserted
string is shorter than the specified length, the remaining characters will
be filled with spaces. Its size can be 0 to 255 characters.
VARCHAR(Size) The VARCHAR data type stores variable-length character strings. It can
hold a variable number of characters, specified by the field's length. For
example, a VARCHAR(10) field can store a string of up to 10 characters.
If the string inserted is shorter than the specified length, it will only use
the necessary amount of storage. Its size can be from 0 to 65535
characters.
DATE The DATE data type stores date values. It can be used to hold a date,
which includes the day, month, and year. It's defined as DATE and used
to store date information such as birthdate, hiring date, order date, etc.
The format of a DATE value can vary depending on the specific SQL
implementation being used, but it typically follows the format of
"YYYY-MM-DD" where YYYY represents the year, MM represents the
month, and DD represents the day.
SQL Commands
SQL commands are instructions. It is used to communicate with the database. It is also used to
perform specific tasks, functions, and queries of data.
SQL can perform various tasks like create a table, add data to tables, drop the table, modify the table,
set permission for users.
o CREATE
o ALTER
o RENAME
o DROP
o TRUNCATE
Example: create table employee(emp_id int, name varchar(20), Email varchar(100), DOB Date);
CREATE TABLE AS statement: It is used to create a table from an existing table by copying the
existing table's columns.
B) ALTER : It is used to add a column, modify a column, drop a column, rename a column or rename a
table.
Add column in table
Syntax: ALTER TABLE table_name
ADD new_column_name column_definition;
table_name :The name of the table to modify.
new_column_name : The name of the new column to add to the table.
column_definition :The datatype of the column.
Example: alter table order_details add order_date date, add quantity integer;
C) Rename table
Syntax: ALTER TABLE table_name
RENAME TO new_table_name;
table_name: The table to rename.
new_table_name: The new table name.
Example: alter table order_details rename to order_information;
D) DROP TABLE Statement : It allows to remove or delete a table from the database.
Syntax: DROP TABLE table_name;
Example: drop table order_details;
E) TRUNCATE : TRUNCATE TABLE command is used to delete complete data from an existing
table. You can also use DROP TABLE command to delete complete table but it would remove complete
table structure from the database and you would need to re-create this table once again if you wish to
store some data.
Syntax: TRUNCATE TABLE table_name;
Example: truncate table company;
A) INSERT : It allows one to insert new rows into a table. One can insert a single row at a time or
several rows as a result of a query.
Here, column1, column2,...columnN are the names of the columns in the table into which you want to
insert data.
Example: insert into company (id, name, age, address, salary, join_date) values (1, ‘Paul’, 32,
‘California’, 20000, ‘25-Jun-1991’);
The following example inserts multiple rows using the multirow VALUES syntax:
Example: insert into company (id, name, age, address, salary, join_date) values ( 2, ‘Mark’, 25,
‘Norway’, 65000, ‘28-Jan-1991’), ( 3, ‘Park’, 26, ‘Texas’, 35000, ‘02-May-1986’);
B) UPDATE: It is used to modify the existing records in a table. You can use WHERE clause with
UPDATE query to update the selected rows. Otherwise, all the rows would be updated.
Syntax: UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
If you want to modify all ADDRESS and SALARY column values in company table, you do not
need to use WHERE clause and UPDATE query would be as follows:
C) DELETE : It is used to delete the existing records from a table. You can use WHERE clause with
DELETE query to delete the selected rows. Otherwise, all the records would be deleted.
If you want to DELETE all the records from COMPANY table, you do not need to use WHERE
clause with DELETE queries, which would be as follows:
DQL is a language used to fetch data from database. It uses only one command.
A) SELECT : It is used to fetch the data from a database table, which returns data in the form of result
table. These result tables are called result-sets.
Here, column1, column2...are the fields of a table, whose values you want to fetch. If you want to fetch
all the fields available in the field then you can use the following
A) GRANT: You can grant users various privileges to tables. These permissions can be any
combination of SELECT, INSERT, UPDATE, DELETE, CREATE.
Object is table_name.
User is user_name.
B) REVOKE: With this command, it can revoke or take grant from users for various privileges to
tables.