SQL
SQL
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
CREATE command
CREATE is a DDL SQL command used to create a table or a database in relational database
management system.
Creating a Database
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.
create table command will tell the database system to create a new table with the given table name
and column information.
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:
Here we have listed some of the most commonly used datatypes used for columns in tables.
Datatype Use
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).
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.
Using ALTER command we can even add multiple new columns to any existing table. Following
is the syntax,
The above command will add three new columns to the student table
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
);
The above command will add a new column with a preset default value to the table student.
ALTER command can also be used to modify data type of any existing column. Following is the
syntax,
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.
Using ALTER command you can rename an existing column. Following is the syntax,
ALTER command can also be used to drop or remove columns. Following is the syntax,
The above command will drop the address column from the table student
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,
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,
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,
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,