0% found this document useful (0 votes)
6 views

SQL

SQL, or Structured Query Language, is categorized into four main types: DML (Data Manipulation Language), DDL (Data Definition Language), DCL (Data Control Language), and TCL (Transaction Control Language), each serving distinct functions for database management. DDL is used to define and modify database structures, while DML is focused on manipulating data within those structures. The document also provides examples and syntax for various SQL commands, including creating, altering, and deleting database objects.

Uploaded by

sonkararyan0000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

SQL

SQL, or Structured Query Language, is categorized into four main types: DML (Data Manipulation Language), DDL (Data Definition Language), DCL (Data Control Language), and TCL (Transaction Control Language), each serving distinct functions for database management. DDL is used to define and modify database structures, while DML is focused on manipulating data within those structures. The document also provides examples and syntax for various SQL commands, including creating, altering, and deleting database objects.

Uploaded by

sonkararyan0000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SQL

STRUCTURED QUERY LANGUAGE is divided into four types of primary language


statements: DML, DDL, DCL and TCL. Using these statements, we can define the structure of a
database by creating and altering database objects, and we can manipulate data in a table through
updates or deletions. We also can control which user can read/write data or manage transactions to
create a single unit of work.

The four main categories of SQL statements are as follows:

1. DML (Data Manipulation Language)


2. DDL (Data Definition Language)
3. DCL (Data Control Language)
4. TCL (Transaction Control Language)

Data Definition Language (DDL) statements are used to define the database structure or
schema. Some examples:
CREATE - to create objects in the database
ALTER - alters the structure of the database
DROP - delete objects from the database
TRUNCATE - remove all records from a table, including all spaces allocated for the
records are removed
COMMENT - add comments to the data dictionary
RENAME - rename an object

Data Manipulation Language (DML) statements are used for managing data within schema
objects. Some examples:
SELECT - retrieve data from the a database
INSERT - insert data into a table
UPDATE - updates existing data within a table
DELETE - deletes all records from a table, the space for the records remain
MERGE - UPSERT operation (insert or update)
CALL - call a PL/SQL or Java subprogram
EXPLAIN PLAN - explain access path to data
LOCK TABLE - control concurrency

Data Control Language (DCL) statements. Some examples:


GRANT - gives user's access privileges to database
REVOKE - withdraw access privileges given with the GRANT command
Transaction Control (TCL) statements are used to manage the changes made by DML
statements. It allows statements to be grouped together into logical transactions.
COMMIT - save work done
SAVEPOINT - identify a point in a transaction to which you can later roll back
ROLLBACK - restore database to original since the last COMMIT
SET TRANSACTION - Change transaction options like isolation level and what
rollback segment to use

Data Definition Language (DDL)

CREATE command

CREATE is a DDL SQL command used to create a table or a database in relational database
management system.

Creating a Database

To create a database in RDBMS, create command is used. Following is the syntax,

CREATE DATABASE <DB_NAME>;

Example for creating Database


CREATE DATABASE Test;

The above command will create a database named Test, which will be an empty schema without
any table.

To create tables in this newly created database, we can again use the create command.

Creating a Table

create command can also be used to create tables. Now when we create a table, we have to specify
the details of the columns of the tables too. We can specify the names and data-types of various
columns in the create command itself.

Following is the syntax,


CREATE TABLE <TABLE_NAME>
(
column_name1 datatype1,
column_name2 datatype2,
column_name3 datatype3,
column_name4 datatype4
);

create table command will tell the database system to create a new table with the given table name
and column information.

Example for creating Table


CREATE TABLE Student(
student_id INT,
name VARCHAR(100),
age INT);

The above command will create a new table with name Student in the current database with 3
columns, namely student_id, name and age. Where the column student_id will only store integer,
name will hold upto 100 characters and age will again store only integer value.

If you are currently not logged into your database in which you want to create the table then you
can also add the database name along with table name, using a dot operator .

For example, if we have a database with name Test and we want to create a table Student in it,
then we can do so using the following query:

CREATE TABLE Test.Student(


student_id INT,
name VARCHAR(100),
age INT);

Most commonly used datatypes for Table columns

Here we have listed some of the most commonly used datatypes used for columns in tables.

Datatype Use

INT used for columns which will store integer values.


FLOAT used for columns which will store float values.

DOUBLE used for columns which will store float values.

used for columns which will be used to store characters and integers, basically a
VARCHAR
string.

CHAR used for columns which will store char values(single character).

DATE used for columns which will store date values.

used for columns which will store text which is generally long in length. For
TEXT example, if you create a table for storing profile information of a social networking
website, then for about me section you can have a column of type TEXT.

ALTER Command: Add multiple new Columns

Using ALTER command we can even add multiple new columns to any existing table. Following
is the syntax,

ALTER TABLE table_name ADD(


column_name1 datatype1,
column-name2 datatype2,
column-name3 datatype3);

Here is an Example for this,

ALTER TABLE student ADD(


father_name VARCHAR(60),
mother_name VARCHAR(60),
dob DATE);

The above command will add three new columns to the student table

ALTER Command: Add Column with default value

ALTER command can add a new column to an existing table with a default value too. The default
value is used when no value is inserted in the column. Following is the syntax,
ALTER TABLE table_name ADD(
column-name1 datatype1 DEFAULT some_value
);

Here is an Example for this,

ALTER TABLE student ADD(


dob DATE DEFAULT '01-Jan-99'
);

The above command will add a new column with a preset default value to the table student.

ALTER Command: Modify an existing Column

ALTER command can also be used to modify data type of any existing column. Following is the
syntax,

ALTER TABLE table_name modify(


column_name datatype
);

Here is an Example for this,

ALTER TABLE student MODIFY(


address varchar(300));

Remember we added a new column address in the beginning? The above command will modify
the address column of the student table, to now hold upto 300 characters.

ALTER Command: Rename a Column

Using ALTER command you can rename an existing column. Following is the syntax,

ALTER TABLE table_name RENAME


old_column_name TO new_column_name;

Here is an example for this,


ALTER TABLE student RENAME
address TO location;

The above command will rename address column to location.

ALTER Command: Drop a Column

ALTER command can also be used to drop or remove columns. Following is the syntax,

ALTER TABLE table_name DROP(


column_name);

Here is an example for this,

ALTER TABLE student DROP(


address);

The above command will drop the address column from the table student

Truncate, Drop or Rename a Table

In this tutorial we will learn about the various DDL commands which are used to re-define the
tables.

TRUNCATE command

TRUNCATE command removes all the records from a table. But this command will not destroy
the table's structure. When we use TRUNCATE command on a table its (auto-increment) primary
key is also initialized. Following is its syntax,

TRUNCATE TABLE table_name

Here is an example explaining it,

TRUNCATE TABLE student;

The above query will delete all the records from the table student.
In DML commands, we will study about the DELETE command which is also more or less same
as the TRUNCATE command. We will also learn about the difference between the two in that
tutorial.

DROP command

DROP command completely removes a table from the database. This command will also destroy
the table structure and the data stored in it. Following is its syntax,

DROP TABLE table_name

Here is an example explaining it,

DROP TABLE student;

The above query will delete the Student table completely. It can also be used on Databases, to
delete the complete database. For example, to drop a database,

DROP DATABASE Test;

The above query will drop the database with name Test from the system.

RENAME query

RENAME command is used to set a new name for any existing table. Following is the syntax,

RENAME TABLE old_table_name to new_table_name

Here is an example explaining it.

RENAME TABLE student to students_info;

The above query will rename the table student to students_info.

Data Manipulation Language (DML)

Using INSERT SQL command

You might also like