Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 3 Basics of SQL

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Chapter 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.

Types of SQL Commands


There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
1. Data Definition Language (DDL)
 DDL changes the structure of the table like creating a table, deleting a table, altering a
relation, etc.

Here are some commands that come under DDL:

o CREATE
o ALTER
o RENAME
o DROP
o TRUNCATE

A) CREATE : It is used to create a new table in the database.

Syntax: CREATE TABLE table_name (column1_name datatype, column2_name datatype,…


columnN_name datatype);

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.

Syntax: CREATE TABLE new_table


AS SELECT expressions from existing_tables;
Example: create table emp as select * from employee;

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;

Modify column in table


Syntax: ALTER TABLE table_name
ALTER COLUMN column_name
TYPE column_definition;
table_name :The name of the table to modify.
column_name :The name of the column to modify in the table.
column_definition :The modified datatype of the column.
Example: alter table order_details alter column notes type varchar(500);

Modify Multiple columns in table


Syntax: ALTER TABLE table_name
ALTER COLUMN column_name TYPE column_definition,
ALTER COLUMN column_name TYPE column_definition,
…..
;
Example: alter table order_details alter column notes type varchar(500), alter column quantity type
numeric;

Drop column in table


Syntax: ALTER TABLE table_name
DROP COLUMN column_name;
table_name : The name of the table to modify.
column_name : The name of the column to delete from the table.
Example: alter table order_details drop column notes;

Rename column in table


Syntax: ALTER TABLE table_name
RENAME COLUMN old_name TO new_name;
table_name : The name of the table to modify.
old_name : The column to rename.
new_name : The new name for the column.
Example: alter table order_details rename column notes to order_notes;

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;

2. Data Manipulation Language (DML)


 DML is a language used for selecting, inserting,deleting and updating data in a database.
 It is used to retrieve and manipulate data in a relational database.

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.

Syntax: INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)


VALUES (value1, value2, value3,...valueN);

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];

Example: update company set salary = 15000 where ID = 3;

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:

Example: update company set address = 'Texas', salary=20000;

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.

Syntax: DELETE FROM table_name


WHERE [condition];

Example: delete from company where ID = 2;

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:

Example: delete from company;

3. Data Query Language (DQL)

 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.

Syntax: SELECT column1, column2, columnN FROM table_name;

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

Syntax: SELECT * FROM table_name;

Example 1 : select id, name, salary from company ;


Example 2 : select * from company ;
4. Data Control Language (DCL)
 DCL commands are used to grant and take back authority from any database user.
 Here are some commands that come under DCL:
1. Grant
2. Revoke

A) GRANT: You can grant users various privileges to tables. These permissions can be any
combination of SELECT, INSERT, UPDATE, DELETE, CREATE.

First create user with following commands:

Syntax: create user user_name with password ‘password’;

Example: create user john with password ‘john123’;

Grant privileges to user:

Syntax: Grant privileges on object to user;

Here Privileges can be select,insert,update,delete,create,all.

Object is table_name.

User is user_name.

Example: Grant select,insert on emp to john;

B) REVOKE: With this command, it can revoke or take grant from users for various privileges to
tables.

Syntax: Revoke privileges on object from user;

Example: Revoke select,insert on emp from john;

You might also like