Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Overview
UNIT 1 Introduction to RDBMS
Relational DBMS
Basic SQL
Overview
UNIT 4 Select Statements
Select statement Where clause Like, Logical operators, In and between predicates Null, Not null, Aggregate functions Order by Group By
Basic SQL
UNIT 1
Introduction to RDBMS
Basic SQL
Relational DBMS
RDBMS is a model in which all data is logically structured with relations Data is stored in tables It is a two dimensional table with special properties
Students Table StudentId 1001 1002 Courses Table CourseId 1 2 Course Basic SQL Excel Name Krishna S Raghav K Age 19 21 CourseId 1 2
Basic SQL
Relational DBMS
Rows Tuples [collection of records] Each record contains same fields Columns Attributes Domain specific Different types of data exist mainly - Character, Numeric, Date
StudI d 1001 1002 1003 Name Krishna S Preethi J Sriram V Age 19 21 21 JoinDate 05/14/2010 05/15/2010 05/15/2010 Course Basic SQL
Basic SQL
CourseId 1 2 1
CourseId 1
Copyright 2010 Pratian Technologies www.pratian.com
Excel
Basic SQL
Basic SQL
Application Programs/Queries
Stored Database
Basic SQL
End Users
Interact with the system from workstations or terminals. A given end user can access the database via one of the applications
Administrators
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
Basic SQL
Primary Key
Uniquely identify a particular record
Foreign Key
Primary key in one table will be referenced in another table CourseId is the foreign key here
Stud_Id 1 2 Name Krishna Radha CourseId 1 2
Copyright 2010 Pratian Technologies www.pratian.com
CourseId 1 2
Summary
Understand database concepts Know characteristics of a DBMS Understand Relational DBMS concepts
Basic SQL
UNIT 2
Introduction to SQL
Basic SQL
What is SQL?
SQL stands for Structured Query Language Simple, powerful and standard data access language for relational database management systems It is a specialized language for updating, deleting and requesting information from databases
Basic SQL
DQL DML
SELECT INSERT UPDATE DELETE CREATE ALTER DROP TRUNCATE GRANT REVOKE
DATA QUERY LANGUAGE Used to get data from database DATA MANIPULATION LANGUAGE Used to add or change database data DATA DEFINITION LANGUAGE Used to manipulate database structures and definitions DATA ADMINISTATION LANGUAGE Used to grant and revoke access rights to database objects
DDL
DAL
Basic SQL
Basic SQL
DOUBLE(M,D)
DATETIME
Basic SQL
Syntax
CREATE TABLE table_name ( {col_name col_datatype [[CONSTRAINT const_name][col_constraint]]} [table_constraint], ) [AS query]
Basic SQL
Basic SQL
Basic SQL
Basic SQL
Basic SQL
Basic SQL
Basic SQL
DEFAULT CLAUSE
To specify a default value for an attribute if no value is given Example:
CREATE TABLE Students ( StudentId int, Name varchar(100), JoinDate date, City varchar(100) DEFAULT BANGALORE );
Basic SQL
UNIQUE CONSTRAINT
The keyword UNIQUE specifies that no two records can have the same attribute value for a column Example:
CREATE TABLE Courses ( CourseId int, Course varchar(50) UNIQUE );
Basic SQL
Basic SQL
Basic SQL
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,..)] | [ALTER existing_col_name new_col_datatype new_constraint] | [ADD (table_constraint)] | [DROP CONSTRAINT constraint_name] | [DROP COLUMN existing_col_name];
Basic SQL
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
DROP clause
DROP clause is used to remove columns or constraints from a table
Basic SQL
MODIFYING A TABLE
Example:
-- To add a column ALTER TABLE Students BANGALORE; ADD Location varchar(100) DEFAULT
-- To change the data type for a column ALTER TABLE Courses ALTER COLUMN Course varchar(100); -- To make a column unique ALTER TABLE Courses UNIQUE(Course)
ADD
CONSTRAINT
uq_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;
Copyright 2010 Pratian Technologies www.pratian.com
Basic SQL
DROP A TABLE
To remove the definition of a table from the database DROP TABLE command is used Example:
DROP TABLE Courses;
Basic SQL
Summary
Understand SQL as the standard language for interacting with relational database Know the type of SQL statements Understand DDL statements to create relational tables
Basic SQL
UNIT 3
Data Manipulation
Basic SQL
DATA MANIPULATION
DML Data Manipulation Statements are used to make changes 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 Update Any modifications to table data are made using UPDATE statement Delete Table data is deleted using DELETE statement
Basic SQL
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 };
Example:
INSERT INTO Courses (CourseId, Course) VALUES (2, EXCEL);
Basic SQL
INSERT STATEMENT
Insert using query output
Example: [CoursesOffered is another table with fields as Courses] INSERT INTO Courses (CourseId, Course) SELECT * FROM CoursesOffered
Basic SQL
Basic SQL
UPDATE STATEMENT
Update statement is used when we need to modify data in a table Syntax:
UPDATE {table_name | alias} SET col_name = value | col_name = (SELECT Statement) [WHERE Condition];
Basic SQL
UPDATE STATEMENT
Update all rows
UPDATE Students SET JoinDate = 10/17/2010;
Basic SQL
DELETE STATEMENT
DELETE statement is used to delete rows from a table Syntax:
DELETE [FROM] {table_name | alias } [WHERE Condition];
Basic SQL
Summary
We know how to manipulate data in a table using DML commands Know how to save or undo changes made to table data
Basic SQL
UNIT 4
SELECT STATEMENT
Basic SQL
SELECT STATEMENT
We will be looking at the following SELECT STATEMENT
How to retrieve data from tables Column aliases Where clause Pattern matching LIKE
Basic SQL
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] [UNION | INTERSECT] [ORDER BY expr | ASC | DESC];
Basic SQL
Giving alias
SELECT StudentId, Name, JoinDate JoiningDate FROM Students;
Basic SQL
WHERE CLAUSE
WHERE Clause
Where clause is used to do selective retrieval of rows Rows which meet search condition are returned WHERE <search condition>
List of Operators
= <> < > <= >= Equal to Not equal to Less than Greater than Less than equal to Greater than equal to
Basic SQL
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 Join date is greater than or equal to a date
SELECT * FROM Students WHERE JoinDate >= 10/17/2010;
Basic SQL
LIKE PREDICATE
The pattern contains a search string along with other special characters % and _ An underscore(_) in the pattern matches exactly one character A percent sign (%) in the pattern can match zero or more characters
It cannot match a NULL
Basic SQL
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 third letter I
SELECT Name FROM Students WHERE Name LIKE K_I%;
Basic SQL
LOGICAL OPERATOR
A logical operator combines the results of two component conditions to produce a single result based on them or invert the result of a single condition NOT Returns true if condition is false, returns false if condition is true 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
Basic SQL
LOGICAL OPERATOR
NOT
SELECT Name FROM Students WHERE NOT StudentId = 2;
AND
SELECT Name FROM Students WHERE StudentId > 1 AND JoinDate >= 10/17/2010;
OR
SELECT Name FROM Students WHERE Name = Krishna OR StudentId = 2;
Basic SQL
IN PREDICATE
To select rows from defined set of values
SELECT * FROM Students WHERE Name IN (Krishna, Radha);
Basic SQL
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 SQL
NULL PREDICATE
NULL predicate is used to check if an attribute or a column is null. Col_name = NULL cannot be done IS NULL
SELECT * FROM Students WHERE JoinDate IS NULL
IS NOT NULL
SELECT * FROM Students WHERE Name IS NOT NULL
Basic SQL
AGGREGATE FUNCTIONS
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() 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 given column
Basic SQL
GROUP BY
The GROUP BY clause is used to group selected rows and return a single row of summary information Each group of rows are based on the values of the expression(s) specified in the GROUP BY clause Grouping can be done on multiple columns
Basic SQL
GROUP BY
Example:
SELECT CourseId, sum(Fees) FROM Students GROUP BY CourseId SELECT CourseId, sum(Fees) FROM Students GROUP BY CourseId HAVING CourseId IS NOT NULL
Basic SQL
ORDER BY
ORDER BY clause is used to sort records The sort is done on the column in either ascending or descending order. [ASC default] Example:
SELECT * FROM Students ORDER BY NAME
Basic SQL
Summary
Know how to use the SELECT statement to retrieve data from a table Know the options to be used with SELECT statement for conditional data retrieval Know how to group data based on a value Know how to sort data
Basic SQL
Question Time
Please try to limit the questions to the topics discussed during the session. Thank you.
Basic SQL