Presentation is loading. Please wait.

Presentation is loading. Please wait.

Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a. SQL2), SQL99 (a.k.a. SQL3),

Similar presentations


Presentation on theme: "Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a. SQL2), SQL99 (a.k.a. SQL3),"— Presentation transcript:

1 Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a. SQL2), SQL99 (a.k.a. SQL3), ….

2  Data Definition Language (DDL)  Create/alter/delete tables and their attributes  Data Manipulation Language (DML)  Query one or more tables.  Insert/delete/modify tuples in tables

3 PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi Product Attribute names Table name Tuples or rows

4  Characters: CHAR(20), VARCHAR(50)  Numbers: INT, BIGINT, SMALLINT, FLOAT  Others: TIME,DATE, DATETIME, …

5  Primary key-Primary keys must contain UNIQUE values, Cannot contains null values.  Alternate key-In the table one attributes will be the primary key and others will be assume as secondary (or) Alternate keys.  Unique key- The Unique keys uniquely identifies each record in a database table.

6  Foreign key: A foreign key in one table points to a primary key in another table.  Candidate key: Candidate key is same as the secondary key.  Super key: A super key is a set of attributes within a table whose values can be used to uniquely identify a tuple.  Composite/Compound key: It means combination of two attributes to uniquely identified keys are compound (or) Composite keys.

7  NOT NULL.  DEFAULT.  UNIQUE.  PRIMARY KEY.  FOREIGN KEY.  CHECK.  AUTO_INCREMENT.

8  NOT NULL: The Not Null constraint enforces a column to Not accept Null values  DEFAULT: The Default constraint is used to insert a default value into a column.  Unique key: The Unique keys uniquely identifies each record in a database table.  Primary key: Primary keys must contain UNIQUE values, Cannot contains null values.

9  Foreign key: A foreign key in one table points to a primary key In another table.  Check: The Check constraint is used to limit the value range that can be placed in a column  Auto_Increment:Very often we would like the value of the primary key field to be created automatically every time a new record is inserted.

10 SQL CREATE DATABASE Statement: SQL DROP DATABASE Statement: SQL USE DATABASE Statement: CREATE DATABASE database_name; Ex : CREATE DATABASE student ; CREATE DATABASE database_name; Ex : CREATE DATABASE student ; DROP DATABASE database_name; Ex: DROP DATABASE student; DROP DATABASE database_name; Ex: DROP DATABASE student; USE DATABASE database_name;; Ex: USE DATABASE student; USE DATABASE database_name;; Ex: USE DATABASE student;

11 SQL CREATE TABLE Statement: CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype,..... columnN datatype, PRIMARY KEY( one or more columns ) ); CREATE TABLE Customers( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), ORDERS VARCHAR(155), PRIMARY KEY(ID) ); CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype,..... columnN datatype, PRIMARY KEY( one or more columns ) ); CREATE TABLE Customers( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), ORDERS VARCHAR(155), PRIMARY KEY(ID) );

12 SQL ALTER TABLE Statement: SQL ALTER TABLE Statement (Rename): ALTER TABLE table_name {ADD|DROP|MODIFY} column_name {data type}; Ex: ALTER TABLE Customers {ADD|DROP|MODIFY} AGE INT NOT NULL ; ALTER TABLE table_name {ADD|DROP|MODIFY} column_name {data type}; Ex: ALTER TABLE Customers {ADD|DROP|MODIFY} AGE INT NOT NULL ; ALTER TABLE table_name RENAME TO new_table_name; Ex: ALTER TABLE Customers RENAME TO Customers2; ALTER TABLE table_name RENAME TO new_table_name; Ex: ALTER TABLE Customers RENAME TO Customers2;

13 SQL DROP TABLE Statement: SQL TRUNCATE TABLE Statement: DROP TABLE table_name;; Ex: DROP TABLE Customers2; DROP TABLE table_name;; Ex: DROP TABLE Customers2; TRUNCATE TABLE table_name; Ex: TRUNCATE TABLE Customers2; TRUNCATE TABLE table_name; Ex: TRUNCATE TABLE Customers2;

14 UPDATE VIEW statement: CREATE VIEW view_name AS SELECT column1, column2..... FROM table_name WHERE [condition]; Ex: CREATE VIEW Customers_View AS SELECT name, age FROM Customers2; CREATE VIEW view_name AS SELECT column1, column2..... FROM table_name WHERE [condition]; Ex: CREATE VIEW Customers_View AS SELECT name, age FROM Customers2; UPDATE VIEW_NAME SET Column_name= New_value WHERE [condition]; Ex: UPDATE Customers_View SET AGE = 35 WHERE name='Ramesh'; UPDATE VIEW_NAME SET Column_name= New_value WHERE [condition]; Ex: UPDATE Customers_View SET AGE = 35 WHERE name='Ramesh'; CREATE VIEW Statement:

15 DROP VIEW Statement: DELETING ROWS FROM VIEWS: DROP VIEW view_name; Ex: DROP VIEW Customers_view; DROP VIEW view_name; Ex: DROP VIEW Customers_view; DELETE FROM VIEW_NAME WHERE [Condition]; Ex: DELETE FROM Customers_View WHERE age = 22; DELETE FROM VIEW_NAME WHERE [Condition]; Ex: DELETE FROM Customers_View WHERE age = 22;

16 PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks SingleTouch$149.99PhotographyCanon MultiTouch$203.99HouseholdHitachi SELECT * FROM Product WHERE category=‘Gadgets’ Product PNamePriceCategoryManufacturer Gizmo$19.99GadgetsGizmoWorks Powergizmo$29.99GadgetsGizmoWorks “selection” SELECT Statement: SELECT * FROM Table_name WHERE [condition];

17 SQL INSERT INTO Statement: SQL UPDATE Statement: INSERT INTO table_name( column1, column2....columnN) VALUES ( value1, value2....valueN); Ex: INSERT INTO Customers(name,age,address) VALUES (Rakesh,25,koramangala); INSERT INTO table_name( column1, column2....columnN) VALUES ( value1, value2....valueN); Ex: INSERT INTO Customers(name,age,address) VALUES (Rakesh,25,koramangala); UPDATE table_name SET column1 = value1, column2 = value2....columnN=valueN [ WHERE CONDITION ]; Ex: UPDATE Customers SET age=26 WHERE name=‘Rakesh’; UPDATE table_name SET column1 = value1, column2 = value2....columnN=valueN [ WHERE CONDITION ]; Ex: UPDATE Customers SET age=26 WHERE name=‘Rakesh’; SQL DELETE Statement: DELETE FROM table_name WHERE [CONDITION]; Ex: DELETE FROM Customers WHERE name=‘Rakesh’; DELETE FROM table_name WHERE [CONDITION]; Ex: DELETE FROM Customers WHERE name=‘Rakesh’;

18 Product(PName, Price, Category, Manfacturer) Answer(PName, Price, Manfacturer) Input Schema Output Schema SELECT column1, column2, columnN FROM table_name WHERE [condition]; Ex: SELECT PName, Price, Manufacturer FROM Product WHERE Price > 100; SELECT column1, column2, columnN FROM table_name WHERE [condition]; Ex: SELECT PName, Price, Manufacturer FROM Product WHERE Price > 100; SQL WHERE Clause:

19 SQL DISTINCT Clause: SELECT DISTINCT column1, column2....columnN FROM table_name; Ex: SELECT DISTINCT name,age FROM Customers; SELECT DISTINCT column1, column2....columnN FROM table_name; Ex: SELECT DISTINCT name,age FROM Customers; SQL ORDER BY Clause: SELECT column-list FROM table_name [WHERE condition] [ORDER BY column1, column2,.. columnN] [ASC | DESC]; Ex: SELECT name,age FROM Customers WHERE age>=25 ORDER BY name DESC; SELECT column-list FROM table_name [WHERE condition] [ORDER BY column1, column2,.. columnN] [ASC | DESC]; Ex: SELECT name,age FROM Customers WHERE age>=25 ORDER BY name DESC; SQL Group By Clause: SELECT column1, column2 FROM table_name WHERE [ conditions ] GROUP BY column1, column2 ORDER BY column1, column2 ; Ex:SELECT Name, SUM(Salary) FROM Customers GROUP BY Name; SELECT column1, column2 FROM table_name WHERE [ conditions ] GROUP BY column1, column2 ORDER BY column1, column2 ; Ex:SELECT Name, SUM(Salary) FROM Customers GROUP BY Name;

20 SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product SELECT product, Sum(price*quantity) AS TotalSales FROM Purchase WHERE date > ‘10/1/2005’ GROUP BY product ProductDatePriceQuantity Bagel10/21120 Bagel10/251.5020 Banana10/30.510 Banana10/10110 ProductTotalSales Bagel50 Banana15

21 SQL HAVING Clause: SELECT column1, column2 FROM table1, table2 WHERE [ conditions ] GROUP BY column1, column2 HAVING [ conditions ] ORDER BY column1, column2; Ex: SELECT * FROM Customers GROUP BY age HAVING COUNT(age) >= 2; SELECT column1, column2 FROM table1, table2 WHERE [ conditions ] GROUP BY column1, column2 HAVING [ conditions ] ORDER BY column1, column2; Ex: SELECT * FROM Customers GROUP BY age HAVING COUNT(age) >= 2;

22 AND OPERATION: The AND operator displays a record if both the first condition AND the second condition are true SYNTAX: SELECT * FROM Customers WHERE Country='Germany' AND City='Berlin'; OR OPERATION: The OR operator displays a record if either the first condition OR the second condition is true SYNTAX: SELECT * FROM Customers WHERE City='Berlin' OR City='München';

