4. Create the Order_Product table as follows: • id is the primary key and its data type is serial quantity is an integer which doesn’t allow null and has a default value of O order_id and product_id are foreign keys and will be set to null on delete.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Need help writing SQL queries for sample data base 

 

### Database Table Creation and Calculation Guide

#### Instructions for Creating the `Order_Product` Table

1. **Primary Key:**
   - **Field:** `id`
   - **Type:** `serial`
   - **Description:** Acts as the primary key.

2. **Quantity Attribute:**
   - **Field:** `quantity`
   - **Type:** `integer`
   - **Constraints:** Cannot be null. 
   - **Default Value:** 0

3. **Foreign Keys:**
   - **Fields:** `order_id` and `product_id`
   - **Behavior on Delete:** These will be set to null when the associated record is deleted.

#### Calculating Average Price

To calculate the average price of all products, sum the prices of the individual products and divide by the number of products.

**Example:**
- If the database contains three products with prices 7, 8, and 9, the average price would be (7 + 8 + 9) / 3 = 8.
Transcribed Image Text:### Database Table Creation and Calculation Guide #### Instructions for Creating the `Order_Product` Table 1. **Primary Key:** - **Field:** `id` - **Type:** `serial` - **Description:** Acts as the primary key. 2. **Quantity Attribute:** - **Field:** `quantity` - **Type:** `integer` - **Constraints:** Cannot be null. - **Default Value:** 0 3. **Foreign Keys:** - **Fields:** `order_id` and `product_id` - **Behavior on Delete:** These will be set to null when the associated record is deleted. #### Calculating Average Price To calculate the average price of all products, sum the prices of the individual products and divide by the number of products. **Example:** - If the database contains three products with prices 7, 8, and 9, the average price would be (7 + 8 + 9) / 3 = 8.
**Write SQL queries for the following database:**

The diagram illustrates an entity-relationship model for a database consisting of five tables: Customers, Orders, Order_Product, Categories, and Products. Each table contains specific columns relevant to the data it represents.

1. **Customers Table**
   - Columns: 
     - `id`: Unique identifier for each customer.
     - `name`: Name of the customer.
     - `email`: Email address of the customer.
   - Relationship: Linked to the Orders table via `id`.

2. **Orders Table**
   - Columns:
     - `id`: Unique identifier for each order.
     - `customer_id`: Foreign key linking to the Customers table.
   - Relationship: Connected to Customers through `customer_id` and to Order_Product via `id`.

3. **Order_Product Table**
   - Columns:
     - `id`: Unique identifier for each order-product entry.
     - `order_id`: Foreign key referring to the Orders table.
     - `product_id`: Foreign key referring to the Products table.
     - `quantity`: Quantity of the product in the specific order.
   - Relationships: Links Orders with Products.

4. **Categories Table**
   - Columns:
     - `id`: Unique identifier for each category.
     - `name`: Name of the product category.
   - Relationship: Connected to Products via `id`.

5. **Products Table**
   - Columns:
     - `id`: Unique identifier for each product.
     - `category_id`: Foreign key linking to the Categories table.
     - `name`: Name of the product.
     - `price`: Price of the product.
   - Relationship: Linked to Categories through `category_id` and to Order_Product through `id`. 

This database schema allows for comprehensive data handling for orders, customer management, product inventory, and category organization through structured relationships.
Transcribed Image Text:**Write SQL queries for the following database:** The diagram illustrates an entity-relationship model for a database consisting of five tables: Customers, Orders, Order_Product, Categories, and Products. Each table contains specific columns relevant to the data it represents. 1. **Customers Table** - Columns: - `id`: Unique identifier for each customer. - `name`: Name of the customer. - `email`: Email address of the customer. - Relationship: Linked to the Orders table via `id`. 2. **Orders Table** - Columns: - `id`: Unique identifier for each order. - `customer_id`: Foreign key linking to the Customers table. - Relationship: Connected to Customers through `customer_id` and to Order_Product via `id`. 3. **Order_Product Table** - Columns: - `id`: Unique identifier for each order-product entry. - `order_id`: Foreign key referring to the Orders table. - `product_id`: Foreign key referring to the Products table. - `quantity`: Quantity of the product in the specific order. - Relationships: Links Orders with Products. 4. **Categories Table** - Columns: - `id`: Unique identifier for each category. - `name`: Name of the product category. - Relationship: Connected to Products via `id`. 5. **Products Table** - Columns: - `id`: Unique identifier for each product. - `category_id`: Foreign key linking to the Categories table. - `name`: Name of the product. - `price`: Price of the product. - Relationship: Linked to Categories through `category_id` and to Order_Product through `id`. This database schema allows for comprehensive data handling for orders, customer management, product inventory, and category organization through structured relationships.
Expert Solution
Step 1

4.

Query:

CREATE TABLE Order_Product(
    ID serial NOT NULL PRIMARY KEY,
    quantity int NOT NULL DEFAULT 0,
    order_id int,
    product_id int,
    CONSTRAINT fk_orders
    FOREIGN KEY(order_id)
    REFERENCES Orders(order_id)
    ON DELETE SET NULL,
    CONSTRAINT fk_products
    FOREIGN KEY(product_id)
    REFERENCES Products(product_id)
    ON DELETE SET NULL
);
        

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY