SQL Commands
SQL Commands
Structured Query Language (SQL) is used to manage data in all relational databases like DB2, Oracle,
SQL Server etc. SQL standards are maintained by ISO. Structured Query Language (SQL) provides four
types of languages based on type of operation to be performed on a database. These languages can be
considered as subsets of SQL and logical groups only. Physically commands for all these languages are
executed from the same interface provided by the database.
Types of SQL
1. Data Definition Language is used to specify the structure i.e. schema of a relational database. DDL
provides commands for creation, modification and deletion of various database objects like tables,
views, stored procedures, indexes, constraints etc. The output of DDL is placed in data dictionary
which contains metadata i.e. data about data.
2. Data Manipulation Language enables users to access or manipulate data in a relational database. DML
provides commands for retrieval, creation, deletion and modification of information in a database.
DML requires a user to specify what data is needed without specifying how to get it. The database
engine is left to figure out effective means of retrieving data.
3. Data Control Language enables users to provide access to various database objects like views, tables,
stored procedures etc. in a relational database. Typically only DBAs have access to grant and revoke
privileges. Whenever a user submits a query, the database checks against the granted privileges and
rejects the query if it is not authorized.
4. Transaction Control Language specifies commands for beginning and ending a transaction. A
transaction consists of a sequence of SQL statements that are applied in an atomic (all or none)
manner. A commit makes all the changes applied by the transaction permanent on the database while
a rollback undoes all the changed applied by the transaction.
Data Type
Character Data Type:
1. Char (n):
i. for predefined length of string
ii. size for n characters
iii. 2000 bytes
iv. A CHAR(10) field will store "Hello" as 10 bytes by appending 5 trailing spaces.
v. Alternate name “Character(N)”
2. Varchar2(n):
i. for varying length of string
ii. size for actual no. of characters + fixed size to store length
iii. 4000 bytes
iv. A VARCHAR2(10) field will store "Hello" as 7 bytes (assuming 2 bytes to store length).
v. Alternate name “CHARACTER VARYING(n)”
Integral Data Type:
SMALLINT, INTEGER and INT data types that are used for storing whole numbers. All integral data
type internally to have 38 digit of precision.
Nonintegral data types have an integer part and a fractional part. Either NUMERIC, DECIMAL or
NUMBER data types can be used to store non-integral numbers.
There is one important difference between Equal To comparison operator in programming languages and
SQL. While SQL uses a single '=', programming languages typically use double '=' to distinguish it from
the assignment operator.
Expressions:
SQL expressions are created from constant values, operators and brackets. They evaluate to a single value
and are used in SELECT and WHERE clauses. Some examples are provided below:
DDL Statements for SQL
CREATE TABLE statement is used to create a table in a database. Database tables are organized into rows
and columns. Each table must have a name and can have any number of columns (minimum 1 column is
required). Each column must have a data type which determines the type of values that can be stored.
CREATE TABLE command will fail if a table is already existing in the database with same name. All tables
must have a unique name.
DROP TABLE statement is used to remove an existing table from the database.
CREATE TABLE
● NOT NULL
● PRIMARY KEY
● CHECK
● UNIQUE
● FOREIGN KEY
We can also specify DEFAULT value for a column. Oracle database does not consider DEFAULT as a
constraint.
Constraints..
NOT NULL Constraint prevents a column from accepting NULL values. NOT NULL can only be applied as a
column level constraint. Constraint name is optional and it can be specified by using CONSTRAINT keyword.
E.g. NOT NULL constraints on StudentId and FName columns.
Create table Student ( STUDENTID INTEGER NOT NULL, FNAME VARCHAR2(10) NOT NULL, LNAME
VARCHAR2(10));
Constraints..
A column can be given the default value by using DEFAULT option. The data type of column and default
expression must be the same. DEFAULT option can be provided for nullable as well as NOT NULL attributes.
Oracle database does not consider DEFAULT as a constraint.
Create table Student ( STUDENTID INTEGER, FName VARCHAR2(10),DOJ DATE DEFAULT SYSDATE );
Constraints..
PRIMARY KEY constraint on a column ensures that the column cannot contain NULL and duplicate values.
We can have only one PRIMARY KEY in a table.
E.G. Student table with primary key constraint on STUDENTID column.
● COLUMN LEVEL CONSTRAINT:
○ Create table Student ( STUDENTID INTEGER PRIMARY KEY, F Name VARCHAR2(10), ContactNO
NUMBER(10) );
E.g. create a CHECK constraint on Gender column to only allow 'M' (Male) and 'F' (Female) values.
Let us now create a table Marks which stores marks scored by each Student in each Course.
The student with id 2001 is not a valid value as it is not present in the student table. To avoid this
problem, the relationship has to be built between the Student and Marks table. This can be achieved
by using FOREIGN KEY.
Constraints..
Let us now create a table Marks which stores marks scored by each Student in each Course
● COLUMN LEVEL CONSTRAINT:
○ Create Table Marks (CourseId INTEGER, StudentId INTEGER REFERENCES STUDENT
(StudentId), MarksScored Decimal(5,2));
Create Table Marks (CourseId INTEGER PRIMARY KEY, StudentId INTEGER REFERENCES STUDENT(StudentId)
, MarksScored Decimal(5,2));
Students are not able to register for a course which is already registered by another Student e.g. Student
1002 could not register for course 801 as it is already registered by student 1001. Students should be able to
register for many courses. This problem can be solved by using composite primary key with columns
(CourseId, StudentId).
Constraints..
Let us now create a table Marks which stores MarksScored by each Student in each Course
We need to use ALTER TABLE command through which the structure of existing table can be changed
without any loss of data. ALTER table can also be used to rename a column, change data type of a column
and add or remove constraints.
Alter table..
Syntax:
Alter table..
Student
Add Columns:
ALTER TABLE Student ADD Address VARCHAR2 (20);
Alter table..
Modify Columns:
ALTER TABLE Student MODIFY Name VARCHAR2(50);
Alter table..
Rename Columns:
Drop Columns: