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

SQL(Lab Work)

The document provides an overview of SQL, a standard language for managing and manipulating databases, including its various functionalities such as executing queries, creating tables, and managing data types. It details SQL statements categorized into DDL, DML, DCL, and TCL, along with examples of creating, altering, and deleting tables and records. Additionally, it explains the structure of tables, fields, records, and the rules for naming database objects.

Uploaded by

kundanr702
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 views49 pages

SQL(Lab Work)

The document provides an overview of SQL, a standard language for managing and manipulating databases, including its various functionalities such as executing queries, creating tables, and managing data types. It details SQL statements categorized into DDL, DML, DCL, and TCL, along with examples of creating, altering, and deleting tables and records. Additionally, it explains the structure of tables, fields, records, and the rules for naming database objects.

Uploaded by

kundanr702
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/ 49

SQL

"SQL, PL/SQL the Programming


Language of Oracle" book by Ivan
Bayross
What is SQL?
• SQL stands for Structured Query Language
• SQL is a standard language for storing,
manipulating and retrieving data in databases.
• SQL lets you access and manipulate databases
• SQL is an ANSI (American National Standards
Institute) standard
SQL
• SQL is used to make a request to retrieve data
from a Database.
• The DBMS processes the SQL request,
retrieves the requested data from the
Database, and returns it.
• This process of requesting data from a
Database and receiving back the results is
called a Database Query and hence the name
Structured Query Language.
• SQL is a language that all commercial RDBMS
implementations understand.
• SQL is a non-procedural language
• We would be discussing SQL with respect to
oracle syntax
How SQL Works ?
What Can SQL do?
• SQL can execute queries against a database
• SQL can retrieve data from a database
• SQL can insert records in a database
• SQL can update records in a database
• SQL can delete records from a database
• SQL can create new databases
• SQL can create new tables in a database
• SQL can create stored procedures in a database
• SQL can create views in a database
• SQL can set permissions on tables, procedures, and
views
SQL Statements
• DDL (Data Definition Language)
• Create - Creates a new table, a view of a
table, or other object in the database.
• Alter - Modifies an existing database
object, such as a table.
• Drop - Deletes an entire table, a view of
a table or other objects in the database.
• Truncate - Is used to delete complete
data from an existing table.
SQL Statements
• DML (Data Manipulation Language)
• Insert - Creates a record.
• Update - Modifies records.
• Delete - Deletes records.
• Select - Retrieves certain records from
one or more tables.
SQL Statements

• DCL (Data Control Language)


• Grant - Gives a privilege to user.
• Revoke - Takes back privileges granted
from user.
SQL Statements

• TCL (Transaction Control Language)


• Commit - To save the changes.
• Rollback - To roll back the changes.
Data types
Data Type Explanation
Char (size) Where size is the number of characters to store. Fixed
length strings. Space padded. The maximum number of
characters this data type can hold is 255 characters.

Varchar(size)/ Where size is the number of characters to store. variable


varchar2(size) length strings.

Date Standard format is DD – MON – YY

Number(p , s) Where p is the precision ands is the scale. For ex.


Number(7,2) is a number that has 5 digits before the
decimal and 2 digits after the decimal.
DDL(Data Definition Language)
Creating and Managing Tables
Objectives
After completing this lesson, you should be
able to do the following
• Describe the main database objects
• Create tables
• Describe the data types that can be used
when specifying column definition
• Alter table definitions
• Drop, rename, and truncate tables
Database Objects
What is a table?
• The data in an RDBMS is stored in database
objects which are called as tables. This table is
basically a collection of related data entries
and it consists of numerous columns and
rows.
• Remember, a table is the most common and
simplest form of data storage in a relational
database. The following program is an
example of a CUSTOMERS table −
Table Example
ID NAME AGE ADDRESS SALARY
1 Ramesh 23 Ghaziabad 22,000
2 Deeptih 21 Kanpur 20.000
3 Komal 35 Meerut 35,000
4 Sekhar 25 Delhi 21,000
5 Saurabh 28 Calcutta 32,000
6 Mufti 37 Agra 35,000
7 Ashutosh 32 Mumbai 22,000
What is a field?
• Every table is broken up into smaller entities
called fields. The fields in the CUSTOMERS
table consist of ID, NAME, AGE, ADDRESS and
SALARY.
• A field is a column in a table that is designed
to maintain specific information about every
record in the table.
What is a Record or a Row?
• A record is also called as a row of data is each
individual entry that exists in a table. For
example, there are 7 records in the above
CUSTOMERS table.

1 Ramesh 23 Ghaziabad 22,000


What is a column?
• A column is a vertical entity in a table that contains all
information associated with a specific field in a table.
• For example, a column in the CUSTOMERS table is
ADDRESS, which represents location description and
would be as shown below.
NAME
Ramesh
Deeptih
Komal
Sekhar
Saurabh
Mufti
Ashutosh
Naming Rules
Table names and column names:
• Must begin with a letter
• Must be 1–30 characters long
• Must contain only A–Z, a–z, 0–9, _, $, and #
• Must not duplicate the name of another
object owned by the same user
• Must not be an Oracle server reserved word
The CREATE TABLE Command

Syntax – CREATE TABLE Tablename


