0% found this document useful (0 votes)
4 views18 pages

Unit II

The document provides an overview of SQL (Structured Query Language), detailing its components such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Query Language (DQL), Data Control Language (DCL), and Transaction Control Language (TCL). It also covers data types in SQL, how to create and manage tables, and various SQL statements for manipulating table data including INSERT, UPDATE, DELETE, and SELECT. Additionally, it includes examples of syntax for each SQL operation and the structure of SQL commands.

Uploaded by

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

Unit II

The document provides an overview of SQL (Structured Query Language), detailing its components such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Query Language (DQL), Data Control Language (DCL), and Transaction Control Language (TCL). It also covers data types in SQL, how to create and manage tables, and various SQL statements for manipulating table data including INSERT, UPDATE, DELETE, and SELECT. Additionally, it includes examples of syntax for each SQL operation and the structure of SQL commands.

Uploaded by

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

B SC CS SY Unit II – SQL statements & Working with Tables

Unit II
SQL statements & Working with Tables

* Structured Query Language –


SQL stands for Structured Query Language. SQL was one of the first commercial languages for
Edgar F. Codd's relational model. SQL was initially developed at IBM by Donald D. Chamberlin and
Raymond F. Boyce.
This version, initially called SEQUEL (Structured English Query Language. The acronym SEQUEL
was later changed to SQL because "SEQUEL" was a trademark of the UK-based Hawker Siddeley
aircraft company.
According to ANSI (American National Standards Institute), it is the standard language for
relational database management systems.
SQL statements are used to perform tasks such as update data on a database, or retrieve data
from a database. SQL consists of a Data Definition Language (DDL), Data Manipulation Language
(DML), Data Query Language (DQL), Data Control Language (DCL), and Transaction Control Language
(TCL).
Some common relational database management systems that use SQL are: Oracle, Microsoft
SQL Server, Access, etc.

a) DDL –
DDL stands for Data Definition Language. DDL statements create, modify, and remove database
objects such as tables, indexes, users, etc. Common DDL statements are CREATE, ALTER, and DROP.

1. CREATE – To create database and its objects like (table, index, views, function and triggers).
2. ALTER – Modify the structure of the existing database and its objects.
3. DROP – Remove database and its objects from the database.
4. TRUNCATE – Remove all records from a table.
5. RENAME – To give new name to a database object.

b) DML –
Data Manipulation Language (DML) statements are used for managing data in database. There
are three basic DML commands: INSERT, UPDATE, and DELETE

1. INSERT – Insert data into a table.


2. UPDATE – Updates existing data within a table.
3. DELETE – Deletes all records from a table, the space for the records remain.

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B SC CS SY Unit II – SQL statements & Working with Tables
c) DQL –
DQL stands for Data Query Language. This command is the heart of SQL. When SELECT is fired
against a table or tables the result is compiled into a further temporary table, which is displayed or
perhaps received by the program i.e. front end.

1. SELECT – Retrieve data from the database object.

d) DCL –
DCL stands for Data Control Language.
Privileges are of two types, SYSTEM and OBJECT. Common DCL statements are GRANT &
REVOKE.
1. GRANT – It gives user's access privileges to database.
2. REVOKE – It withdraw access privileges given with the GRANT command.

e) TCL –
Transaction Control Language (TCL) statements are used to manage the changes made by DML
statements. Common TCL statements are COMMIT and ROLLBACK.

1. COMMIT – Commit command is used to permanently save any transaction into database.
2. ROLLBACK - Restore database to original since the last COMMIT.

* Data types in SQL –


Following are the some commonly used data types,

1) NUMBER (p, s) –
The NUMBER data type is used to store numbers (fixed or floating point). Numbers of virtually
any magnitude may be stored up to 38 digits of precision. The precision, (p) determines the maximum
length of data, whereas scale, (s) determines the number of places to the right of the decimal.

