Database Management System (SQL) 1) NOW() The NOW () function returns the current system date and time

. Syntax: SELECT NOW () FROM table_name For example: We have the following "Products" table: Prod_Id 1 2 3 ProductName Kissan Jam Oral B Toothbrush Ponds Powder Unit 1000 g 2000 g 1500 g UnitPrice 10.45 32.56 15.67

Now we want to display the products and prices per today's date. We use the following SELECT statement: SELECT ProductName, UnitPrice, Now() as PerDate FROM Products The result-set will look like this: ProductName Kissan Jam Oral B Toothbrush Ponds Powder UnitPrice 10.45 32.56 15.67 PerDate 21/4/2010 11:25:02 AM 21/4/2010 11:25:02 AM 21/4/2010 11:25:02 AM

2) FORMAT () The FORMAT () function is used to format how a field is to be displayed. Syntax: SELECT FORMAT(column_name,format) FROM table_name Parameter column_name format Description Required. The field to be formatted. Required. Specifies the format.

For Example: We have the following "Products" table: Prod_Id 1 2 3 ProductName Kissan Jam Oral B Toothbrush Ponds Powder Unit 1000 g 1000 g 1000 g UnitPrice 10.45 32.56 15.67

Now we want to display the products and prices per today's date (with today's date displayed in the following format "YYYY-MM-DD"). We use the following SELECT statement: SELECT ProductName, UnitPrice, FORMAT(Now(),'YYYY-MM-DD') as PerDate FROM Products The result-set will look like this: ProductName Kissan Jam Oral B Toothbrush Ponds Powder UnitPrice 10.45 32.56 15.67 PerDate 2010-04-21 2010-04-21 2010-04-21

3) ROUND () The ROUND () function is used to round a numeric field to the number of decimals specified. Syntax: SELECT ROUND (column_name,decimals) FROM table_name Parameter column_name decimals Description Required. The field to round. Required. Specifies the number of decimals to be returned.

For example: We have the following "Products" table: Prod_Id 1 2 3 ProductName Kissan Jam Oral B Toothbrush Ponds Powder Unit 1000 g 1000 g 1000 g UnitPrice 10.45 32.56 15.67

Now we want to display the product name and the price rounded to the nearest integer. We use the following SELECT statement: SELECT ProductName, ROUND(UnitPrice,0) as UnitPrice FROM Products The result-set will look like this: ProductName Kissan Jam Oral B Toothbrush Ponds Powder UnitPrice 10 33 16

4) Views () y y y In SQL, a view is a virtual table based on the result-set of an SQL statement. A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database. You can add SQL functions, WHERE, and JOIN statements to a view and present the data as if the data were coming from one single table.

SQL CREATE VIEW Syntax CREATE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition

SQL Updating a View You can update a view by using the following syntax: SQL CREATE OR REPLACE VIEW Syntax CREATE OR REPLACE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition

SQL Dropping a View You can delete a view with the DROP VIEW command. SQL DROP VIEW Syntax DROP VIEW view_name

5) Outer join An outer join does not require each record in the two joined tables to have a matching record. The joined table retains each record even if no other matching record exists. Outer joins subdivide further into left outer joins, right outer joins, and full outer joins, depending on which table(s) one retains the rows from (left, right, or both). Left Outer Join Use this when you only want to return rows that have matching data in the left table, even if there's no matching rows in the right table. Example SQL statement SELECT * FROM Individual AS Ind LEFT JOIN Publisher AS Pub ON Ind.IndividualId = Pub.IndividualId

Source Tables Left Table Id 1 2 3 4 5 FirstName Fred Homer Homer Ozzy Homer LastName Flinstone Simpson Brown Ozzbourne Gain UserName freddo homey notsofamous sabbath noplacelike

Right Table IndividualId 1 2 3 4 10 Result IndividualId 1 2 3 4 5 FirstName Fred Homer Homer Ozzy Homer LastName Flinstone Simpson Brown Osbourne Gain UserName freddo homey notsofamous sabbath noplacelike IndividualId 1 2 3 4 NULL AccessLevel Administrator Contributor Contributor Contributor NULL AccessLevel Administrator Contributor Contributor Contributor Administrator

Right Outer Join Use this when you only want to return rows that have matching data in the right table, even if there's no matching rows in the left table. Example SQL statement SELECT * FROM Individual AS Ind RIGHT JOIN Publisher AS Pub ON Ind.IndividualId = Pub.IndividualId

Source Tables Left Table Id 1 2 3 4 5 FirstName Fred Homer Homer Ozzy Homer LastName Flinstone Simpson Brown Ozzbourne Gain UserName freddo homey notsofamous sabbath noplacelike

Right Table IndividualId 1 2 3 4 10 Result IndividualId 1 2 3 4 NULL FirstName Fred Homer Homer Ozzy NULL LastName Flinstone Simpson Brown Osbourne NULL UserName freddo homey notsofamous sabbath NULL IndividualId 1 2 3 4 10 AccessLevel Administrator Contributor Contributor Contributor Administrator AccessLevel Administrator Contributor Contributor Contributor Administrator

Full Outer Join Use this when you want to all rows, even if there's no matching rows in the right table. Example SQL statement SELECT * FROM Individual AS Ind FULL JOIN Publisher AS Pub ON Ind.IndividualId = Pub.IndividualId

Source Tables Left Table Id 1 2 3 4 5 FirstName Fred Homer Homer Ozzy Homer LastName Flinstone Simpson Brown Ozzbourne Gain UserName freddo homey notsofamous sabbath noplacelike

Right Table IndividualId 1 2 3 4 10 AccessLevel Administrator Contributor Contributor Contributor Administrator

Result IndividualId 1 2 3 4 5 NULL FirstName Fred Homer Homer Ozzy Homer NULL LastName Flinstone Simpson Brown Osbourne Gain NULL UserName freddo homey notsofamous sabbath noplacelike NULL IndividualId 1 2 3 4 NULL 10 AccessLevel Administrator Contributor Contributor Contributor NULL Administrator

Sign up to vote on this title
UsefulNot useful