Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

SQL Homework: exercise 2: CREATE TABLE users ( user_id int Primary key NOT NULL,

ID: 3713185 • Letter: S

Question

SQL Homework:

exercise 2:

CREATE TABLE users
(
user_id int Primary key NOT NULL,
email_address VARCHAR(20),
first_name VARCHAR(20),
last_name VARCHAR(20)
);
CREATE TABLE downloads
(
download_id int Primary key NOT NULL,
user_id INT,
download_date DATE,
last_name VARCHAR(20),
product_id INT,
CONSTRAINT fk_column
FOREIGN KEY(user_id)
REFERENCES users(user_id),
FOREIGN KEY(product_id)
REFERENCES product(product_id)
);
CREATE TABLE product
(
product_id int Primary key NOT NULL,
product_name VARCHAR(20)
);

3.

Write a script that adds rows to the database that you created in exercise 2.

Add two rows to the Users and Products tables.

Add three rows to the Downloads table: one row for user 1 and product 2; one row for user 2 and product 1; and one row for user 2 and product 2. Use the SYSDATE function to insert the current date into the download_date column.

Use the sequences created in the previous exercise to get the values for the user_id, download_id, and product_id columns.

Write a SELECT statement that joins the three tables and retrieves the data from these tables like this:

Sort the results by the email address in descending sequence and the product name in ascending sequence.

4.

Write an ALTER TABLE statement that adds two new columns to the Products table created in exercise 2.

Add one column for product price that provides for three digits to the left of the decimal point and two to the right. This column should have a default value of 9.99.

Add one column for the date and time that the product was added to the database.

5.

Write an ALTER TABLE statement that modifies the Users table created in exercise 2 so the first_name column can store NULL values and can store a maximum of 20 characters.

Code an UPDATE statement that inserts a NULL value into this column. It should work since this column now allows NULL values.

Code another UPDATE statement that attempts to insert a first name that’s longer than 20 characters. It should fail due to the length of the column.

EMAIL ADDRESS |0 johnsmith@gmail.com John janedoe@yahoo.com Jane janedoe@yahoo.com Jane LAST NAME |?DOWNLOAD-DATE:0 FILENAME Smith Doe Doe FIRST NAME PRODUCT NAME 17-JUN-14 17-JUN-14 17-JUN-14 pedals_are falling.mp3 Local Music Vol 1 turn_signal.mp3 one_horse_town.mp3 Local Music Vol 1 Local Music Vol 2

Explanation / Answer

Solution:

Note: The first question is done as per Chegg guidelines(because it has 4 subparts), please repost others.

3)

Add two rows to the Users and Products tables.

The INSERT command for SQL to insert data in an already created table is given below:

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

The query here will be

INSERT INTO Users(user_id , email_address, first_name, last_name )
VALUES ('123', 'peterparker@email.co', 'Parker', 'Peter'),

('568', 'jarvis@email.co', 'Stark', 'Tony');

INSERT INTO product(product_id , product_name )
VALUES ('123', 'product1'),

('568', 'product2');

Add three rows to the Downloads table: one row for user 1 and product 2; one row for user 2 and product 1; and one row for user 2 and product 2. Use the SYSDATE function to insert the current date into the download_date column.

INSERT INTO Users(user_id , download_date, last_name, product_id)
VALUES ('123', 'SYSDATE()', 'Parker', 'product2'),

('568', 'SYSDATE()', 'Stark', 'product1'),

('568', 'SYSDATE()', 'Stark','product2');

Write a SELECT statement that joins the three tables and retrieves the data from these tables like this:

Sort the results by the email address in descending sequence and the product name in ascending sequence.

SELECT EMAIL_ADDRESS, FIRST_NAME, LAST_NAME, DOWNLOAD_DATE, FILENAME, PRODUCT_NAME FROM USERS INNER JOIN DOWNLOADS ON USERS.user_id= DOWNLOADS.user_id INNER JOIN PRODUCTS ON DOWNLOADS.product_id = PRODUCTS.product_id ORDER BY EMAIL_ADDRESSDESC

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)