Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23
SQL statements: Listing Unique Values
SQL’s DISTINCT clause is used to produce a list
of only those values that are different from one another. Syntax: SELECT DISTINCT <attribute> FROM <tablename>; Aggregate functions in SQL • SQL can perform various mathematical summaries for you, such as counting the number of rows that contain a specified condition, finding the minimum or maximum values for some specified attribute, summing the values in a specified column, and averaging the values in a specified column. Aggregate functions: COUNT COUNT - it is used to tally the number of non-null values of an attribute. Syntax: SELECT count <attributes> FROM <tablename> WHERE <conditionlist> When used to count unique non-null values. Syntax: SELECT count <DISTINCT attribute> FROM <tablename> WHERE <conditionlist> Aggregate functions: MAX and MIN MAX and MIN • The MAX and MIN functions help you find answers to problems such as the: – Highest (maximum) price in the PRODUCT table. – Lowest (minimum) price in the PRODUCT table. Syntax: SELECT <MAX(attribute1)> FROM <tablename> WHERE <conditionlist> MAX and MIN cont… Why is this query wrong? SELECT * FROM product WHERE p_price = MAX(p_price); Only one parameter is expected to the right of the equals sign. Solution: Compute the maximum price first, then compare it to each price returned by the query. This is known as a nested query. Aggregate functions: SUM and AVG SUM syntax: SELECT <SUM(attribute1)> AS <alias> FROM <tablename>; AVG syntax: SELECT <AVG(attribute1)> AS <alias> FROM <tablename>; SQL joins • Supposing you want to retrieve data from two different tables? • All you need is an equality comparison of the foreign key and the primary key of the two tables. Inner join Inner join - returns matched records from the tables that are being joined. Syntax: SELECT <attributes> FROM <table1, table2> WHERE table1.attributeX = table2.attributeX Outer join • The previous query will exclude any rows which violates the condition i.e conditions with a null value at the foreign key. An outer join is used to include these rows. • Outer join, the matched pairs would be retained, and any unmatched values in the other table would be left null. • Types of outer joins: – Left join – Right join Outer join cont.. • Left join syntax: – SELECT <attribute1,table1.attributeX,attribute2> – FROM <table1> LEFT JOIN <table2> – ON table1.attributeX = table2.attributeX; • Right join syntax: – SELECT <attribute1,table2.attributeX,attribute2> – FROM <table1> RIGHT JOIN <table2> – ON table1.attributeX = table2.attributeX; Outer join cont… • Left and right refers to the tables in the query. • In the left outer join the matching fields for the left table will be included in the results i.e all displayed fields for the left table will have data whereas some fields belonging to the right table will be null. • In the right outer join all the displayed fields for the right table will have data whereas some fields in the left table will be null. Views • The output of a relational operator such as SELECT is another relation(table). • Suppose it is a routine to get a list of products to reorder at the end of everyday? It would be tedious to retype the same query daily. • Wouldn’t it be better to save the query permanently to the database? • A view is a virtual table based on a SELECT query. • Syntax: CREATE VIEW <viewname> AS <SELECT query> Characteristics of a view • It can be used anywhere in an SQL query where a table name is expected. • Views are dynamically updated - every time a view is invoked it retrieves accurate and upto date information. • Views provide some level of security in a database – it restricts the users to specified columns and specified rows in a relation. Indexes • An index is an orderly arrangement used to logically access rows in a table. It is used for faster searching. • Syntax: – CREATE INDEX <indexname> ON <tablename(columnname)> Stored procedures • A stored procedure is a named collection of procedural and SQL statements that are stored in a database. • For example, you can create a stored procedure to represent a product sale, a credit update, or the addition of a new customer. By doing that, you can encapsulate SQL statements within a single stored procedure and execute them as a single transaction. Stored procedures cont.. • Advantages of stored procedures: – Stored procedures substantially reduce network traffic and increase performance. SQL statements are stored in the server and thus individual statements are not transmitted over the network every time they are invoked. – Stored procedures help reduce code duplication by means of code isolation and code sharing Stored procedures cont.. Syntax: CREATE OR REPLACE PROCEDURE procedure_name [(argument [IN/OUT] data-type, )] [IS/AS] [variable_namedata type[:=initial_value] ] BEGIN PL/SQL or SQL statements; ... END; Stored procedure syntax cont.. • Argument specifies the parameters that are passed to the stored procedure. A stored procedure could have zero or more arguments or parameters. • IN/OUT indicates whether the parameter is for input, output, or both. • Data-type is one of the procedural SQL data types used in the RDBMS. The data types normally match those used in the RDBMS table-creation statement. • Variables can be declared between the keywords IS and BEGIN. You must specify the variable name, its data type, and (optionally) an initial value. Triggers • A trigger is procedural SQL code that is automatically invoked by the RDBMS upon the occurrence of a given data manipulation event. – A trigger is invoked before or after a data row is inserted, updated, or deleted. – A trigger is associated with a database table. – Each database table may have one or more triggers. – A trigger is executed as part of the transaction that triggered it. Triggers cont… • Triggers are critical to proper database operation and management, for example: – Triggers can be used to enforce constraints that cannot be enforced at the DBMS design and implementation levels. – Triggers add functionality by automating critical actions and providing appropriate warnings and suggestions – For remedial action. Triggers can be used to update table values, insert records in tables, and call other stored procedures. Triggers cont… • Oracle recommends triggers for: – Auditing purposes (creating audit logs). – Automatic generation of derived column values. – Enforcement of business or security constraints. – Creation of replica tables for backup purposes. Triggers cont… CREATE OR REPLACE TRIGGER trigger_name [BEFORE / AFTER] [DELETE / INSERT / UPDATE OF column_name] ON table_name [FOR EACH ROW] [DECLARE] [variable_namedata type[:=initial_value] ] BEGIN SQL instruction1; SQL instruction2; ... END; Triggers cont… A trigger definition contains the following parts: – The triggering timing: BEFORE or AFTER, this indicates when the trigger’s PL/SQL code executes; in this case, before or after the triggering statement is completed. – The triggering event: the statement that causes the trigger to execute (INSERT, UPDATE, or DELETE). – The triggering level: There are two types of triggers: statement-level triggers and row-level triggers. – The triggering action: The PL/SQL code enclosed between the BEGIN and END keywords.