0% found this document useful (0 votes)
23 views

LAB Experiment 1

The document provides an introduction to Structured Query Language (SQL), detailing its purpose in managing relational databases and explaining key concepts such as schemas, data types, and the differences between tables and databases. It outlines various SQL commands including Data Definition Language (DDL) for creating and modifying tables, as well as Data Manipulation Language (DML) for retrieving, inserting, updating, and deleting data. Additionally, it includes examples and exercises for practical application of SQL commands.

Uploaded by

brandyadav696969
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

LAB Experiment 1

The document provides an introduction to Structured Query Language (SQL), detailing its purpose in managing relational databases and explaining key concepts such as schemas, data types, and the differences between tables and databases. It outlines various SQL commands including Data Definition Language (DDL) for creating and modifying tables, as well as Data Manipulation Language (DML) for retrieving, inserting, updating, and deleting data. Additionally, it includes examples and exercises for practical application of SQL commands.

Uploaded by

brandyadav696969
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Experiment-1

INTRODUCTION TO SQL
What is Structured Query Language (SQL)

Structured Query Language (SQL) is a standardized programming language that is used


to manage relational databases and perform various operations on the data in them.

A Relational Database is a type of database that stores and organizes data in a


structured format using tables. Each table (also called a relation) consists of rows
(records) and columns (fields), making the data easy to access, manage, and update.

All the RDBMS systems like MySQL, MS Access, Oracle and SQL Server use SQL as their
standard database language.
Schemas And Instances

Schemas and instances determine the structure and content of a database in DBMS.
An instance is a particular set of data that aligns with the schema, whereas a schema is the logical
structure of a Database.
DATA TYPES

1. CHAR (Size): This data type is used to store character strings values of fixed length. The size in brackets determines
the number of characters the cell can hold. The maximum number of character is 255 characters.

2. VARCHAR (Size) / VARCHAR2 (Size): This data type is used to store variable length alphanumeric data. The
maximum character can hold is 2000 character.

3. NUMBER (P, S): The NUMBER data type is used to store number (fixed or floating point). Number of virtually any
magnitude may be stored up to 38 digits of precision. Number as large as 9.99 * 10 124. The precision (p) determines
the number of places to the right of the decimal. If scale is omitted then the default is zero. If precision is omitted, values
are stored with their original precision up to the maximum of 38 digits.

4. DATE: This data type is used to represent date and time. The standard format is DD- MM-YY as in 17-SEP-2009. To
enter dates other than the standard format, use the appropriate functions. Date time stores date in the 24-Hours format.
By default the time in a date field is 12:00:00 am, if no time portion is specified. The default date for a date field is the
first day the current month.

5. LONG: This data type is used to store variable length character strings containing up to 2GB. Long data can be used
to store arrays of binary data in ASCII format. LONG values cannot be indexed, and the normal character functions such
as SUBSTR cannot be applied.
Difference between Table & Database

While both databases and tables are used to store data, they have key difference.

A database is a collection of organized and structured data that can be accessed by


computers. A table is a structure within a database that stores data. A database can contain
multiple tables, while tables can only be contained within a database.

However, the important takeout is that tables are objects inside the database, and databases
can not exist without tables.

Example: A University may have a student table that stores information about their
students, such as name, address, and phone number.

They may also have a database that stores all of the student's data in one place. This
database could then contain multiple tables, such as an CSE students table and EC students
table .
Structured Query Language(SQL) is a standard language used to create, access, and manipulate
databases, being an integral part of Database Management Systems(DBMS). SQL uses various
commands to manipulate the database and these commands are divided into 5 parts as follows :
Basic Data Types used in SQL

1. CHAR [(length)] or CHARACTER [(length)]


Description: A fixed-length character string. If the data is shorter than the specified length, it is padded with spaces.

2. VARCHAR (length)
•Description: A variable-length character string. Only the characters entered are stored, without padding.
•Example:
3. BOOLEAN
•Description: A data type that stores TRUE, FALSE, or NULL values. Supported in PL/SQL but not directly in standard
SQL tables in Oracle.
•Example CREATE TABLE students ( student_id INT PRIMARY KEY,
student_name VARCHAR(50), course_completed BOOLEAN );

4. SMALLINT
•Description: Stores small integer values, usually ranging from -32,768 to 32,767.
•Example:

5. INTEGER or INT
•Description: Stores integer values without decimals.
•Example:
6. DECIMAL [(p[,s])] or DEC [(p[,s])]
•Description: Stores fixed-point numbers with precision (p) and scale (s).
•p: Total number of digits.
•s: Number of digits to the right of the decimal point.
•Example:

7. NUMERIC [(p[,s])]
•Description: Similar to DECIMAL, stores fixed-point numbers.
•Example:
8. REAL
•Description: Stores single-precision floating-point numbers.
•Example:

9. FLOAT(p)
•Description: Stores floating-point numbers with precision p. Higher p values mean more accuracy.
•Example:
8

10. DOUBLE PRECISION