2) CHAR (size) –
This data type is used to store character strings values of fixed length. The size in the brackets
determines the number of characters the cell can hold. The maximum number of characters (i.e. the
size) this data type can hold is 255 characters.

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B SC CS SY Unit II – SQL statements & Working with Tables
3) VARCHAR (size) –
This data type is used to store variable length alphanumeric data. The size in the brackets
determines the number of characters the cell can hold. The maximum number of characters (i.e. the
size) this data type can hold is 2000 characters.

4) VARCHAR2 (size) –
This data type is also used to store variable length alphanumeric data. The size in the brackets
determines the number of characters the cell can hold. The maximum number of characters (i.e. the
size) this data type can hold is 4000 characters.

5) LONG –
This data type is used to store variable length character strings containing up to 2 GB. Only one
LONG value can be defined per table. LONG values cannot be used in subqueries, functions, where
clauses or indexes.

6) DATE –
The DATE data type is used to represent date & time. The standard format is DD-MON-YY as in
11-SEP-85.
The default date for a date is the first day of the current month. Valid dates range from January
1, 4712 BC (Before Christ) to December 31, 9999 AD (Anno Domini).

7) RAW (size) / LONG RAW –

The RAW / LONG RAW data type is used to store binary data, such as digitized picture or
image. Data loaded into columns of these data types are stored without any further conversion. RAW
data type can have a maximum length of 255 bytes. LONG RAW data type can contain up to 2 GB.
RAW / LONG RAW data cannot be manipulated. RAW / LONG RAW data is always returned as a
hexadecimal character value.

* Creating & Managing Tables –


1) CREATE TABLE –
The CREATE TABLE statement allows you to create and define a table in a database. This
command defines each column of the table uniquely. Each column has a minimum of three attributes,
a name, datatype and size. Each table column definition is separated from the other by a comma.

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B SC CS SY Unit II – SQL statements & Working with Tables
Syntax –
CREATE TABLE tbl_name
(
column1 datatype [ NULL | NOT NULL],
column2 datatype [ NULL | NOT NULL],
...
column_n datatype [ NULL | NOT NULL]
);

Example –
SQL > CREATE TABLE student
(
RN NUMBER (2) NOT NULL,
NAME VARCHAR2 (10) NOT NULL,
CITY VARCHAR2 (10)
);
Table created.

Above example creates a table called STUDENT which has 3 columns.


 The first column is called RN which is created as a NUMBER datatype (maximum 2 digits
in length) and cannot contain null values.
 The second column is called NAME which is a VARCHAR2 datatype (10 maximum
characters in length) and also can not contain null values.
 The third column is called CITY which is a VARCHAR2 datatype but can contain null
values.
You can also use the CREATE TABLE AS statement to create a table from an existing table by
copying the existing table's columns.
It is important to note that when creating a table in this way, the new table will be populated

with the records from the existing table (based on the SELECT Statement).

a) CREATE TABLE AS – By copying all columns from another table


Example –
SQL> CREATE TABLE stud1 AS (SELECT * FROM student);

This example would create a new table called STUD1 that included all columns from the
STUDENT table. If there were records in the STUDENT table, then the new STUD1 table would
be populated with the records returned by the SELECT statement.

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B SC CS SY Unit II – SQL statements & Working with Tables
b) CREATE TABLE AS – By copying selected columns from another table
Example –
SQL> CREATE TABLE stud3 AS (SELECT RN, NAME FROM student);

This example would create a new table called STUD3 that included RN and NAME columns
from the STUDENT table. If there were records in the STUDENT table, then the new STUD3
table would be populated with the records returned by the SELECT statement.

2) Altering Table Structure: –


The ALTER TABLE statement is used to add, modify, or drop/delete columns in a table. The
ALTER TABLE statement is also used to rename a table or rename a column.

a) Add column in table –


Syntax –
ALTER TABLE tbl_name ADD column_name column-definition;

Example –
SQL> ALTER TABLE student ADD MOBILE_NO NUMBER (10);
Table altered.

This example will add a column called MOBILE_NO to the STUDENT table.

b) Modify column in table –


Syntax –
ALTER TABLE tbl_name MODIFY column_name new_column-definition;

