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

Lecture 9 SQL

The document provides an introduction to SQL and relational database management systems (RDBMS). It defines key terms like database, data, and DBMS. It explains that RDBMS data is structured with tables, rows, and fields and describes common RDBMS components like MySQL and SQL Server. It also summarizes the differences between DBMS and RDBMS, and describes the core SQL languages: DDL, DML, and DCL and how they are used to define, manipulate, and control database access and security.

Uploaded by

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

Lecture 9 SQL

The document provides an introduction to SQL and relational database management systems (RDBMS). It defines key terms like database, data, and DBMS. It explains that RDBMS data is structured with tables, rows, and fields and describes common RDBMS components like MySQL and SQL Server. It also summarizes the differences between DBMS and RDBMS, and describes the core SQL languages: DDL, DML, and DCL and how they are used to define, manipulate, and control database access and security.

Uploaded by

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

Introduction to SQL

BASIC DEFINITIONS
⮚ Database: A collection of related data.
⮚ Data: Known facts that can be recorded and have an implicit
meaning.
⮚ Database Management System (DBMS): A software
package/ system to facilitate the creation and maintenance
of a computerized database.
⮚ Database System: The DBMS software together with the
data itself. Sometimes, the applications are also included.
RELATIONAL DATABASE MANAGEMENT SYSTEM
(RDBMS):

⮚ RDBMS data is structured in database tables, fields and records.


⮚ Each RDBMS table consists of database table rows.
⮚ Each database table row consists of one or more database table fields.
⮚ RDBMS store the data into collection of tables, which might be related by
common fields (database table columns).
⮚ RDBMS also provide relational operators to manipulate the data stored
into the database tables.
⮚ Most RDBMS use SQL as database query language.
⮚ Edgar Codd introduced the relational database model.
⮚ The most popular RDBMS are MS SQL Server, DB2, Oracle and MySQL.
Most RDBMS use SQL as database query language.
What is RDBMS?
• A relational database management system (RDBMS or just RDB)
is a common type of database that stores data in tables, so it can
be used in relation to other stored datasets.
• Most databases used by businesses these days are relational
databases, as opposed to a flat file or hierarchical database.
• The majority of current IT systems and applications are based on
a relational DBMS.
Difference between DBMS and RDBMS?
S.No. DBMS RDBMS
1 DBMS applications store data as file. RDBMS applications store data in a
tabular form.
2 In DBMS, data is generally stored in In RDBMS, the tables have an identifier
either a hierarchical form or a called primary key and the data values
navigational form. are stored in the form of tables.
3 Normalization is not present in Normalization is present in RDBMS.
DBMS.
4 DBMS does not apply any RDBMS defines the integrity
security with regards to data constraint for the purpose of ACID
manipulation. (Atomocity, Consistency, Isolation and
Durability) property.
5 DBMS uses file system to store data, in RDBMS, data values are stored in the
so there will be no relation form of tables, so a relationship
between the tables. between these data values will be
stored in the form of a table as well.
Continued….

S.No. DBMS RDBMS


1 DBMS has to provide some RDBMS system supports a tabular
uniform methods to access the structure of the data and a relationship
stored information. between them to access the stored
information.
2 DBMS does not support RDBMS supports distributed database.
distributed database.
3 DBMS is meant to be for small RDBMS is designed to handle large
organization and deal with small amount of data. it supports multiple
data. it supports single user. users.
4 Examples of DBMS are file Example of RDBMS
systems, xml etc. are mysql, postgre, sql server, oracle etc.
INTRODUCTION TO SQL

SQL stands for Structured Query Language.


⮚ SQL lets you access and manipulate databases.
⮚ 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 and many more
DATA TYPES COMMONLY USED IN SQL

⮚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 it can hold.
The maximum number of characters (i.e. the size) this data type can hold is 255
characters.
The data held is right padded with spaces to whatever length specified.

⮚VARCHAR (size): This data type is used to store variable length alphanumeric data.
It is a flexible form of CHAR data type.
The maximum data this data type can hold is 4000 characters.

⮚DATE: This data type is used to represent date.


Date time stores date in the 24-hour format.

⮚NUMBER (P,S): The NUMBER data type is used to store numbers(fixed or floating point).
Numbers of virtually any magnitude maybe stored up to 38 digits of precision.
SQL COMPONENTS

SQL consists of three components:

1. Data Definition Language (DDL)


2. Data Manipulation Language (DML)
3. Data Control Language (DCL)
DATA DEFINITION LANGUAGE (DDL)

This component of the SQL language is used to create and modify tables and other
objects in the database.

For tables there are three main commands:

CREATE TABLE tablename 🡪 to create a table in the database


DROP TABLE tablename 🡪 to remove a table from the database
ALTER TABLE tablename 🡪 to add or remove columns from a table in the database
RENAME <new table name> to <old table name>; 🡪 To change the name of a table as
per the user’s wish or requirement.
TRUNCATE TABLE<table name>; 🡪 Truncate Tables empties the table completely. Once
the data deleted cannot be retrieved.
DESC tablename 🡪 To view just the blank table
CREATE DATABASE COMMAND

Purpose:- Database is collection of tables that are the basic unit of


data storage in an Oracle Database. Data is stored in rows and
columns.

Syntax:- CREATE DATABASE databasename;


Example:-
Create database testdb;
Use testdb;
CREATE TABLE COMMAND

Purpose:- Tables are the basic unit of data storage in Database. Data
is stored in rows and columns.

Syntax:-
SQL Constraints

• Constraints are used to limit the type of data that can go into a table.

• Constraints can be specified when a table is created (with the CREATE TABLE
statement) or after the table is created (with the ALTER TABLE statement).

• Here are the most important constraints:


• PRIMARY KEY
• NOT NULL
• UNIQUE
• FOREIGN KEY
• CHECK
• DEFAULT
• IDENTITY
PRIMARY KEY
• The PRIMARY KEY constraint uniquely identifies each record in a database table.
• Primary keys must contain unique values. It is normal to just use running numbers, like 1, 2,3, 4,
5, … as values in Primary Key column.
• It is a good idea to let the system handle this for you by specifying that the Primary Key should be
set to identity(1,1).
• IDENTITY(1,1) means the first value will be 1 and then it will increment by 1.
• Each table should have a primary key, and each table can have only ONE primary key.
FOREIGN KEY

• A FOREIGN KEY in one table points to a PRIMARY KEY in another table.


NOT NULL / Required Columns

• The NOT NULL constraint enforces a column to NOT accept NULL values.
• The NOT NULL constraint enforces a field to always contain a value. This means that you cannot
insert a new record, or update a record without adding a value to this field.

We see that “CustomerNumber”, “LastName” and “FirstName” is set to “NOT NULL”, this means these columns
needs to contain data. While “AreaCode”, “Address” and “Phone” may be left empty, i.e, they don’t need to be filled
out.

Note! A primary key column cannot contain NULL values.


UNIQUE

• The UNIQUE constraint uniquely identifies each record in a database table.


• The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column
or set of columns.
• A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.
• In example below, we see that the “CustomerNumber” is set to UNIQUE, meaning each customer
must have a unique Customer Number.
• Note! You can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per
table.
CHECK

• The CHECK constraint is used to limit the value range that can be placed in a column.
• If you define a CHECK constraint on a single column it allows only certain values for this column.
• If you define a CHECK constraint on a table it can limit the values in certain columns based on
values in other columns in the row.

• In this case, when we try to insert a Customer Number less than zero we will get an error message.
DEFAULT

• The DEFAULT constraint is used to insert a default value into a column.


• The default value will be added to all new records, if no other value is specified.
ALTER TABLE COMMAND

Purpose:- The structure of a table can be modified by using a alter table command. Alter
Table allows changing the structure of an existing table. With Alter Table it is possible to add
or delete columns, create or destroy indexes, change the data type of existing columns, or
rename columns or the table itself
Syntax:-
 Adding New Columns
Alter table <table name> add(<new col name> <data type> (<Size>), <new col name> <data
type> (<Size>),…..);

 Modifying Existing Column


Alter table <table name> modify(<col name> <new data type> (< new Size>), <col name>
<new data type> (<new Size>),…..);

 Dropping A Column
Alter table <table name> drop column <col name>;
ALTER TABLE
• The ALTER TABLE statement is used to add, delete, or modify columns in
an existing table.

Example:-

