Академический Документы
Профессиональный Документы
Культура Документы
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
DML Statements
SELECT - retrieve data from the a database INSERT - insert data into a table UPDATE - updates existing data within a table DELETE - deletes all records from a table, the space for the records remain MERGE - UPSERT operation (insert or update) CALL - call a PL/SQL or Java subprogram EXPLAIN PLAN - explain access path to data LOCK TABLE - control concurrency
Joins
A join works through the use of keys, the primary key and the foreign key. Our example has the supplier table that contains a supplier_id column thats configured as a primary key.
We can use the SELECT statement to extract the corresponding information from the tables.
Suppliers Table
Products Table
Cross-Joins
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. SELECT column_names FROM table1, table2 WHERE (table1.column = table2.column);
Making Equi-Joins
For example, to extract the product name and supplier name for each row in our product we say:
SELECT prod_name, supplier_name, supplier_address FROM product, suppliers WHERE (product.supplier_id = suppliers.supplier_id);
Making Equi-Joins
Notes
Note that we have to use the fully qualified name since both contain supplier_id. The fully qualified column name is defined by the following syntax:
<table_name>.column_name
SELECT prod_name, supplier_name, supplier_address FROM product LEFT JOIN suppliers USING (supplier_id) WHERE supplier_name='Microsoft';
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
Views
The command to create the view from the previous slide is: CREATE VIEW suppformat AS SELECT CONCAT(UPPER(supplier_name), ' ', supplier_address) FROM suppliers;
Views
When we want to create a table based on that SELECT statement without reinvoking the statement, we can create a view.
Creating Views
Creating Vies
Remember how to create a Join? Remember that previously weve created a join between two tables? This allowed us to display values from both the supplier and product table.
Creating Views
We can easily create a view based on the join that weve just created. To create this join, simply input the following command:
CREATE VIEW prodsupp AS SELECT prod_code, prod_name, supplier_name, supplier_address FROM product, suppliers WHERE (product.supplier_id = suppliers.supplier_id);
Creating Views
Creating Views
We can easily be able to filter our data from our data the same way we do with any table.
Sources
http://www.techotopia.com/index.php/An_Int roduction_to_MySQL_Views http://www.techotopia.com/index.php/An_Int roduction_to_MySQL_Views