0% found this document useful (0 votes)
38 views24 pages

Data Definition Language

The document defines various data types that can be used in columns when creating tables in SQL, including text, numeric, date/time data types. It provides the syntax for creating a table, adding, modifying and deleting columns. It also describes commands for inserting, selecting, updating, deleting and truncating data in tables.

Uploaded by

Goldi Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views24 pages

Data Definition Language

The document defines various data types that can be used in columns when creating tables in SQL, including text, numeric, date/time data types. It provides the syntax for creating a table, adding, modifying and deleting columns. It also describes commands for inserting, selecting, updating, deleting and truncating data in tables.

Uploaded by

Goldi Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Data Definition

Language
DATA TYPES
Text Use for text or combinations of text and
numbers. 255 characters maximum
Byte Allows whole numbers from 0 to 255 1 byte
Integer Allows whole numbers between -32,768
and 32,767 2 bytes
Long Allows whole numbers between
-2,147,483,648 and 2,147,483,647 4 bytes
DATA TYPES
Single Single precision floating-point. Will handle
most decimals 4 bytes
Double Double precision floating-point. Will
handle most decimals
Date/Time Use for dates and times.
Create Table Statement
The create table defines each column of table
uniquely
Each table column definition is separated from
other by comma
SQL statement is terminated by semicolon

Rules for creating a name of table:


Can have up to maximum of 30 characters
A-Z, a-z alphabets and 0-9 numbers are allowed
Should begin with an alphabet
Reserve words are not allowed
Syntax of Create Table Statement
CREATE TABLE table_name ( column1
datatype, column2 datatype, column3
datatype, .... );

Example:
CREATE TABLE Person ( PersonID int, LastName
varchar(255), FirstName varchar(255), Address
varchar(255), City varchar(255) );
Create table using another table
A copy of an existing table can be created using
a combination of the CREATE TABLE statement
and the SELECT statement.

Syntax:
CREATE TABLE new_table_name AS SELECT
column1, column2,...
FROM existing_table_name;
Example of creating table from another table:

Create table Person2 as select PersonID,


FirstName From Persons;
Alter Table Statement
A DBA can make changes to the table structure
or column definitions after the table has been
created in the database.
The DDL command ALTER TABLE is used to
perform such actions.
The ALTER TABLE statement is used to add,
drop, rename, and modify a column in a table.
To Add a column in an existing table:

Syntax:
ALTER TABLE table_name ADD column_name
datatype;

Example:
ALTER TABLE Person ADD weight_in_Kgs int;
To delete a column in an existing table:

Syntax:

ALTER TABLE table_name DROP COLUMN


column_name;

Example:
ALTER TABLE Person DROP COLUMN
weight_in_Kgs;
To rename a column in an existing table:

Syntax:

ALTER TABLE table_name RENAME COLUMN


existing_column_name
To new_column_name;

Example:
ALTER TABLE Person RENAME COLUMN
weight_in_Kgs TO Wght_in_kgs;
To modify a table by changing the data type of a
column in a table

Syntax:

ALTER TABLE table_name MODIFY column_name


datatype;

Example:
ALTER TABLE Person MODIFY Wght_in_kgs
varchar(90);
INSERTING DATA INTO TABLE:
The insert operation on inserting a single row:
Creates an empty row in database table
Loads the value passed by insert command
into the specified columns

Syntax:
INSERT INTO tablename (Column_name1,
Column_name2) values (expression1,
expression2);
Example:
INSERT INTO person (personID, firstname, lastname,
address) VALUES (23, ravi, dubey, patiala );

Inserting data into a table from another table


INSERT INTO <tablename> select
<columnname1>,<columnname2> from
<tablename>;
VIEWING DATA IN TABLES
The select command is used to retrieve rows selected
from one or more tables
To select all rows and all columns:
SELECT Column1, , Column2 from tablename;
OR
SELECT * from tablename;

Example:
SELECT * FROM person;
Selected columns and all rows

Select<columnname1>,<columnname2>, from
<tablename>;

Example: Select Name,Roll_no from Student;


Selected Rows and All Columns

Select * from <tablename> where <condition>;

Example :select * from student where name=xyz;


Selected columns and selected
rows
Select <columnname1>,<columnname2> from
<tablename> where <condition>;
Example: Select name,roll no from student where
roll no=1;
Eliminating duplicate rows while
using select statement
Select distinct <columnname1>,<columnname2>
from <tablename>;
Select distinct * from student;
Sorting data in a table
Select * from <tablename> order by
<columnname1>,<columnname2> <[sort order];
By default ascending order
Select * from student order by name desc;
DELETE OPERATIONS
Delete command deletes rows from table that
satisfies given condition represented by where clause
and returns number of records deleted.

Removal of all rows


Syntax:
DELETE FROM tablename;

Example:
DELETE FROM person;
Removal of Specific Rows

Delete from <tablename> where<condition>;

Delete from student where name=xyz;


Updating the Contents of a Table

Updating All Rows


Update <tablename> set
<columnName1>=<Expression1>,<ColumnName2=
<Expression2>;
Update Student set roll_no=1;

Updating Records Conditionally


Update <tablename>
set<columnName1>=<Expression1>,<ColumnName
2= <Expression2> where<condition>;
TRUNCATING TABLES
It empties a table completely.
Truncate table differs from delete in following ways:
Truncate drop and recreate the table
So, its much faster than deleting rows one by one
Truncate operations are not transaction safe
The number of deleted rows are not returned

Syntax:
TRUNCATE TABLE tablename;
Example:
Truncate table person;
DESTROYING TABLES
Drop table statement with table name can destroy a
specific table

Syntax:
DROP TABLE tablename;

Example:
DROP TABLE person;

You might also like