Example –
SQL> ALTER TABLE student MODIFY MOBILE_NO NUMBER (11) UNIQUE;
Table altered.

This example will modify the column called MOBILE_NO to be a data type of NUMBER (11) and
force the column to not allow duplicate values.

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B SC CS SY Unit II – SQL statements & Working with Tables
c) Drop column in table –
Syntax –
ALTER TABLE tbl_name DROP COLUMN column_name;

Example –
SQL> ALTER TABLE student DROP COLUMN NATIONALITY;
Table altered.

This example will drop the column called NATIONALITY from the table called STUDENT.

d) Rename column in table –


Syntax –
ALTER TABLE tbl_name RENAME COLUMN old_column_name to new_column_name;

Example –
SQL> ALTER TABLE student RENAME COLUMN MOBILE_NO to MNO;
Table altered.

This ALTER TABLE example will rename the column called MOBILE_NO to MNO.

e) Rename table –
Syntax –
ALTER TABLE tbl_name RENAME TO new_tbl_name;

Example –
SQL> ALTER TABLE student RENAME TO stud_info;
Table altered.
This example will rename the STUDENT table to STUD_INFO.

3) RENAME –
Oracle allows renaming of tables. The rename operation is done atomically, which means that
no other thread can access any of the tables while the rename process is running.

Syntax –
RENAME old_tbl_name TO new_tbl_name;

Example –
SQL> RENAME stud_info TO student;
Table renamed.

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B SC CS SY Unit II – SQL statements & Working with Tables
4) TRUNCATE TABLE –
The TRUNCATE TABLE statement is used to remove all records from a table. It performs the
same function as a DELETE statement without a WHERE clause.

Note: If you truncate a table, the TRUNCATE TABLE statement cannot be rolled back.

Syntax –
TRUNCATE TABLE tbl_name;

Example –
SQL> TRUNCATE TABLE student;
Table truncated.

This example would truncate the table called STUDENT and remove all records from that table.

It would be equivalent to the following DELETE statement,


DELETE FROM student;

5) DROP TABLE –
The DROP TABLE statement allows you to remove or delete a table from the Oracle database.

Syntax –
DROP TABLE tbl_name [CASCADE CONSTRAINTS]

Example –
SQL> DROP TABLE student;
Table dropped.

If there are referential integrity constraints on STUDENT and you do not specify the
CASCADE CONSTRAINTS option, the DROP TABLE statement will return an error and Oracle will
not drop the table.
3) DROP TABLE student CASCADE CONSTRAINTS
Table dropped.
This example would drop the table called STUDENT as well as all referential integrity
constraints.

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B SC CS SY Unit II – SQL statements & Working with Tables
* Manipulation Table Data:–
1) INSERT –
The SQL INSERT statement is used to insert a record into a table. There are several ways to
insert a record in a table,
CREATE TABLE student
(
RN NUMBER (2),
NAME VARCHAR2 (10),
CITY VARCHAR2 (10)
);
Table created.

a) Syntax (Insert values in all columns) –


INSERT INTO tbl_name VALUES (value1, value2, .. );

Example –
SQL> INSERT INTO student VALUES (1, 'AMOL', 'LATUR');
1 row created.

SQL> INSERT INTO student VALUES (2, 'ATUL', 'LATUR');


1 row created.
b) Syntax (Insert values in selected columns) –
INSERT INTO tbl_name (column1, column2, .. ) VALUES (value1, value2, .. );

Example –
SQL> INSERT INTO student (RN, NAME) VALUES (3, 'RAHUL');
1 row created.

c) Syntax (Insert values in all columns through column reference: & sign) –
INSERT INTO tbl_name VALUES (&column1, &column2, .. );

Example –
SQL> INSERT INTO student VALUES (&RN, '&NAME', '&CITY');
Enter value for rn: 4
Enter value for name: BALAJI
Enter value for city: AUSA
old 1: INSERT INTO student VALUES(&RN,'&NAME','&CITY')
new 1: INSERT INTO student VALUES(4,'BALAJI','AUSA')
1 row created.

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B SC CS SY Unit II – SQL statements & Working with Tables

