1.5. SQL Operations
1.5. SQL Operations
SQL was the first commercial language introduced for E.F Codd’s Relational model.
Today almost all RDBMS ( MySql, Oracle, Infomix, Sybase, MS Access) uses SQL as the standard
database language .
Alter Delete
Truncate Merge
SQL - DDL
Data Definition Language (DDL) statements are used to define the database structure or
schema.
It simply deals with descriptions of the database schema and is used to create and modify the
structure of database objects in the database
All DDL commands are auto-committed. That means it saves all the changes permanently in
the database.
SQL - DDL
SQL | DDL | CREATE
There are two CREATE statements available in SQL
1. CREATE DATABASE
2. CREATE TABLE
CREATE DATABASE
A Database is defined as a structured set of data. So, in SQL the very first step to store the data in a well structured
manner is to create a database.
The CREATE DATABASE statement is used to create a new database in SQL.
Syntax
Example Query:
CREATE DATABASE database_name; CREATE DATABASE my_database;
Where database_name: name of the database. This query will create a new database in SQL and
name the database as my_database
SQL - DDL
SQL | DDL | CREATE
CREATE TABLE
The CREATE TABLE statement is used to create a table in SQL
While creating tables we have to provide all the information to SQL about the names of the columns, type of data to be stored
in columns, size of the data etc.
Example Query:
Syntax:
CREATE TABLE Students
CREATE TABLE table_name
(
(
ROLL_NO int(3),
column1 data_type(size),
NAME varchar(20),
column2 data_type(size), SUBJECT varchar(20),
column3 data_type(size), );
This query will create a table named Students with three
....
columns, ROLL_NO, NAME and SUBJECT.
);
SQL - DDL
SQL | DDL | DROP
DROP is used to delete a whole database or just a table.
The DROP statement destroys the objects like an existing database, table, index, or view.
Syntax:
DROP object object_name
The result of this operation quickly removes all data from a table, typically bypassing a number of integrity enforcing
mechanisms.
Truncate >> delete all the tuples in a relation (keeps the structure)
Syntax:
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE Student_details;
After running the above query Student_details table will be truncated, i.e, the
data will be deleted but the structure will remain in the memory for further
operations.
SQL - DDL
SQL | DDL | ALTER (ADD, DROP, MODIFY)
ADD is used to add columns into the existing table
Syntax:
ALTER TABLE table_name
ADD (Columnname_1 datatype,
Columnname_2 datatype,
…
Columnname_n datatype);
Example: (ADD 2 columns AGE and COURSE to table
Student)
ALTER TABLE Student
ADD (AGE number(3),
COURSE varchar(40));
SQL - DDL
SQL | DDL | ALTER (ADD, DROP, MODIFY)
DROP COLUMN is used to drop column in a table
Syntax:
ALTER TABLE table_name
DROP COLUMN column_name;
Syntax:
ALTER TABLE table_name
MODIFY column_name column_type;
The SQL commands that deals with the manipulation of data present in the database belong to
Data Manipulation Language (DML)
Syntax: (Only values) Example: (To fetch the fields ROLL_NO, NAME, AGE from
INSERT INTO table_name the table Student
VALUES (value1, value2, value3,…); INSERT INTO Student VALUES (‘5′,’HARSH’,’WEST
BENGAL’,’XXXXXXXXXX’,’19’)
Syntax: (Column names and values both)
INSERT INTO table_name (column1, column2,..)
VALUES ( value1, value2,..);
SQL - DML
SQL | DML | INSERT
Example:
INSERT INTO Student VALUES (‘5′,’HARSH’,’WEST BENGAL’,’XXXXXXXXXX’,’19’)
SQL - DML
SQL | DML | INSERT
Example:
INSERT INTO Student (ROLL_NO, NAME, Age) VALUES (‘5′,’PRATIK’,’19’);
SQL - DML
SQL | DML | INSERT
Syntax
UPDATE table_name SET column1 = value1, column2 = value2,...
WHERE condition;
Syntax:
DELETE FROM table_name WHERE some_condition;
Example
SQL - TCL
DCL includes commands such as GRANT and REVOKE which mainly deal with the rights,
permissions and other controls of the database system.
GRANT-gives user’s access privileges to the database