0% found this document useful (0 votes)
3 views20 pages

01 Basic SQL Commands Final Ddl Dml Dcl

The document provides an overview of SQL data types, including string, numeric, date/time, and large object types, as well as their specific characteristics in Oracle. It also outlines the five types of SQL queries: DDL, DML, DCL, TCL, and DQL, detailing their functions and syntax. Additionally, it explains various SQL commands for database manipulation, including creating, altering, and deleting databases and tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views20 pages

01 Basic SQL Commands Final Ddl Dml Dcl

The document provides an overview of SQL data types, including string, numeric, date/time, and large object types, as well as their specific characteristics in Oracle. It also outlines the five types of SQL queries: DDL, DML, DCL, TCL, and DQL, detailing their functions and syntax. Additionally, it explains various SQL commands for database manipulation, including creating, altering, and deleting databases and tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

SQL Data Types

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.

Oracle Data Types

Oracle String data types

CHAR(size) It is used to store character data within the predefined length. It can be
stored up to 2000 bytes.

VARCHAR2(size) It is used to store variable string data within the predefined length. It can
be stored up to 4000 byte.

VARCHAR(SIZE) It is the same as VARCHAR2(size). You can also use VARCHAR(size), but it
is suggested to use VARCHAR2(size)

NVARCHAR2(size) It is used to store Unicode string data within the predefined length. We
have to must specify the size of NVARCHAR2 data type. It can be stored up
to 4000 bytes.

Oracle Numeric Data Types

NUMBER(p, s) It contains precision p and scale s. The precision p can range from 1 to 38,
and the scale s can range from -84 to 127.

FLOAT(p) It is a subtype of the NUMBER data type. The precision p can range
from 1 to 126.

BINARY_FLOAT It is used for binary precision( 32-bit). It requires 5 bytes, including length
byte.

BINARY_DOUBLE It is used for double binary precision (64-bit). It requires 9 bytes, including
length byte.

Oracle Date and Time Data Types

DATE It is used to store a valid date-time format with a fixed length. Its range varies
from January 1, 4712 BC to December 31, 9999 AD.
TIMESTAMP It is used to store the valid date in YYYY-MM-DD with time hh:mm:ss format.

Oracle Large Object Data Types (LOB Types)

BLOB It is used to specify unstructured binary data. Its range goes up to 232-1 bytes or 4
GB.

BFILE It is used to store binary data in an external file. Its range goes up to 232-1 bytes or
4 GB.

CLOB It is used for single-byte character data. Its range goes up to 232-1 bytes or 4 GB.

NCLOB It is used to specify single byte or fixed length multibyte national character set
(NCHAR) data. Its range is up to 232-1 bytes or 4 GB.

RAW(size) It is used to specify variable length raw binary data. Its range is up to 2000 bytes
per row. Its maximum size must be specified.

LONG It is used to specify variable length raw binary data. Its range up to 231-1 bytes or 2
RAW GB, per row.

Types of SQL
Here are five types of widely used SQL queries.

• Data Definition Language (DDL)


• Data Manipulation Language (DML)
• Data Control Language(DCL)
• Transaction Control Language(TCL)
• Data Query Language (DQL)

A Database Manipulation (DML) statement (DELETE, INSERT, or UPDATE)


 A Database Definition (DDL) statement (CREATE, ALTER, DROP,
TRUNCATE).
A Database Control (DCL) statement (GRANT,REVOKE).
A Transaction Control (TCL) statement (COMMIT,
ROLLBACK,SAVEPOINT).
A Data Query Language (DQL) statement(SELECT).
 A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or
SHUTDOWN).
DBMS languages

Database languages are used to read, update and store data in a database. There are several
such languages that can be used for this purpose; one of them is SQL (Structured Query
Language).

• DDL – Data Definition Language:


(CREATE,DROP,ALTER,TRUNCATE,COMMENT,RENAME)
• DML – Data Manipulation Language: (INSERT, UPDATE,DELETE)
• DCL – Data Control Language: (GRANT,REVOKE)
• TCL-Transaction Control Language: (COMMIT,ROLLBACK)

1. DDL(Data Definition Language) : DDL or Data Definition Language actually consists of


the SQL commands that can be used to define the database 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.
CREATE – it is used to create the database or its objects (like table, index, function, views,
store procedure and triggers).
There are two CREATE statements available in SQL:
• CREATE DATABASE
• 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:
CREATE DATABASE database_name;

DATABASE MANAGEMENT SYSTEMS


Example:
SQL> CREATE DATABASE Employee;
In order to get the list of all the databases, you can use SHOW DATABASES statement.
Example –
SQL> SHOW DATABASES;

CREATE TABLE:
The CREATE TABLE statement is used to create a table in SQL. We know that a table
comprises of rows and columns. So 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. Let us now dive into details on how to use CREATE TABLE statement to
create tables in SQL.
Syntax:
CREATE TABLE table_name
(
column1 data_type(size),
column2 data_type(size),
column3 data_type(size),
....
);
Example Query:
This query will create a table named Students with three columns, ROLL_NO, NAME and
SUBJECT.
CREATE TABLE Students
(
ROLL_NO int(3),
NAME varchar(20),
SUBJECT varchar(20),
);
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.
A DROP statement in SQL removes a component from a relational database management
system (RDBMS).
Syntax:
DROP object object_name;
Examples:
DROP TABLE table_name;
table_name: Name of the table to be deleted.
DROP DATABASE database_name;
database_name: Name of the database to be deleted.
TRUNCATE
It is used to remove all records from a table, including all spaces
The TRUNCATE TABLE mytable statement is logically (though not physically) equivalent
to the DELETE FROM mytable statement (without a WHERE clause).
Syntax:
TRUNCATE TABLE table_name;
DROP vs TRUNCATE
• Truncate is normally ultra-fast and its ideal for deleting data from a temporary table.
• Truncate preserves the structure of the table for future use, unlike drop table where
the table is deleted with its full structure.
• Table or Database deletion using DROP statement cannot be rolled back, so it must be
used wisely.