SQL> SELECT * FROM student;

RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 SAMEER
4 BALAJI AUSA

2) UPDATE –
The UPDATE statement is used to modify the existing record(s) in a table.

Syntax –
UPDATE tbl_name
SET column1 = value1, ...
[WHERE condition(s)];

Example –
SQL> SELECT * FROM student;

RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 SAMEER
4 BALAJI AUSA

The following SQL statement will update the CITY to ‘NANDED’ for all records whose RN is 3.
SQL> UPDATE student SET CITY = ‘NANDED’ WHERE RN = 3;
1 row updated.
SQL> SELECT * FROM student;

RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 SAMEER NANDED
4 BALAJI AUSA

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B SC CS SY Unit II – SQL statements & Working with Tables

3) DELETE –
The DELETE statement is a used to delete a one or more records from a table.

Syntax –
DELETE FROM tbl_name [WHERE condition(s)];

Example –
SQL> SELECT * FROM student;

RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 SAMEER NANDED
4 BALAJI AUSA
The following SQL statement deletes the student ‘BALAJI’ from the STUDENT table.

SQL> DELETE FROM student WHERE NAME = ‘BALAJI’;


1 row deleted.

SQL> SELECT * FROM student;

RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 SAMEER NANDED

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B SC CS SY Unit II – SQL statements & Working with Tables
4) SELECT
The SELECT statement is used to retrieve records from table.

Syntax –
SELECT column1, column2, ...
FROM tbl_name
[WHERE condition(s)];

a) All Fields and All Rows –


Syntax –
SELECT * FROM tbl_name;
Example –
SQL> SELECT * FROM student;

RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 SAMEER NANDED

In this example, we've used * to signify that we wish to select all fields from the STUDENT
table. Conditions are not provided, so it displayed all records from STUDENT table.
b) Selected Fields and All Rows –
Syntax –
SELECT column1, column2, …. FROM tbl_name;

Example –
SQL> SELECT RN, NAME FROM student;

RN NAME
1 AMOL
2 ATUL
3 SAMEER

This example would return only the RN and NAME fields from the STUDENT table. Conditions
are not provided, so it displayed all values for RN and NAME fields from STUDENT table.

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B SC CS SY Unit II – SQL statements & Working with Tables

c) All Fields and Selected Rows –


Syntax –
SELECT * FROM tbl_name WHERE condition(s);

Example –
SQL> SELECT * FROM student WHERE CITY = ‘LATUR’;

RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR

In this example, we've used * to signify that we wish to select all fields from the STUDENT table
where the CITY is equal to LATUR.

d) Selected Fields and Selected Rows –


Syntax –
SELECT column1, column2, …. FROM tbl_name WHERE condition(s);

Example –
SQL> SELECT RN, CITY FROM student WHERE CITY = ‘NANDED’;

RN CITY
3 NANDED

This example would return only the RN and NAME fields from the STUDENT table where the
CITY is equal to NANDED.

* WHERE Clause –
The WHERE clause is used to filter records from a SELECT, UPDATE or DELETE statement. The
WHERE clause is used to extract only those records that fulfill a specified condition(s).

Syntax –
WHERE condition(s);

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B SC CS SY Unit II – SQL statements & Working with Tables
Example –
SQL> SELECT * FROM student;

RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 SAMEER NANDED

SQL> SELECT * FROM student WHERE RN = 2;

RN NAME CITY
2 ATUL LATUR

In this example, we've used the WHERE clause to filter our results from the STUDENT table. The
SELECT statement above would return all rows from the STUDENT table where the RN is 2.
Because the * is used in the SELECT, all fields from the STUDENT table would appear in the
result set.

The following operators can be used in the WHERE clause:

* DISTINCT Clause
Inside a table, a column often contains many duplicate values; and sometimes you only want to
list the different (distinct) values. The DISTINCT clause is used to return only distinct (different) values.

The DISTINCT clause can only be used with SELECT statements.


Syntax –
SELECT DISTINCT column1, column2, . . FROM tbl_name
[WHERE condition(s)];
Example –
SELECT * FROM student;

