0% found this document useful (0 votes)
11 views4 pages

Lab2 4q

lab query
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Lab2 4q

lab query
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

mysql> create database suppliers1;

Query OK, 1 row affected (0.07 sec)

mysql> use suppliers1;


Database changed
mysql> CREATE TABLE Suppliers ( sid INTEGER PRIMARY KEY, sname VARCHAR(100)
NOT NULL, address VARCHAR(200));
Query OK, 0 rows affected (0.10 sec)

mysql> INSERT INTO Suppliers (sid, sname, address) VALUES(1, 'Acme Widget
Suppliers', '123 Acme St'),(2, 'Best Parts Inc.', '456 Best Ave'),(3, 'Quality
Supplies', '789 Quality Rd'),(4, 'Universal Supplies', '101 Universal Blvd');
Query OK, 4 rows affected (0.02 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> select *from suppliers;


+-----+-----------------------+--------------------+
| sid | sname | address |
+-----+-----------------------+--------------------+
| 1 | Acme Widget Suppliers | 123 Acme St |
| 2 | Best Parts Inc. | 456 Best Ave |
| 3 | Quality Supplies | 789 Quality Rd |
| 4 | Universal Supplies | 101 Universal Blvd |
+-----+-----------------------+--------------------+
4 rows in set (0.00 sec)

mysql> CREATE TABLE Parts ( pid INTEGER PRIMARY KEY, pname VARCHAR(100) NOT
NULL, color VARCHAR(50));
Query OK, 0 rows affected (0.09 sec)

mysql> INSERT INTO Parts (pid, pname, color) VALUES(1, 'Widget', 'red'),(2,
'Gadget', 'blue'),(3, 'Doodad', 'green'),(4, 'Thingamajig', 'yellow'),(5,
'Contraption', 'red'),(6, 'Device', 'green');
Query OK, 6 rows affected (0.07 sec)
Records: 6 Duplicates: 0 Warnings: 0

mysql> select *from parts;


+-----+-------------+--------+
| pid | pname | color |
+-----+-------------+--------+
| 1 | Widget | red |
| 2 | Gadget | blue |
| 3 | Doodad | green |
| 4 | Thingamajig | yellow |
| 5 | Contraption | red |
| 6 | Device | green |
+-----+-------------+--------+
6 rows in set (0.00 sec)

mysql> CREATE TABLE Catalog ( sid INTEGER, pid INTEGER, cost REAL,
PRIMARY KEY (sid, pid), FOREIGN KEY (sid) REFERENCES Suppliers(sid) ON DELETE NO
ACTION, FOREIGN KEY (pid) REFERENCES Parts(pid) ON DELETE NO ACTION);
Query OK, 0 rows affected (0.13 sec)

mysql> INSERT INTO Catalog VALUES(1, 1, 10.00),(1, 5, 12.00),(2, 3, 20.00),(2, 6,


25.00),(3, 2, 15.00),(4, 1, 11.00),(4, 3, 22.00),(4, 4, 30.00);
Query OK, 8 rows affected (0.07 sec)
Records: 8 Duplicates: 0 Warnings: 0
mysql> selct *from catalog;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'selct
*from catalog' at line 1
mysql> select *from catalog;
+-----+-----+------+
| sid | pid | cost |
+-----+-----+------+
| 1 | 1 | 10 |
| 1 | 5 | 12 |
| 2 | 3 | 20 |
| 2 | 6 | 25 |
| 3 | 2 | 15 |
| 4 | 1 | 11 |
| 4 | 3 | 22 |
| 4 | 4 | 30 |
+-----+-----+------+
8 rows in set (0.00 sec)

mysql> SELECT s.sid


-> FROM Suppliers s
-> JOIN Catalog c ON s.sid = c.sid
-> JOIN Parts p ON c.pid = p.pid
-> WHERE p.color = 'red'
-> AND s.sid NOT IN (
-> SELECT s.sid
-> FROM Suppliers s
-> JOIN Catalog c ON s.sid = c.sid
-> JOIN Parts p ON c.pid = p.pid
-> WHERE p.color <> 'red'
-> );
+-----+
| sid |
+-----+
| 1 |
| 1 |
+-----+
2 rows in set (0.01 sec)

mysql> SELECT DISTINCT s1.sid


-> FROM Suppliers s1
-> JOIN Catalog c1 ON s1.sid = c1.sid
-> JOIN Parts p1 ON c1.pid = p1.pid
-> WHERE p1.color = 'red'
-> AND EXISTS (
-> SELECT 1
-> FROM Suppliers s2
-> JOIN Catalog c2 ON s2.sid = c2.sid
-> JOIN Parts p2 ON c2.pid = p2.pid
-> WHERE s2.sid = s1.sid AND p2.color = 'green'
-> );
+-----+
| sid |
+-----+
| 4 |
+-----+
1 row in set (0.00 sec)
mysql> SELECT DISTINCT s.sid
-> FROM Suppliers s
-> JOIN Catalog c ON s.sid = c.sid
-> JOIN Parts p ON c.pid = p.pid
-> WHERE p.color = 'red' OR p.color = 'green';
+-----+
| sid |
+-----+
| 1 |
| 4 |
| 2 |
+-----+
3 rows in set (0.00 sec)

mysql> SELECT s.sname, COUNT(p.pid) AS total_parts


-> FROM Suppliers s
-> JOIN Catalog c ON s.sid = c.sid
-> JOIN Parts p ON c.pid = p.pid
-> WHERE p.color = 'green'
-> AND s.sid NOT IN (
-> SELECT s.sid
-> FROM Suppliers s
-> JOIN Catalog c ON s.sid = c.sid
-> JOIN Parts p ON c.pid = p.pid
-> WHERE p.color <> 'green'
-> )
-> GROUP BY s.sname;
+-----------------+-------------+
| sname | total_parts |
+-----------------+-------------+
| Best Parts Inc. | 2 |
+-----------------+-------------+
1 row in set (0.00 sec)

mysql> SELECT s.sname, MAX(c.cost) AS max_cost


-> FROM Suppliers s
-> JOIN Catalog c ON s.sid = c.sid
-> JOIN Parts p ON c.pid = p.pid
-> WHERE s.sid IN (
-> SELECT DISTINCT s1.sid
-> FROM Suppliers s1
-> JOIN Catalog c1 ON s1.sid = c1.sid
-> JOIN Parts p1 ON c1.pid = p1.pid
-> WHERE p1.color = 'red'
-> AND EXISTS (
-> SELECT 1
-> FROM Suppliers s2
-> JOIN Catalog c2 ON s2.sid = c2.sid
-> JOIN Parts p2 ON c2.pid = p2.pid
-> WHERE s2.sid = s1.sid AND p2.color = 'green'
-> )
-> )
-> GROUP BY s.sname;
+--------------------+----------+
| sname | max_cost |
+--------------------+----------+
| Universal Supplies | 30 |
+--------------------+----------+
1 row in set (0.06 sec)
mysql> CREATE VIEW SupplierMaxCost AS
-> SELECT s.sname, MAX(c.cost) AS max_cost
-> FROM Suppliers s
-> JOIN Catalog c ON s.sid = c.sid
-> JOIN Parts p ON c.pid = p.pid
-> GROUP BY s.sname;
Query OK, 0 rows affected (0.07 sec)

mysql> SELECT * FROM SupplierMaxCost;


+-----------------------+----------+
| sname | max_cost |
+-----------------------+----------+
| Acme Widget Suppliers | 12 |
| Best Parts Inc. | 25 |
| Quality Supplies | 15 |
| Universal Supplies | 30 |
+-----------------------+----------+
4 rows in set (0.00 sec)

mysql>

You might also like