•Description: Stores double-precision floating-point numbers, offering higher precision than REAL or FLOAT.
•Example:
11. DATE
•Description: Stores date values (YYYY-MM-DD format).
•Example:

12. TIME
•Description: Stores time values (HH:MM:SS format). Oracle uses TIMESTAMP to handle both date and time.
•Example:

13. TIMESTAMP
•Description: Stores both date and time values (YYYY-MM-DD HH:MM:SS).
•Example:
14. CLOB [(length)] or CHARACTER LARGE OBJECT [(length)]
•Description: Stores large character data (e.g., text documents).
•Example:

15. BLOB [(length)] or BINARY LARGE OBJECT [(length)]


•Description: Stores large binary data (e.g., images, videos).
•Example:
16. NUMBER
.Description: The NUMBER data type is used for storing numeric data, including integers, decimals, and floating-
point numbers.

NUMBER(p, s)
•p (Precision): Total number of digits (1 to 38).
•s (Scale): Number of digits to the right of the decimal point (can be negative).

Example:
1. Storing Integers

2. Specifying Precision and Scale

•6 is the precision (total digits: 1234.56 has 6 digits).


•2 is the scale (2 digits to the right of the decimal point).
3. Storing Floating-Point Numbers

4. Negative Scale (Rounding)


Data Type Purpose Range / Size Example

CHARACTER [(length)] / CHAR [(length)] Fixed-length character data 1 to 2000 characters (varies by database) CHAR(5) stores "ABC "

Up to 2000-4000 characters (varies by


VARCHAR (length) Variable-length character data VARCHAR(50) for strings ≤ 50 chars
database)
BOOLEAN Logical data TRUE, FALSE, or NULL BOOLEAN stores TRUE or FALSE
SMALLINT Small integers -32,768 to 32,767 (2 bytes) SMALLINT stores 12345

INTEGER / INT Whole numbers -2,147,483,648 to 2,147,483,647 (4 bytes) INT stores 100000

p ≤ 38 digits; s for decimal places (e.g.,


DECIMAL [(p[,s])] / DEC [(p[,s])] Fixed-point numbers 12345.67 (precision 10, scale 2)
DECIMAL(10, 2))
NUMERIC [(p[,s])] Fixed-point numbers Same as DECIMAL NUMERIC(8, 2) stores 1234.56
Approx. range: -3.4E+38 to +3.4E+38; ~7
REAL Single-precision floating-point numbers REAL stores 1.234567
decimal places
p = precision (1-53 bits); FLOAT(24) for
FLOAT(p) Floating-point numbers with precision single (~7 digits), FLOAT(53) for double (~15 FLOAT(24) stores 1.234567
digits)
Approx. range: -1.7E+308 to +1.7E+308; ~15
DOUBLE PRECISION Double-precision floating-point numbers DOUBLE stores 123.456789012345
decimal places
Typically 01-01-4712 BC to 31-12-9999 AD
DATE Calendar date DATE stores 2024-01-08
(varies by database)
TIME Time of the day 24-hour format HH:MI:SS TIME stores 12:30:45
Includes DATE and TIME with fractional
TIMESTAMP Date and time 2024-01-08 12:30:45.123
seconds precision
CLOB [(length)] / CHARACTER LARGE Up to 4 GB or more (depending on the
Large text data Stores large documents or text
OBJECT [(length)] database)
BLOB [(length)] / BINARY LARGE OBJECT Up to 4 GB or more (depending on the
Binary data Stores images, videos, or files
[(length)] database)
DDL COMMANDS
A) CREATION OF TABLE:
A DDL (Data Definition Language) command like CREATE TABLE is used in SQL to define the structure of a
table in a database. It creates a new table by specifying its name, column definitions, and constraints.

SYNTAX: create table<tablename>(column_name1 datatype(<size>), column_name2 datatype(<size>) ...);


•table_name: The name of the table.
•column1, column2, ...: The names of the columns in the table.
•datatype: The type of data the column will store (e.g., VARCHAR, NUMBER, DATE).

create table STUDENT22 (


reg_no number (5),
stu_name varchar(20),
stu_age number(5),
stu_dob date,
subject1_marks number (4,2),
subject2_marks number(4,2),
subject3_marks number(4,1));

insert into STUDENT22 values (10, 'AAA', 16, DATE '1988-03-07',80,90,98);


select * from STUDENT22
B). MODIFYING THE STRUCTURE OF TABLE
The ALTER TABLE command is used to modify the structure of an existing table. It allows to:

a) Add new columns


Syntax:
Alter table <tablename>add (<new col><datatype (size),<newcol>datatype(size));
Ex: Add a new column ‘Gender’ to student table.
alter table student22 add(Gender char (5));

b) Dropping a column from a table


Syntax: Alter table <tablename> drop column <col>;
Ex: To drop a column ‘Gender’ from student table.
Alter table student22 drop column Gender;
create table STUDENT00 (
c) Modifying existing columns: The MODIFY command is used reg_no number (5),
to change the column definition of the table. stu_name varchar(20),
stu_age number(5),
stu_dob date,
Syntax: Alter table <tablename> modify
subject1_marks number (4,2),
(<col><newdatatype>(<newsize>));
subject2_marks number(4,2),
Ex: To modify the datatype of stu_age subject3_marks number(4,1));
desc STUDENT00;

create table STUDENT00 (


reg_no number (5),
stu_name varchar(20),
stu_age number(5),
stu_dob date,
subject1_marks number (4,2),
subject2_marks number(4,2),
subject3_marks number(4,1));
Alter table student00 modify (stu_age varchar(3));
desc STUDENT00;
d) Rename a Column
Syntax: ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;
ALTER TABLE STUDENT00 RENAME COLUMN STU_AGE TO STU_ADDR;

e) Rename the Table


Syntax: ALTER TABLE old_table_name RENAME TO new_table_name;
C. TRUNCATE THE TABLE
The TRUNCATE command is used to delete all rows from a table.
Syntax: Trunc table <tablename>;
Ex: Trunc table students;
Key Features of TRUNCATE
1.Removes All Rows: Deletes all data in the table.
2.Retains Table Structure: The table structure remains for future data insertion.
3.Faster than DELETE: TRUNCATE is faster because it doesn't log individual row deletions.
4.Auto-Increment Reset: Resets any auto-increment counter to its initial value.
5.Cannot Be Rolled Back: TRUNCATE cannot be undone as it does not generate rollback data.
6.No WHERE Clause: Unlike DELETE, it does not support filtering.

create table STUDENT000 (


reg_no number (5),
stu_name varchar(20),
stu_age number(5),
stu_dob date,
subject1_marks number (4,2),
subject2_marks number(4,2),
subject3_marks number(4,1));
insert into STUDENT000 values (10, 'AAA', 16, DATE '1988-03-07',80,90,98);
select * from STUDENT000;
TRUNCATE TABLE STUDENT000;
D) Delete the table structure
The DROP command is used to permanently remove an entire database object such as a table. When
executed, it completely deletes the object along with its data, structure, and dependencies. Removes the
table and all its data permanently
Syntax: Drop table <tablename>;
Ex: drop table student000;

Key Differences Between DROP and TRUNCATE

Feature DROP TRUNCATE


Removes Table Yes (deletes table structure) No (keeps table structure)
Deletes Data Yes Yes
Rollbacks No (permanent action) No (permanent action)
Performance Faster (removes everything) Fast (removes rows only)
DML commands

DML Commands (Data Manipulation Language) in SQL are used to retrieve, insert, update, and
delete data in a database table. These commands directly affect the data stored in the database.

1. Selecting the information from table(s)


Syntax: Select col1,col2,col3,……, coln from <table_name> where < condition >
Ex:
create table STUDENT22 (
reg_no number (5),
stu_name varchar(20),
stu_age number(5),
stu_dob date,
subject1_marks number (4,2),
subject2_marks number(4,2),
subject3_marks number(4,1));

insert into STUDENT22 values (10, 'AAA', 16, DATE '1988-03-07',80,90,98);


select * from STUDENT22
2. Inserting Data into Tables:

create table STUDENT22 (


reg_no number (5),
stu_name varchar(20),
stu_age number(5),
stu_dob date,
subject1_marks number (4,2),
subject2_marks number(4,2),
subject3_marks number(4,1));

insert into STUDENT22 values (10, 'AAA', 16, DATE '1988-03-07',80,90,98);


select * from STUDENT22
create table STUDENT000 (
reg_no number (5),
stu_name varchar(20),
stu_age number(5),
stu_dob date,
subject1_marks number (4,2),
subject2_marks number(4,2),
subject3_marks number(4,1));
insert into STUDENT000 values (10, 'AAA', 16, DATE '1988-03-
07',80,90,98);
select * from STUDENT000;

3. Delete operations
a) Removal of specified row/s
Syntax: Delete from <tablename> where <condition>;
Ex: Delete from STUDENT000 where stu_age=16;

b) Remove all rows


Syntax: Delete from <tablename>;
Ex: Delete from STUDENT;
4. Updating the contents of a table
a) Updating all rows
Syntax: Update <tablename> set <col>=<exp>, <col>=<exp>;
Ex: Update STUDENT000 set stu_name='MANAV';

b) Updating selected records


Syntax: Update <tablename> set <col>=<exp>,<col>=<exp>where <condition>;
Ex: Update STUDENT000 set stu_name='YADAV' where stu_age=16;
LAB EXERCISES:

1. Create a table employee with ( emp_no, emp_name, emp_address)


2. Insert five employees information.
3. Display names of all employees.
4. Display all the employees from ‘MANIPAL’.
5. Add a column named salary to employee table.
6. Assign the salary for all employees.
7. View the structure of the table employee using describe.
8. Delete all the employees from ‘MANGALORE’
9. Rename employee as employee1.
10. Drop the table employee1.

You might also like