Data Definition Language
Data Definition Language
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
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:
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:
Example:
ALTER TABLE Person DROP COLUMN
weight_in_Kgs;
To rename a column in an existing table:
Syntax:
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:
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 );
Example:
SELECT * FROM person;
Selected columns and all rows
Select<columnname1>,<columnname2>, from
<tablename>;
Example:
DELETE FROM person;
Removal of Specific Rows
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;