DBMSsqlpart
DBMSsqlpart
DBMSsqlpart
BASIC SQL
SQL stands for Structured Query Language
SQL is a standard language for accessing and manipulating databases.
• Schema
– The structure that contains descriptions of objects created by a user (base tables, views, constraints)
• Creating tables
• Inserting/Updating/Deleting data
• Retrieving data
– Single table queries
– Where
– Joins
– Grouping
SQL
• SQL is a data manipulation language.
Tuples or rows
Tables Explained
SELECT <attributes>
FROM <one or more relations>
WHERE <conditions>
Notation
Input Schema
Output Schema
• Case insensitive:
– Same: SELECT Select select
– Same: Product product
– Different: ‘Seattle’ ‘seattle’
• Constants:
– ‘abc’ - yes
– “abc” - no
Removing Tables
37
Updating Data
UPDATE COURSE SET HOURS=5;
CHANGES EVERY ROW
ERROR :
integrity constraint violated - child record found
Delete Statement
Removes rows from a table
Delete certain rows
DELETE FROM CUSTOMER_T WHERE
CUSTOMERSTATE = ‘HI’;
Delete all rows
DELETE FROM CUSTOMER_T;
43
Deleting Data
Be careful!! This deletes ALL of
the rows in your table. If you
DELETE COURSE; use this command in error, you
can use ROLLBACK to undo
Deletes All Rows the changes.
ERROR at line 1:
integrity constraint violated - child record
found
SELECT Statement
• Used for queries on single or multiple tables
• Clauses of the SELECT statement:
SELECT
- List the columns (and expressions) to be returned from the query
FROM
- Indicate the table(s) or view(s) from which data will be obtained
WHERE
- Indicate the conditions under which a row will be included in the result
GROUP BY
- Indicate categorization of results
HAVING
- Indicate the conditions under which a category (group) will be included
ORDER BY
- Sorts the result according to specified criteria
47
SQL statement
processing
SELECT Example
• Find products with standard price less than $275
49
SQL - Other Features
• DISTINCT
• Arithmetic operators: +, -, *, /
• Comparison operators: =, >, >=, <, <=, <>
• Concatenation operator: ||
• Substring comparisons: %, _
• BETWEEN
• AND, OR, NOT
The LIKE operator
SELECT *
FROM Products
WHERE PName LIKE ‘%gizmo%’
Compare to:
Category
Gadgets
SELECT category
Gadgets
FROM Product
Photography
Household
Ordering the Results
Ties are broken by the second attribute on the ORDER BY list, etc.
SELECT Category
FROM Product
ORDER BY PName
Canon 65 Japan
Hitachi 15 Japan
Product
4. Give syntax and apply the SQL commands for defining two
example tables of your choice. Then insert data, update data
in the tables
Questions
4. Explain Primary and Foreign Key constraints with examples.
Create tables for the above schema with primary key, foreign
key and not null constraints wherever necessary.
Asignment Question
7. Consider the following schema and answer the following queries in
SQL.
Suppliers( sid:integer, sname:string, address:string)
Parts(pid:integer, pname:string, colour:string)
Catalog( sid:integer, pid:integer, cost:real)
i)Find the ids and names of suppliers whose name begins with
character “A”.
ii)Find the ids of suppliers who supply some Red colour part.
iii)Find the ids of suppliers who supply either a Red colour part or a
Green colour part.
iv)Find the names of all parts supplied by supplier named John.
Assignment Question
7. Consider the following schema and answer the following queries in
SQL.
Students(sid: string, sname:string, Date_of_Birth:date, GPA:real)
Enrolled(sid:string, cid:string)
i) For each course offered by CSE department, find the total number of
enrollments.
ii) Find the sum of credits of all courses taken by student “ S01”.