23 SQL LIKE Clause: The SQL LIKE clause is used to compare a value to similar values using wildcard operators. There are two wildcards used in conjunction with the LIKE operator:  The percent sign (%)  The underscore (_) SELECT FROM table_name WHERE column LIKE 'XXXX%' or SELECT FROM table_name WHERE column LIKE '%XXXX%' or SELECT FROM table_name WHERE column LIKE 'XXXX_' or SELECT FROM table_name WHERE column LIKE '_XXXX' or SELECT FROM table_name WHERE column LIKE '_XXXX_'

24 SQL IN Clause: SELECT column1, column2....columnN FROM table_name WHERE column_name IN (val-1, val-2,...val-N); Ex: SELECT * FROM Customers WHERE city IN(’paris’,’london’); SQL BETWEEN Clause: SELECT column1, column2....columnN FROM table_name WHERE column_name BETWEEN val-1 AND val-2; Ex: SELECT * FROM Customers WHERE price [NOT]BETWEEN 100 AND 200;

25 SQL Join Types:  INNER JOIN: returns rows when there is a match in both tables.  LEFT JOIN: returns all rows from the left table, even if there are no matches in the right table.  RIGHT JOIN: returns all rows from the right table, even if there are no matches in the left table.  FULL JOIN: returns rows when there is a match in one of the tables.  CARTESIAN JOIN: returns the Cartesian product of the sets of records from the two or more joined tables.

26 INNER JOIN: The most frequently used and important of the joins is the INNER JOIN. They are also referred to as an EQUIJOIN. Syntax: SELECT table1.column1, table2.column2... FROM table1 INNER JOIN table2 ON table1.common_filed = table2.common_field; Ex: SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS INNER JOIN ORDERS ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

27 LEFT JOIN: Syntax: SELECT table1.column1, table2.column2... FROM table1 LEFT JOIN table2 ON table1.common_filed = table2.common_field; Ex: SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS LEFT JOIN ORDERS ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

28 RIGHT JOIN: Syntax: SELECT table1.column1, table2.column2... FROM table1 RIGHT JOIN table2 ON table1.common_filed = table2.common_field; Ex: SELECT ID, NAME, AMOUNT, DATE FROM CUSTOMERS RIGHT JOIN ORDERS ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

29 SELECT count(*) FROM Product WHERE year > 1995 SELECT count(*) FROM Product WHERE year > 1995 Except count, all aggregations apply to a single attribute SELECT avg(price) FROM Product WHERE maker=“Toyota” SELECT avg(price) FROM Product WHERE maker=“Toyota” SQL supports several aggregation operations: sum, count, min, max, avg,first,last.

30 SELECT min(price) FROM Product WHERE maker=“Toyota” SELECT min(price) FROM Product WHERE maker=“Toyota” SELECT max(price) FROM Product WHERE maker=“Toyota” SELECT max(price) FROM Product WHERE maker=“Toyota” SELECT first(price) FROM Product WHERE maker=“Toyota” SELECT first(price) FROM Product WHERE maker=“Toyota” SELECT last(price) FROM Product WHERE maker=“Toyota” SELECT last(price) FROM Product WHERE maker=“Toyota” SELECT sum(price) FROM Product WHERE maker=“Toyota” SELECT sum(price) FROM Product WHERE maker=“Toyota”

31 Database normalization is the process of efficiently organizing data in a database. There are two reasons of the normalization process:  Eliminating redundant data, for example, storing the same data in more than one table.  Ensuring data dependencies make sense.  First Normal Form (1NF)  Second Normal Form (2NF)  Third Normal Form (3NF)

32 First normal form (1NF) sets the very basic rules for an organized database:  Define the data items required, because they become the columns in a table. Place related data items in a table.  Ensure that there are no repeating groups of data.  Ensure that there is a primary key.

33 Second normal form states that it should meet all the rules for 1NF and there must be no partial dependences of any of the columns on the primary key:

34 A table is in third normal form when the following conditions are met:  It is in second normal form.  All non primary fields are dependent on the primary key

35 Sql Statements are used to retrieve data from the database. We can get same results by writing different sql queries. But use of the best query is important when performance is considered. So you need to sql query tuning based on the requirement. Here is the list of queries which we use reqularly and how these sql queries can be optimized for better performance. The sql query becomes faster if you use the actual columns names in SELECT statement instead of than '*'. For Example: Write the query as SELECT id, first_name, last_name, age, subject FROM student_details; Instead of: SELECT * FROM student_details;

36 Symbols and Notations ER-Diagram is a visual representation of data that describes how data is related to each other..

37 Entity: Entities are represented by means of rectangles. Rectangles are named with the entity set they represent. Attributes: Attributes are the properties of entities. Attributes are represented by means of ellipses. Every ellipse represents one attribute and is directly connected to its entity (rectangle).

38 Relationship: A Relationship describes relations between entities. Relationship is represented using diamonds. Composite attribute: An attribute can also have their own attributes. These attributes are known as Composite attribute. Key attribute: Key attribute represents the main characterstic of an Entity. It is used to represent Primary key. Ellipse with underlying lines represent Key Attribute.


Download ppt "Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a. SQL2), SQL99 (a.k.a. SQL3),"

Similar presentations


Ads by Google