CREATE VIEW <NieuweTabelnaam> AS SELECT <kolom1>, <kolom2> FROM <tabel1> INNER JOIN <tabel2> ON <tabel1.waarde1> = <tabel2.waarde2>
Het maken van een View
SELECT cust_name, cust_contact, prod_id FROM Customers INNER JOIN Orders ON Customers.cust_id = Orders.cust_id INNER JOIN OrderItems ON Orders.order_num = OrderItems.order_num;
CREATE VIEW ProductCustomers AS SELECT cust_name, cust_contact, prod_id FROM Customers INNER JOIN Orders ON Customers.cust_id = Orders.cust_id INNER JOIN OrderItems ON Orders.order_num = OrderItems.order_num;
SELECT * FROM ProductCustomers;
| cust_name | cust_contact | prod_id |
| Village Toys | John Smith | BR01 |
| Village Toys | John Smith | BR03 |
| Village Toys | John Smith | BNBG01 |
| Village Toys | John Smith | BNBG02 |
| Village Toys | John Smith | BNBG03 |
| Fun4All | Jim Jones | BR01 |
| Fun4All | Jim Jones | BR02 |
| Fun4All | Jim Jones | BR03 |
| Fun4All | Denise L. Stephens | BR03 |
| Fun4All | Denise L. Stephens | BNBG01 |
| Fun4All | Denise L. Stephens | BNBG02 |
| Fun4All | Denise L. Stephens | BNBG03 |
| Fun4All | Denise L. Stephens | RGAN01 |
| The Toy Store | Kim Howard | RGAN01 |
| The Toy Store | Kim Howard | BR03 |
| The Toy Store | Kim Howard | BNBG01 |
| The Toy Store | Kim Howard | BNBG02 |
| The Toy Store | Kim Howard | BNBG03 |
Probeer een view algemeen te houden, dan kan hij vaker gebruikt worden
-- Oracle / PostgreSQL SELECT RTRIM(vend_name) || ' (' || RTRIM(vend_country) || ')' AS vend_title FROM Vendors ORDER BY vend_name;
-- MySQL SELECT CONCAT(vend_name, ' (', vend_country, ')') AS vend_title FROM Vendors ORDER BY vend_name;
-- SQL Server SELECT RTRIM(vend_name) + ' (' + RTRIM(vend_country) + ')' AS vend_title FROM Vendors ORDER BY vend_name;
| vend_title |
| Bear Emporium (usa) |
| Bears R Us (usa) |
| Doll House Inc. (usa) |
| Fun and Games (England) |
| Furball Inc. (usa) |
| Jouets et ours (France) |
-- Oracle / PostgreSQL CREATE VIEW VendorLocations AS SELECT RTRIM(vend_name) || ' (' || RTRIM(vend_country) || ')' AS vend_title FROM Vendors;
-- MySQL CREATE VIEW VendorLocations AS SELECT CONCAT(vend_name, ' (', vend_country, ')') AS vend_title FROM Vendors;
-- SQL Server CREATE VIEW VendorLocations AS SELECT RTRIM(vend_name) + ' (' + RTRIM(vend_country) + ')' AS vend_title FROM Vendors;
SELECT * FROM VendorLocations ORDER BY vend_title;
| vend_title |
| Bear Emporium (usa) |
| Bears R Us (usa) |
| Doll House Inc. (usa) |
| Fun and Games (England) |
| Furball Inc. (usa) |
| Jouets et ours (France) |
CREATE VIEW CustomerEmailList AS SELECT cust_name AS Klantnaam, cust_email AS Email_adres FROM Customers WHERE cust_email IS NOT NULL;
SELECT * FROM CustomerEmailList;
| Klantnaam | Email_adres |
| Village Toys | sales@villagetoys.com |
| Fun4All | jjones@fun4all.com |
| Fun4All | dstephens@fun4all.com |
DROP VIEW <View-Naam>;
DROP VIEW VendorLocations;