100% found this document useful (1 vote)
246 views28 pages

SQL Commands

Structured Query Language (SQL) is used to manage data in relational databases. SQL provides four types of languages - Data Definition Language, Data Manipulation Language, Data Control Language, and Transaction Control Language - based on the type of operation to be performed on a database. Data Definition Language is used to specify the structure of a database, Data Manipulation Language enables users to access or manipulate data, Data Control Language provides access privileges to database objects, and Transaction Control Language specifies commands for transactions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
246 views28 pages

SQL Commands

Structured Query Language (SQL) is used to manage data in relational databases. SQL provides four types of languages - Data Definition Language, Data Manipulation Language, Data Control Language, and Transaction Control Language - based on the type of operation to be performed on a database. Data Definition Language is used to specify the structure of a database, Data Manipulation Language enables users to access or manipulate data, Data Control Language provides access privileges to database objects, and Transaction Control Language specifies commands for transactions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 28

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.

Non-Integral Data Type:

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.

Scale is the number of digits


allowed after the decimal point.

Precision is the total number of


significant digits i.e. digits both
before and after the decimal point.
If Scale is not provided then
“NUMBER” data type can be used to
store integral values.
Precision and Scale of non integral data type determines the maximum value that can be stored. It also
determines how input data will be modified before storing the value.
Operators

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

Create table Student ( StudentId INTEGER, F Name VARCHAR2(10),


Gender CHAR(1), Contact NUMBER(10) );
Constraints
Constraints are typically specified along with the CREATE TABLE statement. Constraints are classified into
multiple types based on the number of columns they act upon as well as on the way they are specified.

Various constraints that can be created on database tables are:

● 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) );

● TABLE LEVEL CONSTRAINT:


○ Create table Student ( STUDENTID INTEGER, F Name VARCHAR2(10), ContactNO NUMBER(10) ,
PRIMARY KEY (STUDENTID));
Constraints..
CHECK constraint is used to limit the values that can be specified for a column.

E.g. create a CHECK constraint on Gender column to only allow 'M' (Male) and 'F' (Female) values.

● COLUMN LEVEL CONSTRAINT:


○ Create table Student ( StudentId INTEGER, F Name VARCHAR2(10), Gender CHAR(1)
CHECK(GENDER IN(‘M’, ‘F’)), Contact NUMBER(10) );

● TABLE LEVEL CONSTRAINT:


○ Create table Student ( StudentId INTEGER, F Name VARCHAR2(10), Gender CHAR(1), Contact
NUMBER(10), CHECK(GENDER IN(‘M’, ‘F’)) );
Constraints..
UNIQUE constraint on a column ensures that two rows in a table cannnot have same value in that
column. Unlike Primary Key, UNIQUE constraint allows NULL values. A table can have many UNIQUE
constraints.
E.g. create Unique constraint on ContactNo so that two student cannot have the same contact details.
● COLUMN LEVEL CONSTRAINT:
○ Create table Student ( StudentId INTEGER, F Name VARCHAR2(10), Gender CHAR(1) , Contact
NUMBER(10) UNIQUE );

● TABLE LEVEL CONSTRAINT:


○ Create table Student ( StudentId INTEGER, F Name VARCHAR2(10), Gender CHAR(1), Contact
NUMBER(10), UNIQUE);
Need for Foreign Key
Student Table has already been created and inserted with few records in the database.

Let us now create a table Marks which stores marks scored by each Student in each Course.

Create Table Marks (CourseId INTEGER, StudentId INTEGER, MarksScored Decimal(5,2));

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, StudentId INTEGER REFERENCES STUDENT,


MarksScored Decimal(5,2));

● TABLE LEVEL CONSTRAINT:


○ Create Table Marks (CourseId INTEGER, StudentId INTEGER, MarksScored Decimal(5,2),
FOREIGN KEY (StudentId) REFERENCES STUDENT(StudentId));

○ Create Table Marks (CourseId INTEGER, StudentId INTEGER, MarksScored Decimal(5,2),


FOREIGN KEY (StudentId) REFERENCES STUDENT);
Need for Composite Primary Key
Let us now create a table Marks which stores MarksScored by each Student in each Course

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

Create Table Marks (CourseId INTEGER, StudentId INTEGER REFERENCES STUDENT(StudentId)


, MarksScored Decimal(5,2), PRIMARY KEY(CourseId, StudentId));
Alter Table
How do we add a new column to a table that already exists?

One option is to drop the table and create it again.


What if the table has lot of data and we do not want to lose existing data? We cannot drop the
table now. One option is to take a backup of data, then drop and recreate the table and finally
load the data from backup into the modified table. This approach might work but if number of
rows are large then this process will take lot of time.

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:

ALTER TABLE Student RENAME COLUMN Id TO SID;


Alter table..

Drop Columns:

ALTER TABLE Student DROP (DOJ);

You might also like