RN NAME CITY
1 AMOL LATUR
2 ATUL LATUR
3 SAMEER NANDED

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B SC CS SY Unit II – SQL statements & Working with Tables
SQL> SELECT DISTINCT CITY FROM student;

CITY
LATUR
NANDED

This above example would return all unique CITY values from the STUDENT table.

SQL> SELECT DISTINCT RN, CITY FROM student;

RN CITY
1 LATUR
2 LATUR
3 NANDED

* Using Column ALIASES –


Oracle ALIASE can be used to create a temporary name for columns. Aliases are often used to
make column names more readable. An alias only exists for the duration of the query.

Syntax –
column_name AS alias_name

Example –
SQL> SELECT RN AS ROLLNO FROM student;

ROLLNO
1
2
3

SQL> SELECT RN AS ROLL_NO FROM student;

ROLL_NO
1
2
3

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B SC CS SY Unit II – SQL statements & Working with Tables
SQL> SELECT RN AS “ROLL NO” FROM student;

ROLL NO
1
2
3

Data Constraints: -
Integrity Constraints are used to apply business rules for the database tables.
The constraints available in SQL are Foreign Key, Not Null, Unique, Check.

1) SQL Primary key: -


This constraint defines a column or combination of columns which uniquely identifies each row in the
table.
Syntax to define a Primary key at column level:
column name datatype [CONSTRAINT constraint_name] PRIMARY KEY

For Example: To create an employee table with Primary Key constraint, the query would be like.

SQL> Crate Table Employee


(id number (5) PRIMARY KEY,
name char (20),
dept char (10),
age number (2),
salary number (10));

2) SQL Foreign key or Referential Integrity: -


This constraint identifies any column referencing the PRIMARY KEY in another table.
Syntax to define a Foreign key at column level:
[CONSTRAINT constraint_name] REFERENCES Referenced_Table_name(column_name)
SQL> Create Table Student
(Roll_No number (5) PRIMARY KEY,
Name char (20),
City char (20),
);

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B SC CS SY Unit II – SQL statements & Working with Tables

SQL> Create table Employee


(Emp_No number (12) references Student (Roll_No),
Emp_Name char (10),Emp_City char(11));

3) SQL Not Null Constraint: -


This constraint ensures all rows in the table contain a definite value for the column which is specified
as not null. Which means a null value is not allowed.
Syntax to define a Not Null constraint:
[CONSTRAINT constraint name] NOT NULL
For Example: To create an employee table with Null value, the query would be like
SQL> Create Table Employee
(id number (5),
name char (20) NOT NULL,
dept char (10),
age number (2),
salary number (10));

4) SQL Unique Key: -


This constraint ensures that a column or a group of columns in each row have a distinct value. A
column(s) can have a null value but the values cannot be duplicated.
Syntax to define a Unique key at column level:
[CONSTRAINT constraint_name] UNIQUE
Syntax to define a Unique key at table level:
[CONSTRAINT constraint_name] UNIQUE(column_name)
For Example: To create an employee table with Unique key, the query would be like,

SQL> Create Table Employee


(id number (5) PRIMARY KEY,
name char (20),
dept char (10),
age number (2),
salary number (10),
location char (10) UNIQUE
);

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B SC CS SY Unit II – SQL statements & Working with Tables

5) SQL Check Constraint: -


This constraint defines a business rule on a column. All the rows must satisfy this rule. The constraint
can be applied for a single column or a group of columns.
Syntax to define a Check constraint:

[CONSTRAINT constraint_name] CHECK (condition)


For Example: In the employee table to select the gender of a person, the query would be like
Check Constraint at column level:
SQL> Create Table Employee
(id number (5) PRIMARY KEY,
name char (20),
salary number (10),
age number (2), Check (Age>20));

Prepared by, Mr. V. D. Patil


COCSIT, Latur
B SC CS SY Unit II – SQL statements & Working with Tables

Prepared by, Mr. V. D. Patil


COCSIT, Latur

You might also like