Alter table STUDENT add( Telephone number(10));


Alter table STUDENT modify( Telephone number(20));
Alter table STUDENT drop column Telephone;
RENAME TABLE COMMAND

Purpose:- To change the name of a table as per the user’s wish or


requirement.

Syntax:- Rename <new table name> to <old table name>;

Example:- Rename STUDENT to group_a101;


DROP TABLE COMMAND

Purpose:- Sometimes table in the database becomes obsolete and need


to be discarded, so we use drop table command.

Syntax:- Drop table <table name>;

Example:-
Drop table CSEA_Data101;
DATA MANIPULATION LANGUAGE (DML)

DML component of the SQL language is used to manipulate data within a


table. There are four main commands:

SELECT 🡪 to select rows of data from a table

INSERT 🡪 to insert rows of data into a table

UPDATE 🡪 to change rows of data in a table

DELETE 🡪 to remove rows of data from a table


INSERT COMMAND
Command:- Insert data into a table

Purpose:- The Insert Into Table command is used to load the created table
with data to be manipulated later.

Syntax:- Insert into <table name>(<col1>,<col2>,……..,<col n>) values


(<expression1>, <expression2> ,……,<expression3>);

Example:-
Insert into STUDENT values(101, ‘Aanchal’, ’CSE’);
Insert into STUDENT values(126, ‘Jotnain’, ’CSE’);
Insert into STUDENT values(149, ‘Sapanpreet’, ’ECE’);
Insert into STUDENT values(151, ‘Savita’, ’CSE’);
UPDATE

• The UPDATE statement is used to update existing records in a table.


• The syntax is as follows:
DELETE
• The DELETE statement is used to delete rows in a table.
• Syntax:
TRUNCATE
• The SQL TRUNCATE TABLE command is used to empty a table
• Syntax:
Difference between DELETE, DROP and TRUNCATE

DELETE DROP TRUNCATE


1. It is a DML command. 1. It is a DDL command. 1. It is a DDL command.

2. It is used to delete rows or records 2. It is used to delete the entire table 2. It is used to delete the entire
based on conditions specified in the along with its schema and structure records of a table without affecting
WHERE clause. respectively. the schema of the table.

3. If the WHERE clause is not specified


with conditions it deletes all the 3. There is no WHERE clause. 3. There is no WHERE clause.
records of the table.

4. It is a DDL command. As a result, 4. It is a DDL command. As a result,


4. It is a DML command. As a result,
the changes cannot be rolled back or the changes cannot be rolled back or
the operation can be rolled back.
undone. undone.

5. It is faster than DELETE in execution


5. It scans every row before deleting because it does not scan every row
5. It is faster and time-saving.
making it slower and time-consuming. before deleting which makes it the
least time-consuming.

6. Syntax: DELETE FROM TABLE 6. Syntax: TRUNCATE TABLE


6. Syntax: DROP TABLE Table_Name;
Table_Name WHERE [CONDITIONS]; Table_Name;
SELECT

• The SELECT statement is probably the most used SQL command.


• The SELECT statement is used for retrieving rows from the database and enables the selection of
one or many rows or columns from one or many tables in the database.
SELECT DISTINCT

• In a table, some of the columns may contain duplicate values.


• This is not a problem,however, sometimes you will want to list only the different (distinct) values
in a table.
• The DISTINCT keyword can be used to return only distinct (different) values.
• The syntax is as follows:
ORDER BY Keyword

• If you want the data to appear in a specific order you need to use the “order by” keyword.
• If you use the “order by” keyword, the default order is ascending (“asc”). If you want the order to
be opposite, i.e., descending, then you need to use the “desc” keyword.
The WHERE Clause

• The WHERE clause is used to extract only those records that fulfill a specified
criterion.
Operators

• With the WHERE clause, the following operators can be used:


LIKE Operator

• The LIKE operator is used to search for a specified pattern in a


column.
IN Operator

• The IN operator allows you to specify multiple values in a WHERE


clause.
BETWEEN Operator

• The BETWEEN operator selects a range of data between two values. The values can be numbers,
text, or dates.
AND & OR Operators

• The AND operator displays a record if both the first condition and the second condition is true.
• The OR operator displays a record if either the first condition or the second condition is true.
Thank you

47

You might also like