Commit 0bf4dfa2 authored by michael lundquist's avatar michael lundquist
Browse files

Adding homeworks from weeks 3 and 4.

parent baa103fa
......@@ -10,6 +10,11 @@ constraints (PK, NOT NULL, CHECK, UNIQUE). Use the DESCRIBE command
to verify that the columns have been defined correctly.
*/
CREATE TABLE MICHAEL_LUNDQUIST (ID integer, firstName VARCHAR2(20) NOT NULL, lastName VARCHAR2(20) NOT NULL, addDay DATE DEFAULT SYSDATE, zip CHAR(5) NOT NULL, CONSTRAINT MICHAEL_LUNDQUIST_ID_pk PRIMARY KEY (ID));
DESC MICHAEL_LUNDQUIST;
/*
problem2
......@@ -22,6 +27,8 @@ command to display the content of the table
table).
*/
INSERT INTO MICHAEL_LUNDQUIST values (1, 'Michael', 'Lundquist', DEFAULT, '20194');
/*
problem3
......@@ -33,6 +40,8 @@ command to display the content of the table
(make sure to substitute your_table_name with the actual name of your table).
*/
INSERT INTO MICHAEL_LUNDQUIST (ID, firstName, lastName, addDay, zip) values (0, 'Michael', 'Lundquist', DEFAULT, '20194');
/*
problem4
......@@ -44,6 +53,8 @@ command to display the content of the table
table).
*/
UPDATE MICHAEL_LUNDQUIST SET LASTNAME = 'coolGuy' where LASTNAME = 'Lundquist';
/*
problem5
......@@ -55,6 +66,8 @@ command to display the content of the table
table).
*/
DELETE FROM MICHAEL_LUNDQUIST WHERE ID = 0;
/*
problem6
......@@ -64,6 +77,8 @@ content of the table (make sure to substitute your_table_name with the
actual name of your table).
*/
COMMIT;
/*
problem7
......@@ -75,7 +90,17 @@ command to display the content of the table
table).
*/
/*
DDL commands can't be rolled back
uncommitted DML commands are deleted
we just committted, so nothing
*/
ROLLBACK;
/*
problem8
Use the DROP TABLE command to drop the table created in Problem 1.
*/
\ No newline at end of file
*/
DROP TABLE MICHAEL_LUNDQUIST;
\ No newline at end of file
# ch 5 DML
## problem 1
Create a new table using the CREATE TABLE command. Use your first name
and your last name for the name of the table
(for instance, for John Smith the name of the table will be JOHN_SMITH).
Make sure to include at least four different data types
(CHAR, VARCHAR2, NUMBER, DATE) for the columns and appropriate
constraints (PK, NOT NULL, CHECK, UNIQUE). Use the DESCRIBE command
to verify that the columns have been defined correctly.
### problem 1 commands
```sql
CREATE TABLE MICHAEL_LUNDQUIST (ID integer, firstName VARCHAR2(20) NOT NULL, lastName VARCHAR2(20) NOT NULL, addDay DATE DEFAULT SYSDATE, zip CHAR(5) NOT NULL, CONSTRAINT MICHAEL_LUNDQUIST_ID_pk PRIMARY KEY (ID));
DESC MICHAEL_LUNDQUIST;
```
![problem 1](./PICS/PROBLEM_1.PNG)
## problem 2
Use the INSERT command to insert a new record into the table created in
Problem 1. When inserting a record, provide values for all columns.
Use the default format for the date. Use the
SELECT * FROM your_table_name;
command to display the content of the table
(make sure to substitute your_table_name with the actual name of your
table).
### problem 2 commands
```sql
INSERT INTO MICHAEL_LUNDQUIST values (1, 'Michael', 'Lundquist', DEFAULT, '20194');
```
![problem 2](./PICS/PROBLEM_2.PNG)
## problem 3
Use the INSERT command to insert a new record into the table created in
Problem 1. When inserting a record, provide values for selected columns
only. Use the default format for the date. Use the
SELECT * FROM your_table_name;
command to display the content of the table
(make sure to substitute your_table_name with the actual name of your table).
### problem 3 commands
```sql
INSERT INTO MICHAEL_LUNDQUIST (ID, firstName, lastName, addDay, zip) values (0, 'Michael', 'Lundquist', DEFAULT, '20194');
```
![problem 4](./PICS/PROBLEM_3.PNG)
## problem 4
Use the UPDATE command to update multiple values in the table created in
Problem 1. Use the SELECT * FROM your_table_name;
command to display the content of the table
(make sure to substitute your_table_name with the actual name of your
table).
### problem 4 commands
```sql
UPDATE MICHAEL_LUNDQUIST SET LASTNAME = 'coolGuy' where LASTNAME = 'Lundquist';
```
![problem 4](./PICS/PROBLEM_4.PNG)
## problem 5
Use the DELETE command to delete one specific row from the table created
in Problem 1. Use the
SELECT * FROM your_table_name;
command to display the content of the table
(make sure to substitute your_table_name with the actual name of your
table).
### problem 5 commands
```sql
DELETE FROM MICHAEL_LUNDQUIST WHERE ID = 0;
```
![problem 5](./PICS/PROBLEM_5.PNG)
## problem 6
Use the COMMIT statement to save changes in the table created in
Problem 1. Use the SELECT * FROM your_table_name; command to display the
content of the table (make sure to substitute your_table_name with the
actual name of your table).
### problem 6 commands
```sql
COMMIT;
```
![problem 6](./PICS/PROBLEM_6.PNG)
## problem 7
Use the ROLLBACK command to undo changes in the table created in Problem 1.
What happened? Why? Use the
SELECT * FROM your_table_name;
command to display the content of the table
(make sure to substitute your_table_name with the actual name of your
table).
### problem 7 commands
```sql
ROLLBACK;
```
![problem 7](./PICS/PROBLEM_7.PNG)
### Explanation
The `ROLLBACK` command did nothing. It didn't delete the table itself because DDL commands can't be rolled back, only DML commands. It didn't remove any of the inserted values because they were already committed.
## problem 8
Use the DROP TABLE command to drop the table created in Problem 1.
### problem 8 commands
```sql
DROP TABLE MICHAEL_LUNDQUIST;
```
![problem 8](./PICS/PROBLEM_8.PNG)
\ No newline at end of file
......@@ -6,6 +6,9 @@ JustLee Books database. In a search condition, include one or more
arithmetic comparison operators (=, !=, >, <, >=, etc.).
*/
select table_name from user_tables;
SELECT * FROM PROMOTION WHERE MINRETAIL > 20;
/*
problem2
......@@ -15,6 +18,8 @@ JustLee Books database. In a search condition, include the BETWEEN … AND
operator.
*/
SELECT * FROM PROMOTION WHERE MINRETAIL BETWEEN 20 AND 30;
/*
problem3
......@@ -22,6 +27,8 @@ Write an SQL query to retrieve records from one of the tables in the
JustLee Books database. In a search condition, include the IN operator.
*/
SELECT * FROM PROMOTION WHERE GIFT IN ('BOOK COVER', 'BOOKMARKER');
/*
problem4
......@@ -30,6 +37,8 @@ JustLee Books database. In a search condition, include the LIKE operator
with either % or _ (or both).
*/
SELECT * FROM PROMOTION WHERE GIFT LIKE '%OO%';
/*
problem5
......@@ -38,6 +47,8 @@ JustLee Books database. In a search condition, include the IS NULL
operator.
*/
SELECT * FROM CUSTOMERS WHERE EMAIL IS NULL;
/*
problem6
......@@ -46,6 +57,8 @@ JustLee Books database. In a search condition, include multiple conditions
using logical operators (AND and OR).
*/
SELECT * FROM CUSTOMERS WHERE EMAIL IS NULL AND REGION = 'W';
/*
problem7
......@@ -54,4 +67,6 @@ in the JustLee Books database. In a search condition, use logical
operators to join multiple conditions that include at least one of the
arithmetic operators and one of the special operators. Use the
ORDER BY statement to order the output.
*/
\ No newline at end of file
*/
SELECT * FROM BOOKS WHERE COST BETWEEN 10 AND 30 AND PUBID = 4 ORDER BY COST;
# ch8, complex conditions
## problem1
Write an SQL query to retrieve records from one of the tables in the
JustLee Books database. In a search condition, include one or more
arithmetic comparison operators (=, !=, >, <, >=, etc.).
```sql
SELECT * FROM PROMOTION WHERE MINRETAIL > 20;
```
![problem 1 screenshot](./PICS/PROBLEM1.PNG)
## problem2
Write an SQL query to retrieve records from one of the tables in the
JustLee Books database. In a search condition, include the BETWEEN … AND
operator.
```sql
SELECT * FROM PROMOTION WHERE MINRETAIL BETWEEN 20 AND 30;
```
![problem2 screenshot](./PICS/PROBLEM2.PNG)
## problem3
Write an SQL query to retrieve records from one of the tables in the
JustLee Books database. In a search condition, include the IN operator.
```SQL
SELECT * FROM PROMOTION WHERE GIFT IN ('BOOK COVER', 'BOOKMARKER');
```
![PROBLEM3 SCREENSHOT](./PICS/PROBLEM3.PNG)
## problem4
Write an SQL query to retrieve records from one of the tables in the
JustLee Books database. In a search condition, include the LIKE operator
with either % or _ (or both).
```SQL
SELECT * FROM PROMOTION WHERE GIFT LIKE '%OO%';
```
![PROBLEM4 SCREENSHOT](./PICS/PROBLEM4.PNG)
## problem5
Write an SQL query to retrieve records from one of the tables in the
JustLee Books database. In a search condition, include the IS NULL
operator.
```SQL
SELECT * FROM CUSTOMERS WHERE EMAIL IS NULL;
```
![PROBLEM5 SCREENSHOT](./PICS/PROBLEM5.PNG)
## problem6
Write an SQL query to retrieve records from one of the tables in the
JustLee Books database. In a search condition, include multiple conditions
using logical operators (AND and OR).
```SQL
SELECT * FROM CUSTOMERS WHERE EMAIL IS NULL AND REGION = 'W';
```
![PROBLEM6 SCREENSHOT](./PICS/PROBLEM6.PNG)
## problem7
Write a complex SQL query to retrieve records from one of the tables
in the JustLee Books database. In a search condition, use logical
operators to join multiple conditions that include at least one of the
arithmetic operators and one of the special operators. Use the
ORDER BY statement to order the output.
```SQL
SELECT * FROM BOOKS WHERE COST BETWEEN 10 AND 30 AND PUBID = 4 ORDER BY COST;
```
![PROBLEM7 SCREENSHOT](./PICS/PROBLEM7.PNG)
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment