Lesson VIII: DBMS

Joins and Views

DDL and DML Languages
• Data Definition Language deals with database schemas and descriptions of how data should be structured in the database, • Data Modification Language deals with data manipulation.

DDL Statements
• • • • CREATE - to create objects in the database ALTER - alters the structure of the database DROP - delete objects from the database TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed • COMMENT - add comments to the data dictionary • RENAME - rename an object

control concurrency . the space for the records remain MERGE .deletes all records from a table.DML Statements • • • • • • • • SELECT .explain access path to data LOCK TABLE .call a PL/SQL or Java subprogram EXPLAIN PLAN .insert data into a table UPDATE .updates existing data within a table DELETE .UPSERT operation (insert or update) CALL .retrieve data from the a database INSERT .

the primary key and the foreign key. . • We can use the SELECT statement to extract the corresponding information from the tables.Joins • A join works through the use of keys. • Our example has the supplier table that contains a supplier_id column that’s configured as a primary key.

Suppliers Table .

Products Table .

It’s not really used for real life situations. table2. • The cross-join simply links one row from one table to every row of the second table. . • SELECT column_names FROM table1.Creating Cross joins • We join two tables by combining rows. The most basic type of join is what we call a crossjoin.

Cross-Joins .

. table2 WHERE (table1.column = table2.column). • SELECT column_names FROM table1.Making Equi-Joins • Equi-Joins (Inner Joins) join two rows from two or more tables through a specific comparison of two columns from their corresponding tables.

to extract the product name and supplier name for each row in our product we say: • SELECT prod_name. . suppliers WHERE (product.supplier_id = suppliers. supplier_name. supplier_address FROM product.supplier_id).Making Equi-Joins • For example.

Making Equi-Joins .

• The fully qualified column name is defined by the following syntax: – <table_name>.column_name .Notes • Note that we have to use the fully qualified name since both contain supplier_id.

• SELECT column names FROM table1 LEFT JOIN table2 ON (table1.Creating a Left or Right Join • You can also join tables using the LEFT JOIN or the RIGHT JOIN statements.column).column = table2. . Using the LEFT JOIN joins the tables before the WHERE clause is used.

Creating a Left Join .

• An example would be. if a product from our first table is mapped to a non existent supplier. . when we run SELECT the row will still be displayed except the a NULL value will be placed.Creating a Left Join • The difference with LEFT JOIN is it will list first table rows that do not have matches on the second table.

.Creating Right Joins • Creating a RIGHT JOIN is the same with creating a LEFT JOIN except that a right join displays all the tables regardless of whether they have a matching column value or not.

Creating Right Joins .

Creating Joins using WHERE and USING • We can use the WHERE clause when we want to narrow down our results such as searching only for products created by Microsoft. .

Creating Joins using WHERE and USING .

supplier_id we say: SELECT prod_name.Creating Joins using WHERE and USING • Utilizing the USING clause prevents us from using fully qualified names that simplifies the usage of JOIN. . supplier_name.supplier_id and supplier. • So to perform the same commands from the previous slides using the values of product. supplier_address FROM product LEFT JOIN suppliers USING (supplier_id) WHERE supplier_name='Microsoft'.

Creating Joins using WHERE and USING .

Views • MySQL views are basically a way to group a set of SELECT statements into re-useable tables that can easily be referenced without re-invoking the view. • Usually JOINS are used in conjunction with the CREATE VIEW command. .

Views • To create a view in MySQL simply use the CREATE VIEW statement where the syntax is: CREATE VIEW tablename AS select statement • A basic view is composed of data from a single table. We might have a normal SELECT statement that we can use to format data in a certain way. .

Views .

. ' '. supplier_address) FROM suppliers.Views The command to create the view from the previous slide is: CREATE VIEW suppformat AS SELECT CONCAT(UPPER(supplier_name).

we can create a view. .Views When we want to create a table based on that SELECT statement without reinvoking the statement.

Creating Views .

Creating Vies • Remember how to create a Join? Remember that previously we’ve created a join between two tables? • This allowed us to display values from both the supplier and product table. .

supplier_address FROM product.supplier_id). prod_name. simply input the following command: CREATE VIEW prodsupp AS SELECT prod_code. suppliers WHERE (product. .Creating Views • We can easily create a view based on the join that we’ve just created. To create this join. supplier_name.supplier_id = suppliers.

Creating Views .

.Creating Views We can easily be able to filter our data from our data the same way we do with any table.

php/An_Int roduction_to_MySQL_Views .Sources • http://www.php/An_Int roduction_to_MySQL_Views • http://www.techotopia.com/index.com/index.techotopia.