unit2 rdbms and basic sql.pdf
TRANSCRIPT
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
1/61
Unit 2: Basic SQL
Pratian Technologies (India) Pvt. Ltd.www.pratian.com
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
2/61
Overview
UNIT 1 Introduction to RDBMSRelational DBMS
UNIT 2 Introduction to SQL
DDL Create, Alter, Drop, Truncate
UNIT 3 Data Manipulation Language
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
DML Insert, Update, Delete, Transactions Commit, Rollback
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
3/61
Overview
UNIT 4 Select StatementsSelect statement Where clause
Like, Logical operators, In and between predicates
Null, Not null, Aggregate functions
Order by
Group By
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
4/61
UNIT 1
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
Introduction to RDBMS
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
5/61
Relational DBMS
RDBMS is a model in which all data is logicallystructured with relations
Data is stored in tables
It is a two dimensional table with special propertiesStudents Table
StudentId Name Age CourseId
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
Courses Table
1002 Raghav K 21 2
CourseId Course
1 Basic SQL
2 Excel
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
6/61
Relational DBMS
Rows Tuples [collection of records]
Each record contains same fields
Columns Attributes Domain specific
Different types of data exist mainly - Character,Numeric, DateStudId
Name Age JoinDate Fees CourseId
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
1001 Krishna S 19 05/14/2010 1000 1
1002 Preethi J 21 05/15/2010 2000 2
1003 Sriram V 21 05/15/2010 1200 1
CourseId Course
1 Basic SQL
2 Excel
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
7/61
Relational DBMS - Properties
Entries of column and row values have to be singlevalued
Entries of attributes [columns] are of same kind
No two rows are identical
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
Order of rows and columns are unimportant
Every column has to be uniquely defined
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
8/61
Relational DBMS Database Schema
Organization of information within database for single ormultiple users
Store of data that describes the content and structure ofphysical data store
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
v u y ,relationships, access controls etc..
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
9/61
Relational DBMS - Architecture
Software to process queries/programs
DBMS
Application Programs/Queries
Users
DATABASE
SYSTEM
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
Stored Data Defn. Stored Database
Software to access stored data
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
10/61
Application Programmers Responsible for writing application programs that use the
database
End Users
Interact with the system from workstations or terminals. A givenend user can access the database via one of the applications
Administrators
Relational DBMS - Users
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
One who manages the database centrally Decides on the type of internal structures and relationships
Ensures security of the database
Controls access to data through codes and passwords
Can restrict views or operations users can perform on database
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
11/61
Relational DBMS Data Integrity
Data integrity Refers to wholeness and roundness of the database
Achieved by using integrity constraints and domain constraints
Primary Key Uniquely identify a particular record
Foreign Key
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
CourseId is the foreign key here
Stud_Id Name Age CourseId
1 Krishna 19 1
2 Radha 21 2
CourseId Course
1 SQL
2 Excel
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
12/61
Summary
Understand database concepts
Know characteristics of a DBMS
Understand Relational DBMS concepts
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
13/61
UNIT 2
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
Introduction to SQL
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
14/61
What is SQL?
SQL stands for Structured Query Language
Simple, powerful and standard data access languagefor relational database management systems
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
s a spec a ze anguage or up a ng, e e ng anrequesting information from databases
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
15/61
Standard SQL Statement Groups
Group Statements Description
DQL SELECT DATA QUERY LANGUAGEUsed to get data from database
DML INSERTUPDATEDELETE
DATA MANIPULATIONLANGUAGEUsed to add or change databasedata
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
ALTERDROPTRUNCATE
Used to manipulate databasestructures and definitions
DAL GRANT
REVOKE
DATA ADMINISTATION
LANGUAGEUsed to grant and revoke accessrights to database objects
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
16/61
Data Types in SQL
Data type Description
CHAR(n) To store fixed length string. Maximum length = 255bytes
VARCHAR(n) To store variable length string. Maximum length =
255 bytesTEXT or BLOB To store maximum length of 65535 characters.
Binary Large Objects Text, images, files etc..
TINYINT If signed: -128 to 127. If unsigned: 0 to 255. You
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
can specify a width of up to 4 digits.
SMALLINT If signed : -32768 to 32767. If unsigned : 0 to65535. You can specify a width of up to 5 digits.
INT If signed : -2147483648 to 2147483647. Ifunsigned : 0 to 4294967295. You can specify a
width of up to 11 digits.
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
17/61
Data Types in SQL
Data type Description
FLOAT(M,D) You can define the display length (M) and thenumber of decimals (D). Decimal precision can goto 24 places for a FLOAT.
DOUBLE(M,D) You can define the display length (M) and thenumber of decimals (D). Decimal precision can go
to 53 places for a FLOAT.
DATETIME A date and time combination in MM/DD/YYYY
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
HH:MM:SS format, between 01/01/1000 00:00:00and 12/31/9999 23:59:59. For example, 3:30 in theafternoon on December 30th, 1973 would be
stored as 12/30/1973 15:30:00
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
18/61
Creation of Database Objects - DDL
Table Tables are objects which store data
A table can have a maximum of 1000 columns
Syntax
CREATE TABLE table_name
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
({col_name col_datatype [[CONSTRAINTconst_name][col_constraint]]}
[table_constraint],
)[AS query]
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
19/61
Creation of Database Objects - DDL
Example
CREATE TABLE Students
(StudentId int,
Name varchar(100),
JoinDate date
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
); CREATE TABLE Courses
(
CourseId int,
Course varchar(50)
);
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
20/61
Creation of Database Objects - DDL
Naming of tables
Name of the table must begin with a letter A-Z or a-z. It maycontain numerals and the special character _ (underscore)
It can be 30 characters in length
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
It must not be a SQL reserved word
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
21/61
Specifying Integrity Constraints
A database is said to be integrated if the values in thedatabase are correct according to a set of rules
Checking of consistency must be carried out to ensuredata integrity
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
y uintegrity constraints
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
22/61
Specifying Integrity Constraints
Types of Constraints
Column constraints associated with a single column
Table constraints associate with more than one column
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
If not named, will be automatically generated by DBMS
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
23/61
Types of Integrity Constraints
NOT NULL
UNIQUE
PRIMARY KEY
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
FOREIGN KEY
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
24/61
NOT NULL CONSTRAINT
User will not be allowed to enter null values
Example:
CREATE TABLE Students
(
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
,
Name varchar(100) NOT NULL,
JoinDate date NOT NULL
);
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
25/61
DEFAULT CLAUSE
To specify a default value for an attribute if no value isgiven
Example:
CREATE TABLE Students
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
StudentId int,Name varchar(100),
JoinDate date,
City varchar(100) DEFAULT BANGALORE
);
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
26/61
UNIQUE CONSTRAINT
The keyword UNIQUE specifies that no two records canhave the same attribute value for a column
Example:
CREATE TABLE Courses
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
CourseId int,Course varchar(50) UNIQUE
);
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
27/61
PRIMARY KEY CONSTRAINT
A primary key constraint enables a unique identification ofeach record in a table
Example:
CREATE TABLE Students
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
StudentId int NOT NULL IDENTITY(1,1),
Name varchar(50),
JoinDate date,
PRIMARY KEY (StudentId)
);
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
28/61
FOREIGN KEY CONSTRAINT
This constraint specifies a column or list of columns as aforeign key of the referencing table
Referencing table is called Child table, referenced tableis called Parent table
Example:
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
CREATE TABLE Students
(
StudentId int NOT NULL PRIMARY KEY IDENTITY,
Name varchar(100),CourseId int FOREIGN KEY REFERENCES Courses(CourseId)
);
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
29/61
MODIFYING A TABLE
The ALTER command is used to modify an existing table
Syntax:
ALTER TABLE table_name
[ADD (col_name col_datatype col_constraint,..)] |
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
_ _ _ _ _
[ADD (table_constraint)] |
[DROP CONSTRAINT constraint_name] |
[DROP COLUMN existing_col_name];
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
30/61
MODIFYING A TABLE
ADD clause The ADD clause is used to add a column or a constraint to an
existing table
ALTER clause The ALTER clause is used to modify existing columns of a table
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
DROP clause DROP clause is used to remove columns or constraints from a
table
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
31/61
MODIFYING A TABLE
Example: -- To add a column
ALTER TABLE Students ADD Location varchar(100) DEFAULTBANGALORE;
-- To change the data type for a column
ALTER TABLE Courses ALTER COLUMN Course varchar(100);
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
-- To make a column unique ALTER TABLE Courses ADD CONSTRAINT uq_Course
UNIQUE(Course)
-- To rename a column in a table
Exec sp_rename Courses.Course , Courses.CourseName, Column
-- To drop a column
ALTER TABLE Students DROP COLUMN JoinDate;
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
32/61
DROP A TABLE
To remove the definition of a table from the databaseDROP TABLE command is used
Example:
DROP TABLE Courses;
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
33/61
Summary
Understand SQL as the standard language for interactingwith relational database
Know the type of SQL statements
Understand DDL statements to create relational tables
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
34/61
UNIT 3
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
Data Manipulation
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
35/61
DATA MANIPULATION
DML Data Manipulation Statements are used to makechanges to the data stored in a table
The manipulations that can be performed on a table are Add
Data is inserted into table using INSERT statement
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
Update
Any modifications to table data are made usingUPDATE statement
Delete
Table data is deleted using DELETE statement
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
36/61
INSERT STATEMENT
INSERT statement is used to add new data into the table
Syntax:
INSERT INTO table_name[(col_name1, col_name2,)]
{VALUES (value1, value2, ) | query };
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
Example: INSERT INTO Courses (CourseId, Course)
VALUES (2, EXCEL);
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
37/61
INSERT STATEMENT
Insert using query output Example: [CoursesOffered is another table with fields as Courses]
INSERT INTO Courses (CourseId, Course)
SELECT * FROM CoursesOffered
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
38/61
INSERT STATEMENT GENERAL RULES
Values should match data type of the respective columns
Number of values should match the number of column
names mentioned
All columns declared as NOT NULL should be supplied
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
w v u
Character strings should be enclosed in quotes
DATE values should be enclosed in quotes
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
39/61
UPDATE STATEMENT
Update statement is used when we need to modify data ina table
Syntax: UPDATE {table_name | alias}
SET col_name = value |
=
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
_
[WHERE Condition];
UPDATE STATEMENT
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
40/61
UPDATE STATEMENT
Update all rows UPDATE Students
SET JoinDate = 10/17/2010;
Update only certain rows
UPDATE Students
SET JoinDate = 10/17/2010
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
WHERE Name = Krishna;
Update multiple columns
UPDATE Students
SET JoinDate = 10/17/2010, Name = Krishna S
WHERE StudentId = 2;
DELETE STATEMENT
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
41/61
DELETE STATEMENT
DELETE statement is used to delete rows from a table
Syntax:
DELETE [FROM] {table_name | alias }[WHERE Condition];
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
DELETE FROM Students
Delete particular row from table
DELETE FROM Students WHERE StudentId = 2
S
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
42/61
Summary
We know how to manipulate data in a table using DMLcommands
Know how to save or undo changes made to table data
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
43/61
UNIT 4
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
SELECT STATEMENT
SELECT STATEMENT
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
44/61
SELECT STATEMENT
We will be looking at the following
SELECT STATEMENT
How to retrieve data from tables
Column aliases
Where clause
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
SELECT STATEMENT - more options Logical operators AND, OR, NOT
IN
BETWEEN
IS NULL / IS NOT NULL
GROUP BY / ORDER BY
SELECT STATEMENT
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
45/61
SELECT STATEMENT
Syntax:
SELECT [ALL | DISTINCT ] { * | col_name,..}
FROM table_name alias
[WHERE expr1]
[CONNECT BY expr2 [START WITH expr3]]
[GROUP BY expr4] [HAVING expr5]
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
[UNION | INTERSECT][ORDER BY expr | ASC | DESC];
COLUMN ALIAS AND DISTINCT
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
46/61
COLUMN ALIAS AND DISTINCT
Select Statement to get all columns SELECT * FROM Students;
Select Statement to get specific columns
SELECT StudentId, Name FROM Students;
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
SELECT StudentId, Name, JoinDate JoiningDateFROM Students;
DISTINCT To get unique values
SELECT DISTINCT Name FROM Students;
WHERE CLAUSE
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
47/61
WHERE CLAUSE
WHERE Clause Where clause is used to do selective retrieval of rows
Rows which meet search condition are returned
WHERE
List of Operators
=
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
Not equal to
< Less than
> Greater than
= Greater than equal to
WHERE CLAUSE
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
48/61
WHERE CLAUSE
To get all rows where join date is equal to a date SELECT * FROM Students WHERE JoinDate = 10/17/2010;
To get all rows where StudentId is greater than 2
SELECT * FROM Students WHERE StudentId > 2;
T ll r w wh r in i r r h n r l
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
a date SELECT * FROM Students WHERE JoinDate >= 10/17/2010;
LIKE PREDICATE
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
49/61
LIKE PREDICATE
The pattern contains a search string along with otherspecial characters % and _
An underscore(_) in the pattern matches exactly onecharacter
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
z
characters It cannot match a NULL
LIKE PREDICATE
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
50/61
LIKE PREDICATE
To list all students whose name begins with K SELECT Name
FROM Students
WHERE Name LIKE K%;
To list all students whose name begins with K and thirdletter I
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
SELECT Name
FROM Students
WHERE Name LIKE K_I%;
LOGICAL OPERATOR
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
51/61
LOGICAL OPERATOR
A logical operator combines the results of two componentconditions to produce a single result based on them orinvert the result of a single condition
NOT Returns true if condition is false, returns false ifcondition is true
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
AND Returns true if both component conditions are true,returns false if both component conditions are false
OR Returns true if either component condition is true,returns false if both component conditions are false
LOGICAL OPERATOR
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
52/61
LOGICAL OPERATOR
NOT SELECT Name FROM Students WHERE NOT StudentId = 2;
AND
SELECT Name FROM Students
WHERE StudentId > 1 AND JoinDate >= 10/17/2010;
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
OR SELECT Name FROM Students
WHERE Name = Krishna OR StudentId = 2;
IN PREDICATE
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
53/61
IN PREDICATE
To select rows from defined set of values
SELECT *
FROM Students
WHERE Name IN (Krishna, Radha);
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
BETWEEN PREDICATE
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
54/61
BETWEEN PREDICATE
To retrieve rows which meets the range of values given
SELECT * FROM Students
WHERE JoinDate
BETWEEN 10/17/2010 AND 10/20/2010;
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
NULL PREDICATE
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
55/61
U C
NULL predicate is used to check if an attribute or acolumn is null.
Col_name = NULL cannot be done
IS NULL
SELECT * FROM Students WHERE JoinDate IS NULL
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
IS NOT NULL SELECT * FROM Students WHERE Name IS NOT NULL
AGGREGATE FUNCTIONS
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
56/61
Produces a single value for an entire group
Functions are
COUNT()
Produces the number of rows query has selected
AVG()
Produces the average of all selected values of a given column
MAX()
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
Produces the largest of all selected values of a given column
MIN()
Produces the smallest of all selected values of a given column
SUM()
Produces the arithmetic sum of all selected values of a givencolumn
GROUP BY
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
57/61
The GROUP BY clause is used to group selected rowsand return a single row of summary information
Each group of rows are based on the values of theexpression(s) specified in the GROUP BY clause
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
u u u
GROUP BY
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
58/61
Example: SELECT CourseId, sum(Fees)
FROM Students
GROUP BY CourseId
SELECT CourseId, sum(Fees)
FROM Students
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
GROUP BY CourseId
HAVING CourseId IS NOT NULL
ORDER BY
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
59/61
ORDER BY clause is used to sort records
The sort is done on the column in either ascending ordescending order. [ASC default]
Example:
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
tu ents
ORDER BY NAME
Summary
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
60/61
Know how to use the SELECT statement to retrieve datafrom a table
Know the options to be used with SELECT statement forconditional data retrieval
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com
w w u v u
Know how to sort data
Question Time
-
7/28/2019 Unit2 RDBMS and Basic SQL.pdf
61/61
Please try to limit the questions to the topics discussed during the session. Thank you.
Basic SQLCopyright 2010 Pratian Technologieswww.pratian.com