SQL Introduction: DR - Aparna Chaparala
SQL Introduction: DR - Aparna Chaparala
Dr.Aparna Chaparala
SQL is a standard language for accessing and
manipulating databases.
Syntax
CREATE TABLE table_name (
column1 datatype [ NULL | NOT NULL ] [constraint definition],
column2 datatype [ NULL | NOT NULL ] [constraint definition], CREATE TABLE table_name
... AS (Query);
column_n datatype [ NULL | NOT NULL ] [constraint definition],
[Table level constraint definition] );
Character Types
• CHAR Datatype
• The CHAR datatype stores fixed-length character strings
• VARCHAR2
• The VARCHAR2 datatype stores variable-length character
strings
• VARCHAR
Oracle 8i • The VARCHAR datatype is currently synonymous with the
VARCHAR2 datatype.
Types • NUMBER
• can store numeric values with the maximum range and
precision
Date Type
• Date
• storing date data
Constraints
NOT NULL
• A NOT NULL constraint prohibits a database value from being null.
UNIQUE
• A unique constraint prohibits multiple rows from having the same value in the same column or
combination of columns but allows some values to be null.
PRIMARY KEY
• A primary key constraint combines a NOT NULL constraint and a unique constraint in a single declaration.
That is, it prohibits multiple rows from having the same value in the same column or combination of
columns and prohibits values from being null.
FOREIGN KEY
• A foreign key constraint requires values in one table to match values in another table.
CHECK
• A check constraint requires a value in the database to comply with a specified condition.
Steps for creating a table
Identify columns
Identify columns Identify primary
Identify data types that must be
that can and key–foreign key
for attributes unique (candidate
cannot be null mates
keys)
Identify constraints
Determine default on columns
Create the table
values (domain
specifications)
ALTER TABLE Statement
ADD Clause
• ALTER TABLE table_name ADD column_name column-definition;
• ALTER TABLE table_name ADD (column_1 column-definition, column_2 column-
definition, ... column_n column_definition);
MODIFY Clause
• ALTER TABLE table_name MODIFY column_name column_type;
• ALTER TABLE table_name MODIFY (column_1 column_type, column_2 column_type, ... column_n column_type);
DROP Clause
• ALTER TABLE table_name DROP COLUMN column_name;
DROP & TRUNCATE TABLE Statements
DROP [schema_name].TABLE table_name
[ CASCADE CONSTRAINTS ] [ PURGE ];
TRUNCATE TABLE [schema_name.]table_name;
Insert Statement
INSERT INTO table (column1, column2, ... column_n )
VALUES (expression1, expression2, ... expression_n );
INSERT INTO table (column1, column2, ... column_n ) SELECT
expression1, expression2, ... expression_n FROM source_table WHERE conditions;
UPDATE Statement
UPDATE table SET column1 = expression1, column2 = expression2, ... column_n = expression_n
WHERE conditions;
DELETE Statement
DELETE FROM table_name WHERE conditions;