To delete the whole database


DROP DATABASE student_data;
After running the above query whole database will be deleted.
To truncate Student_details table from student_data database.
TRUNCATE TABLE Student_details;
ALTER (ADD, DROP, MODIFY)
ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is also
used to add and drop various constraints on the existing table.
ALTER TABLE – ADD:

ADD is used to add columns into the existing table. Sometimes we may require to add
additional information, in that case we do not require to create the whole database
again, ADD comes to our rescue.
Syntax:
ALTER TABLE table_name
ADD (Columnname_1 datatype,
Columnname_2 datatype,

Columnname_n datatype);

ALTER TABLE – DROP


DROP COLUMN is used to drop column in a table. Deleting the unwanted columns from the
table.
Syntax:
ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE-MODIFY
It is used to modify the existing columns in a table. Multiple columns can also be modified at
once.
ALTER TABLE table_name
MODIFY column_name column_type;
QUERY:
To ADD 2 columns AGE and COURSE to table Student.
ALTER TABLE Student ADD (AGE number(3),COURSE varchar(40));
MODIFY column COURSE in table Student
ALTER TABLE Student MODIFY COURSE varchar(20);
Comments
As is any programming languages comments matter a lot in SQL also. In this set we will
learn about writing comments in any SQL snippet.
Comments can be written in the following three formats:
• Single line comments.
• Multi line comments

Single line comment;


--single line comment:
SELECT * FROM Customers;

Multi line comments

/*Select all the columns


of all the records
in the Customers table:*/
SELECT * FROM Customers;
DML(Data Manipulation Language) : The SQL commands that deals with the
manipulation of data present in the database belong to DML or Data Manipulation Language
and this includes most of the SQL statements.
SELECT Statement
select statement is used to fetch data from relational database. A relational database is
organized collection of data. As we know that data is stored inside tables in a database. SQL
select statement or SQL select query is used to fetch data from one or more than one tables.

SELECT Syntax
One column:
Here column_name is the name of the column for which we need to fetch data and
table_name is the name of the table in the database.
SELECT column_name FROM table_name;
More than one columns:
SELECT column_name_1, column_name_2, ... FROM table_name;
To fetch the entire table or all the fields in the table:
SELECT * FROM table_name;
Example:
SELECT EMP_NAME FROM EMPLOYEES;
To fetch the entire EMPLOYEES table:
SELECT * FROM EMPLOYEES;
Query to fetch the fields ROLL_NO, NAME, AGE from the table Student:
SELECT ROLL_NO, NAME, AGE FROM Student;
INSERT INTO Statement
The INSERT INTO statement of SQL is used to insert a new row in a table. There are two
ways of using INSERT INTO statement for inserting rows:
Only values: First method is to specify only the value of data to be inserted without the
column names.

INSERT INTO table_name VALUES (value1, value2, value3,…);

Column names and values both: In the second method we will specify both the columns
which we want to fill and their corresponding values as shown below:
INSERT INTO table_name (column1, column2, column3,..) VALUES ( value1, value2,
value3,..);
Example:
Method 1 (Inserting only values) :
INSERT INTO Student VALUES (‘5′,’HARSH’,’WEST
BENGAL’,’XXXXXXXXXX’,’19’);
Method 2 (Inserting values in only specified columns):
INSERT INTO Student (ROLL_NO, NAME, Age) VALUES (‘5′,’PRATIK’,’19’);
UPDATE Statement
The UPDATE statement in SQL is used to update the data of an existing table in database.
We can update single columns as well as multiple columns using UPDATE statement as per
our requirement.
Basic Syntax:
UPDATE TableName
SET column_name1 = value, column_name2 = value....
WHERE condition;
EX1:
SQL> UPDATE EMPLOYEES
SET EMP_SALARY = 10000
WHERE EMP_AGE > 25;

EX2;
SQL> UPDATE EMPLOYEES
SET EMP_SALARY = 120000
WHERE EMP_NAME = 'Apoorv';
DELETE Statement
The DELETE Statement in SQL is used to delete existing records from a table. We can delete
a single record or multiple records depending on the condition we specify in the WHERE
clause.
Basic Syntax:
DELETE FROM table_name WHERE some_condition;
Deleting single record: Delete the rows where NAME = ‘Ram’. This will delete only the first
row.
DELETE FROM Student WHERE NAME = 'Ram';

Deleting multiple records: Delete the rows from the table Student where Age is 20. This will
delete 2 rows(third row and fifth row).
DELETE FROM Student WHERE Age = 20;
Delete all of the records: There are two queries to do this as shown below,
query1: "DELETE FROM Student";
query2: "DELETE * FROM Student";

DCL(Data Control Language) : DCL includes commands such as GRANT and REVOKE
which mainly deals with the rights, permissions and other controls of the database system.
Examples of DCL commands:
GRANT-gives user’s access privileges to database.
REVOKE-withdraw user’s access privileges given by using the GRANT command.
TCL(transaction Control Language) : TCL commands deals with the transaction within the
database.
Examples of TCL commands:
COMMIT– commits a Transaction.
ROLLBACK– rollbacks a transaction in case of any error occurs.
SAVEPOINT–sets a savepoint within a transaction.
SET TRANSACTION–specify characteristics for the transaction.

You might also like