Question

In: Computer Science

Based on the tables below, write SQL command to perform the following tasks for MySql: Create...

  1. Based on the tables below, write SQL command to perform the following tasks for MySql:
  1. Create SALESREP and CUSTOMER tables
  2. Create primary and foreign keys as appropriate. The custNo should use a surrogate key as the primary key with auto-increment
  3. increase the balance of the Gonzales account by $100 to a total of $450?
  4. Find an average customer balance
  5. Display the name of the sales representative and the name of the customer for each customer that has a balance greater than 400

SALESREP

SalesRepNo

RepName

HireDate

654

Jones

01/02/2005

734

Smith

02/03/2007

345

Chen

01/25/2004

434

Johnson

11/23/2004

CUSTOMER

CustNo

CustName

Balance

SalesRepNo

9870

Winston

500

345

8590

Gonzales

350

434

7840

Harris

800

654

4870

Miles

100

345

Solutions

Expert Solution

a. Create SALESREP and CUSTOMER tables

Create SalesRep table

CREATE TABLE SALESREP (
SalesRepNo int NOT NULL,
RepName varchar(20),
HireDate DATE
);

Create Customer table

CREATE TABLE CUSTOMER (
CustNo int NOT NULL,
CustName varchar(20),
Balance int,
SalesRepNo int
);

b. Create primary and foreign keys as appropriate. The custNo should use a surrogate key as the primary key with auto-increment

ALTER TABLE SALESREP
ADD PRIMARY KEY (SalesRepNo);

ALTER TABLE CUSTOMER
change CustNo CustNo int AUTO_INCREMENT PRIMARY KEY;

ALTER TABLE CUSTOMER
ADD FOREIGN KEY (SalesRepNo) REFERENCES SALESREP(SalesRepNo);

c. Increase the balance of the Gonzales account by $100 to a total of $450

UPDATE CUSTOMER
SET Balance = 400
WHERE CustName = 'Gonzales';

d. Find an average customer balance

SELECT AVG(Balance)
FROM CUSTOMER;

e. Display the name of the sales representative and the name of the customer for each customer that has a balance greater than 400

SELECT a.CustName, b.RepName FROM 
CUSTOMER a join SALESREP b
on a.SalesRepNo = b.SalesRepNo
WHERE a.Balance > 400;

Related Solutions

Create three MySQL database tables and write SQL scripts to read, insert, and delete data. The...
Create three MySQL database tables and write SQL scripts to read, insert, and delete data. The first database table will contain the names of at least four movies. The second table will be a list of actors who appear in the movies. The third table will be an associative table that describes the relationship between the actors and their movies (which actors appear in which movies). Actors and movies have a “many-to-many relationship,” meaning an actor can be in multiple...
Create three MySQL database tables and write SQL scripts to read, insert, and delete data. The...
Create three MySQL database tables and write SQL scripts to read, insert, and delete data. The first database table will contain the names of at least four movies. The second table will be a list of actors who appear in the movies. The third table will be an associative table that describes the relationship between the actors and their movies (which actors appear in which movies). Actors and movies have a “many-to-many relationship,” meaning an actor can be in multiple...
Write the SQL DDL to create the following 5 tables for an App store: Publisher, Category,...
Write the SQL DDL to create the following 5 tables for an App store: Publisher, Category, App, AppVersion, AppVersionReview: A Publisher table where each publisher is identified by an integer id and has a name (up to 40 characters). (1 mark) A Category table where each category has an id (integer), a name (up to 50 characters), and a parentId to identify its parent category. The parentId should be a foreign key to the Category table. (1.5 marks) An App...
1. write SQL statements to create the following two tables: a) Department table that contains the...
1. write SQL statements to create the following two tables: a) Department table that contains the following columns(dept_no,deptname,location) set the deptno to be the primary key. b) Employee table contains the following columns(emp_no,empname,deptno,salary)set the emp_no to be the primary key and dept_no to be the foreign key. 2. Write SQL statements to Insert the following 3 rows in Employee table:        (101,’Sami’,’D-101’,5000)        (102,’Yousef’,’D-101’,4000)        (103,’Sami’,’D-102’,7000) 3. Write SQL statements to Insert the following 3 rows in Department table:       ...
In MySQL, create a new schema titled <yourlastname>module3. Using the below file, create the tables in...
In MySQL, create a new schema titled <yourlastname>module3. Using the below file, create the tables in your new schema and populate with the supplied data. Tables do not have keys. Do not define them at this time. There are errors in the data population (INSERT) statements. It is your job to find the errors and correct them. This is important. You will need correct data for future exercises in this module. In the submission area, choose Write Submission and identify...
In LINUX Provide the command(s) and any additional steps to perform the following tasks. You will...
In LINUX Provide the command(s) and any additional steps to perform the following tasks. You will need to be running with root privileges. Create a symlink to the /etc/openldap/ directory named "ldapconf" in /tmp/. Find all .conf files in the /etc/ directory and its subfolders. Find all directories starting with "gnome" or "GNOME" anywhere under the /usr/share/ directory. Sam and Susan are members of the "sales" group. Sam has been working on a project that has grown too big for...
Write C++ programs to perform the following tasks. In the program descriptions below, example input and...
Write C++ programs to perform the following tasks. In the program descriptions below, example input and output is provided. NOTE: You don’t need arrays to solve any of these problems. You should NOT use arrays to solve any of these problems. • stat.cpp: Let the user input a one or more integers, space separated, on a single line (as seen below), then work out and display the sum, average, sum of squares and population variance of the numbers. Remember, you...
Using your downloaded DBMS (MS SQL Server), create a new database. Create the database tables based...
Using your downloaded DBMS (MS SQL Server), create a new database. Create the database tables based on your entities defining The attributes within each table The primary and foreign keys within each table *****Show your database tables, tables attributes, primary and foreign keys***** Do not forget to check the lesson slides and videos that show you how to convert an ER/EER into a database schema, and how to create a database and tables using MS SQL Server.
Write the SQL queries that accomplish the following tasks using the AP Database 9. Write a...
Write the SQL queries that accomplish the following tasks using the AP Database 9. Write a select statement to show the invoicelineitemdescriptions that have the total invoicelineitemamount >1000 and the number of accountno is >2. 10. Write a select statement that returns the vendorid, paymentsum of each vendor, and the number of invoices of each vendor, where paymentsum is the sum of the paymentotal column. Return only the top ten vendors who have been paid the most and the number...
Using your downloaded DBMS (MS SQL Server or MySQL), write SQL queries that inserts at least...
Using your downloaded DBMS (MS SQL Server or MySQL), write SQL queries that inserts at least three rows in each table. For the On-Demand Streaming System, First, insert information for multiple users, at least three video items and insert the three different types of subscriptions (Basic, Advanced, Unlimited) into the database. Then insert at least three user subscriptions. Execute the queries and make sure they run correctly
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT