Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views25 pages

Unit 3dbms

Uploaded by

Sudhan Khanal
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)
8 views25 pages

Unit 3dbms

Uploaded by

Sudhan Khanal
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/ 25

Unit 3

SQL
Define SQL
• Structure Query Language is a database query language used for
storing and managing data in relational DBMS. It is a database
language designed for the retrieval and management of data stored in
relational database management system, database schema creation
and modification and database object access control management.
Objectives of SQL
• Create the database and relation structures.
• Perform basic data management tasks,such as the
insertion,modification and deletion of the data from the relations.
• Perform both simple and complex queries.
Advantages of SQL
➢Standard Independent language:SQL is an open language implying it
is not owned or controlled by any single company.
➢Speed:It has fast speed and lower costs per transaction.
➢Cross-Platform Abilities:It has been used in different hardware
platforms for years.
➢Easy to learn and Use:It used simple English sentences and making
SQL easy to learn and understand.
➢Less Programming:It need less programming as compared to
traditional methods.
Data Definition:
• It is a standard for commands that define the different structures in
database.DDL statements create,modify and remove database objects
such as tables,indexes, and users.DDL statements are:
• Create
• Alter
• Drop
• Truncate
• Rename
• Comment
Data types:The data type defines the allowed values that each
column or argument can store.
Data Type Properties
These are used to store numeric values. Examples
Numeric data types include INT,
BIGINT, DECIMAL, and FLOAT.
These are used to store character strings. Examples
Character data types
include CHAR, VARCHAR, and TEXT.
These are used to store date and time values.
Examples include
Date and time data types
DATE, TIME, and TIMESTAMP
.
These are used to store binary data, such as images
Binary data types
or audio files. Examples include BLOB and BYTEA.
This data type is used to store logical values. The
Boolean data type
only possible values are TRUE and FALSE.
Binary Datatypes
There are four subtypes of this datatype which are given
below:
Data Type Description

The maximum length of 8000 bytes(Fixed-Length


Binary
binary data)

The maximum length of 8000 bytes(Variable


varbinary
Length binary data)

The maximum length of 231 bytes(SQL Server


varbinary(max)
2005 only).(Variable Length binary data)

Maximum Length of 2,147,483,647


image
bytes(Variable Length binary data)
Exact Numeric Data type
There are nine subtypes which are given below in the table. The table contains
the range of data in a particular type.
Data Type From To
-263 (- 263 -1
BigInt
9,223,372,036,854,775,808) (9,223,372,036,854,775,807)
Int -231 (-2,147,483,648) 231-1 (2,147,483,647)
smallint -215 (-32,768) 215-1 (32,767)
tinyint 0 28-1 (255)
bit 0 1
decimal -1038+1 1038-1
numeric -1038+1 1038-1
Approximate Numeric Datatype
The subtypes of this datatype are given in the table with the range.

Data Type From To

Float -1.79E+308 1.79E+308

Real -3.40E+38 3.40E+38


Character String Datatype
The subtypes are given in below table –
Data Type Description

The maximum length of 8000 characters.(Fixed-Length


char
non-Unicode Characters)

The maximum length of 8000 characters.(Variable-


varchar
Length non-Unicode Characters)

The maximum length of 231 characters(SQL Server


varchar(max)
2005 only).(Variable Length non-Unicode data)

The maximum length of 2,127,483,647


text
characters(Variable Length non-Unicode data)
Date and Time Datatype
The details are given in the below table.
Data Type Description

A data type is used to store the data of date in a


DATE
record

A data type is used to store the data of time in a


TIME
record

A data type is used to store both the data,date, and


DATETIME
time in the record.
Specifying Constrains : Constraints are the rules that we can apply on the
type of data in a table. That is, we can specify the limit on the type of data
that can be stored in a particular column in a table using constraints.
• The available constraints in SQL are:

• NOT NULL: This constraint tells that we cannot store a null value in a column. That is, if a
column is specified as NOT NULL then we will not be able to store null in this particular
column any more.
• UNIQUE: This constraint when specified with a column, tells that all the values in the
column must be unique. That is, the values in any row of a column must not be repeated.
• PRIMARY KEY: A primary key is a field which can uniquely identify each row in a table.
And this constraint is used to specify a field in a table as primary key.
• FOREIGN KEY: A Foreign key is a field which can uniquely identify each row in a another
table. And this constraint is used to specify a field as Foreign key.
• CHECK: This constraint helps to validate the values of a column to meet a particular
condition. That is, it helps to ensure that the value stored in a column meets a specific
condition.
• DEFAULT: This constraint specifies a default value for the column when no value is
specified by the user.
How to specify constraints?

• We can specify constraints at the time of creating the table using


CREATE TABLE statement. We can also specify the constraints after
creating a table using ALTER TABLE statement.
Basic Retrieval Queries:
• A simple retrieval query in SQL can consist of up to four clauses, but
only the first two—SELECT and FROM—are mandatory. The clauses
are specified in the follow-ing order, with the clauses between square
brackets [ ... ] being optional:
SELECT <attribute list> FROM <table list>
[ WHERE <condition> ]
[ ORDER BY <attribute list> ];

• The SELECT clause lists the attributes to be retrieved, and


the FROM clause specifies all relations (tables) needed in the simple
query. The WHERE clause identifies the conditions for selecting the
tuples from these relations, including join conditions if needed.
Selecting All Columns from a Table: To select all columns
from a table, you can use *. This returns all the data in
the table.
• SELECT * FROM Employees;
2)Filtering Rows with WHERE: The WHERE clause
is used to filter records.
• SELECT * FROM Employees WHERE Salary > 50000;
3) Selecting Specific Columns: You can specify the columns you want to
select.
• SELECT FirstName, LastName FROM Employees;
Complex Retrieval Queries:
• Complex SQL queries go beyond the standard requests by retrieving
data from several tables and by limiting the result set with multiple
conditions. Complex retrieval queries often involve the use of
multiple SQL commands or operators in conjunction with each other
to retrieve data from a database. These can include commands like
SELECT, WHERE, JOIN, GROUP BY, ORDER BY, HAVING, UNION, and
others.
Joins: Joins allow you to combine data from two or more tables based
on a related column.

• SELECT Orders.OrderID, Customers.CustomerName


• FROM Orders
• INNER JOIN Customers ON Orders.CustomerID =
Customers.CustomerID;
Subqueries: Subqueries, or nested queries, are queries that are
embedded within other queries. They can be used in various parts of a
query, such as the SELECT, FROM, WHERE, or HAVING clauses.
• SELECT CustomerName
• FROM Customers
• WHERE CustomerID IN (SELECT CustomerID FROM Orders WHERE
Quantity > 5);
Create statement
• The create table statement is used to create a table in sql.We know
that a table comprises of rows and column.so while creating tables
we have to provide all the information to SQL about the names of the
columns,type of data to be stored in columns,size of the data.
• Syntax:
CREATE TABLE table_name
(column 1 data-type (size), column 2 data-type (size), column 3 data-
type (size), column n data-type (size));
Insert,Delete,Update statement:
•Insert:It is a statement is used to add one or more tuples into
database relations.It has two formats:
• INSERT INTO table 1[column-list]VALUES(value-list)
• And
• INSERT INTO table-1[column-list](query-specification)
The first form inserts a single row into table-1 and Explicitly Specifies
the column values for the row.
The Second form uses the result of query-specification to insert one or
more rows into table-1.
Update statement
• The UPDATE statement is used to modified column values in selected
table rows.It has the following general format:
• UPDATE table-1 SET set-list [WHERE predicate];
• The optional WHERE clause has the same format as in SELECT
Statement.The WHERE clause chooses which table rows to update.If it
missing all rows are in table-1 are updated.The set-list contains
assignments of new values for selected columns.The SET Clause
expressions and WHERE clause predicate can contain sub queries but
subqueries cannot reference table-1.
Delete Statement:
• The DELETE Statement is used to remove selected rows from a table.It
has the following general format:
• DELETE FROM table_name[where condition];
• The optional WHERE clause has the same format as in the SELECT
statement.The WHERE clause chooses which table rows to delete.If it
is missing all rows are in table-1 are removed.The WHERE clause
predicate can contain sub-queries,but the sub-queries cannot
reference table-1.This prevents situations where results are
dependent on the order of processing.
Views:
• A view is a logical representation of another table or combination of
tables.A view derives its data from the tables on which it is based. It
does not physically store data like tables but represent data stored in
underlying tables in different formats.A view does not require disk
space and we can use view in most places where a table can be
used.Therefore views are called logical table or virtual table.it stored
in database in the form of query.Since the views are derived from
other tables thus when the data in its source tables are updated,the
view reflects the updates as well.View also can be used by DBA to
enforce database security by hiding confidential data or fields from
user.
Advantages of Views
• Database Security: view allows users to access only those sections of
database that directly concerns them.
• View provides data independence.
• Easier querying.
• Shielding from change.
• Views provide group of users to access the data according to their
criteria.
• Views allows the same data to be seen by different users in different
ways at the same time.
Syntax: CREATE VIEW<view name> as <query expression>
Where<query expression>is any legal query expression.

You might also like