(
ColumnName1 Datatype(Size),
ColumnName2 Datatype(Size),
…………………………………………..
…………………………………….
);
Example create table
Ex. – CREATE TABLE Student
( Roll number(5),
Name varchar2(30)
);
Example create table
Ex. – CREATE TABLE Employee
( ID number(5),
FatherName varchar2(30),
Age number(3),
Address varchar2(30),
Salary number(7)
);
To see the structure of the Table
DESCRIBE Tablename;
DESCRIBE student;
The ALTER TABLE Statement
Use the ALTER TABLE statement to:
• Add a new column
• Modify an existing column
• Define a default value for the new column
• Drop a column
The ALTER TABLE Statement
Adding New Columns –
Syntax – ALTER TABLE Tablename
ADD NewColumnName Datatype(size),
NewColumnName Datatype(size);
Ex- Add a field named branch to the newly created table
student.
ALTER TABLE student
ADD branch varchar2 (10);
The ALTER TABLE Statement
Droping a Column from a table –
Syntax – ALTER TABLE Tablename
DROP COLUMN columnname;
Ex- Drop branch field from student table
ALTER TABLE student
DROP COLUMN branch;
The ALTER TABLE Statement
Modifying Existing Columns
Syntax – ALTER TABLE Tablename
MODIFY ( ColumnName NewDatatype(Newsize),
Ex- Modify name field of student table to varchar2(35).
ALTER TABLE student
modify name varchar2(35);
Restrictions – we cannot do the following with the ALTER
TABLE COMMAND.
Decrease the size of a column if table data exists.
Renaming Tables

RENAME Command

Syntax – ALTER TABLE Tablename


RENAME TO NewTableName

Ex- ALTER TABLE student


RENAME TO student1;
Renaming Columns
RENAME COLUMN statement

Syntax-
ALTER TABLE Tablename
RENAME COLUMN oldColumnName TO
NewColumnName

Ex - ALTER TABLE Employee


RENAME COLUMN MANAGER TO SUPERVISOR
Dropping a Table
• All data and structure in the table is deleted.
• Any pending transactions are committed.
• All indexes are dropped.
• You cannot rollback the DROP TABLE
statement.
Syntax – DROP TABLE TableName;
Ex – DROP TABLE student;
Truncating a Table
The TRUNCATE TABLE statement:
• Removes all rows from a table
• Releases the storage space used by that table
• You cannot roll back row removal when using
TRUNCATE.
Syntax - TRUNCATE TABLE TableName;

Ex - TRUNCATE TABLE student;


Summary
• In this lesson, you should have learned how to
use DDL statements to create, alter, drop, and
rename tables.
Manipulating Data
DML Statement
Objectives
After completing this lesson, you should be
able to do the following:
• Describe each DML statement
• Insert rows into a table
• Update rows in a table
• Delete rows from a table
• Merge rows in a table
A DML statement is executed when you:
• Add new rows to a table
• Modify existing rows in a table
• Remove existing rows from a table
• Retrieve records from a table
Adding a New Row to a Table
New
row
DEPARTMENTS
insert a new row
into the
DEPARMENTS
table…
Inserting Data Into Tables

Syntax – INSERT INTO tablename


(columnname1, columnname2,……)
values (data1,data2,……);

Ex – INSERT INTO student


(roll,name) values(1,’yatin’) ;
Inserting Data Into Tables
Points to remember:

1. Table columns & values have a one to one relationship.


2. If there are exactly the same number of values as there
are columns and the values are sequenced in exactly in
accordance with the data type of the table columns ,
there is no need to indicate the column names.
3. If there are less values being described than there are
columns in the table then it is mandatory to indicate both
the table column name and its corresponding value.
4. Only one row is inserted at a time with this syntax.
5. Enclose character and date values within single quotation
marks.
Inserting Multiple Rows Into Tables
Syntax – INSERT ALL
INTO tablename
(columnname1, columnname2,……) values
(data1,data2,……)
(columnname1, columnname2,……) values
(data1,data2,……)
SELECT * FROM dual;

Ex – INSERT ALL
INTO customer (first_name, last_name)
VALUES ('Kristen', 'Rowley')
INTO customer (first_name, last_name)
VALUES ('Jed', 'Tomlinson')
INTO customer (first_name, last_name)
VALUES ('Margie', 'Escobar')
SELECT * FROM dual;
Concept of NULL
NULL
• Missing / unknown / inapplicable data
represented as a null value
• NULL is not a data value. It is just an indicator
that the value is unknown
Inserting Rows with Null Values
• Implicit method: Omit the column from the column
list.

INSERT INTO departments


(department_id, department_name)
VALUES (30, 'Purchasing');

• Explicit method: Specify the NULL keyword in the


VALUES clause.

INSERT INTO departments


VALUES (100, 'Finance', NULL, NULL);
Changing Data in a Table
EMPLOYEES

Update rows in the EMPLOYEES table.


The UPDATE Statement Syntax
Syntax –
UPDATE Tablename
SET columnName1 = value ,
columnName2 = value, ...
WHERE condition;
Note -
• Without where condition all the rows get
updated.
• Depending upon where condition selected rows
get updated.
Example of UPDATE Statement

UPDATE student
set college_name = ‘XYZ’;

UPDATE student
set college_name = ‘ABC’
Where roll = 10;
Removing a Row from a Table
DEPARTMENTS

Delete a row from the DEPARTMENTS table.


The DELETE Statement
Removal of all rows
Syntax –
DELETE FROM Tablename

Ex – DELETE FROM student;


The DELETE Statement
Removal of specific row(s)

Syntax –
DELETE FROM Tablename
Where Condition

Ex – DELETE FROM student


where branch =‘MCA’;

You might also like