SQL
I have a database
CREATE TABLE vendor
( vid CHAR(2) NOT NULL,
vname VARCHAR(25) NOT NULL,
PRIMARY KEY (vid) );
CREATE TABLE category
( catid CHAR(2) NOT NULL,
catname VARCHAR(25) NOT NULL,
PRIMARY KEY (catid) );
CREATE TABLE product
( pid CHAR(3) NOT NULL,
pname VARCHAR(25) NOT NULL,
price NUMERIC (7,2) NOT NULL,
vid CHAR(2) NOT NULL,
categoryid CHAR(2) NOT NULL,
PRIMARY KEY (pid));
CREATE TABLE region
( rid CHAR NOT NULL,
rname VARCHAR(25) NOT NULL,
PRIMARY KEY (rid) );
CREATE TABLE store
( sid VARCHAR(3) NOT NULL,
szip CHAR(5) NOT NULL,
rid CHAR NOT NULL,
PRIMARY KEY (sid));
CREATE TABLE customer
( cid CHAR(7) NOT NULL,
cname VARCHAR(15) NOT NULL,
czip CHAR(5) NOT NULL,
gender CHAR(1) NOT NULL,
PRIMARY KEY (cid) );
CREATE TABLE salestrans
( tid VARCHAR(8) NOT NULL,
custid CHAR(7) NOT NULL,
storeid VARCHAR(3) NOT NULL,
sdate VARCHAR(10) NOT NULL,
PRIMARY KEY (tid));
CREATE TABLE soldvia
( prodid CHAR(3) NOT NULL,
transid VARCHAR(8) NOT NULL,
numitems INT NOT NULL,
PRIMARY KEY (prodid, transid));
INSERT INTO vendor VALUES ('PG','Pacifica Gear');
INSERT INTO vendor VALUES ('MK','Mountain King');
INSERT INTO category VALUES ('CP','Camping');
INSERT INTO category VALUES ('FW','Footwear');
INSERT INTO product VALUES ('1X1','Zzz Bag',100,'PG','CP');
INSERT INTO product VALUES ('2X2','Easy Boot',70,'MK','FW');
INSERT INTO product VALUES ('3X3','Cosy Sock',15,'MK','FW');
INSERT INTO product VALUES ('4X4','Dura Boot',90,'PG','FW');
INSERT INTO product VALUES ('5X5','Tiny Tent',150,'MK','CP');
INSERT INTO product VALUES ('6X6','Biggy Tent',250,'MK','CP');
INSERT INTO product VALUES ('7X7','Comfy Pad',50,'PG','CP');
INSERT INTO product VALUES ('8X8','Turkey Jerky',2,'PG','CP');
INSERT INTO region VALUES ('C','Chicagoland');
INSERT INTO region VALUES ('T','Tristate');
INSERT INTO region VALUES ('D','Downstate');
INSERT INTO region VALUES ('O','OutOfstate');
INSERT INTO store VALUES ('S1','60600','C');
INSERT INTO store VALUES ('S2','60605','C');
INSERT INTO store VALUES ('S3','35400','T');
INSERT INTO customer VALUES ('1-2-333','Tina','60137', 'F');
INSERT INTO customer VALUES ('2-3-444','Tony','60611', 'M');
INSERT INTO customer VALUES ('4-5-666','Pam ','35401', 'F');
INSERT INTO salestrans VALUES ('T111','1-2-333','S1','1/1/2011');
INSERT INTO salestrans VALUES ('T222','2-3-444','S2', '1/1/2011');
INSERT INTO salestrans VALUES ('T333','1-2-333','S3', '2/1/2011');
INSERT INTO salestrans VALUES ('T444','4-5-666','S3', '2/1/2011');
INSERT INTO salestrans VALUES ('T555','2-3-444','S3', '2/1/2011');
INSERT INTO soldvia VALUES ('1X1','T111',1);
INSERT INTO soldvia VALUES ('2X2','T222',1);
INSERT INTO soldvia VALUES ('3X3','T333',5);
INSERT INTO soldvia VALUES ('1X1','T333',1);
INSERT INTO soldvia VALUES ('4X4','T444',1);
INSERT INTO soldvia VALUES ('2X2','T444',2);
INSERT INTO soldvia VALUES ('4X4','T555',4);
INSERT INTO soldvia VALUES ('5X5','T555',2);
INSERT INTO soldvia VALUES ('6X6','T555',1);

1.
a)
The SQL query to display name and id of the customers who made a transaction on 2/1/2011 using ‘in’ is given as follows:
SELECT cname, cid FROM customer, salestrans where customer.cid = salestrans.custid and sdate in ('2/1/2011');

b)
The SQL query to display name and id of the customers who made a transaction on 2/1/2011 using ‘exists’ is given as follows:
SELECT cname, cid FROM customer where exists (select custid from salestrans where custid = customer.cid and sdate = '2/1/2011');
c)
The SQL query to display name and id of the customers who made a transaction on 2/1/2011 using single block query is given as follows:
SELECT cname, cid FROM customer, salestrans where customer.cid = salestrans.custid and sdate = '2/1/2011';

2.
The SQL query to display the customer name and store zip code of all female customers who made a sales transaction at a store in the region named ‘Chicagoland’ is given as follows:
SELECT cname, szip FROM customer join salestrans on salestrans.custid = customer.cid join store on store.sid = salestrans.storeid join region on region.rid = store.rid where rname = 'Chicagoland' and gender = 'F';

3.
a)
The SQL query to display the customer name and customer zip code of all customers who did not buy any items from store S2 using nested query is given as follows:
select cname, czip from customer where cname <> (select cname from customer, salestrans where customer.cid = salestrans.custid and storeid = 'S2');

b)
The SQL query to display the customer name and customer zip code of all customers who did not buy any items from store S2 using nested query is given as follows:
select cname, czip from customer, salestrans except select cname, czip from customer, salestrans where customer.cid = salestrans.custid and salestrans.storeid = 'S2';

4.
The SQL query to display the total number of products that cost less than $100 is given as follows:
select count(price) as NumberOfProducts from product where price < 100;

5.
The SQL query to display the vendor name and cost of product with the lowest cost in category CP is given as follows:
select vname, price, min(price) as MinPrice from vendor, product where vendor.vid = product.vid and categoryid = 'CP';

6.
The SQL query to display the TID and total number of products sold for each transaction that has more than 1 product is given as follows:
select tid, count(pid) as TotalProduct from product, salestrans, soldvia where salestrans.tid = soldvia.transid and soldvia.prodid = product.pid and numItems > 1;

7.
The SQL query to display the region name, store id, and store zip for all regions even those regions which do not have any stores is given as follows:
select rname, sid, szip from region left outer join store on region.rid = store.rid;

SQL I have a database CREATE TABLE vendor ( vid CHAR(2) NOT NULL, vname VARCHAR(25) NOT...
Database Management
6. [5] Create the following table: CREATE TABLE customer ( cust_name VARCHAR(30) NOT NULL, address VARCHAR(60), UNIQUE (cust name, address)); A. Run the following inserts and explain why both work and how will you prevent it INSERT INTO customer VALUES ('Alex Doe', NULL); INSERT INTO customer VALUES ('Alex Doe', NULL); 7. [5] Modify the following table definition to ensure that all employees have a minimum wage of $10 CREATE TABLE employee ( empId INT PRIMARY KEY, empName VARCHAR(40)...
CREATE TABLE person ( pid INTEGER NOT NULL ,pname VARCHAR(30) NOT NULL ,PRIMARY KEY (pid) ); CREATE TABLE organization ( oid INTEGER NOT NULL ,oname VARCHAR(30) NOT NULL ,PRIMARY KEY (oid) ); CREATE TABLE venue ( vid INTEGER NOT NULL ,area CHAR(1) NOT NULL ,capacity INTEGER NOT NULL ,PRIMARY KEY (vid) ); CREATE TABLE calendar ( vid INTEGER NOT NULL ,date DATE NOT NULL ,price NUMERIC(6,2) NOT NULL ,PRIMARY KEY(vid,date) ,FOREIGN KEY(vid) REFERENCES venue(vid) ); CREATE TABLE event ( eid...
CREATE TABLE DEPT (
DEPTNO INTEGER NOT NULL,
DNAME VARCHAR(14),
LOC VARCHAR(13),
PRIMARY KEY (DEPTNO));
INSERT INTO DEPT VALUES (10,'SPORTS','NEW YORK');
INSERT INTO DEPT VALUES (20,'HOME','DALLAS');
INSERT INTO DEPT VALUES (30,'OUTDOOR','CHICAGO');
INSERT INTO DEPT VALUES (40,'CLOTHING','BOSTON');
CREATE TABLE EMP (
EMPNO INTEGER NOT NULL,
ENAME VARCHAR(10),
JOB VARCHAR(9),
MGR INTEGER,
SAL FLOAT,
COMM FLOAT,
DEPTNO INTEGER NOT NULL,
FOREIGN KEY (DEPTNO) REFERENCES DEPT (DEPTNO),
FOREIGN KEY (MGR) REFERENCES EMP(EMPNO),
PRIMARY KEY (EMPNO));
INSERT INTO EMP VALUES (7839,'KING','PRESIDENT',NULL,
5000,NULL,10);
INSERT INTO...
Relation Students has schema: CREATE TABLE Students ( BannerID CHAR(9), stuName VARCHAR(40) NOT NULL, scholarship INT, PRIMARY KEY(BannerID)); The relation Students is currently empty. Develop a test that determines whether an insertion into Students is currently legal. Then apply your test to determine which of the following INSERT statements is allowable. a. INSERT INTO Students VALUES(950111333, ’John Smith’, 1000); b. INSERT INTO Students (BannerID, stuName) VALUES(‘950111333’, ’John Smith’); c. INSERT INTO Students VALUES(‘950111222’, NOT NULL,...
CREATE TABLE Gender ( gender CHAR(1), description VARCHAR(10), PRIMARY KEY (gender) ); CREATE TABLE People ( ID INT, name VARCHAR(50), gender CHAR(1), height FLOAT, PRIMARY KEY (ID), FOREIGN KEY (gender) REFERENCES Gender (gender) ); CREATE TABLE Sports ( ID INT, name VARCHAR(50), record FLOAT, PRIMARY KEY (ID), UNIQUE (name) ); CREATE TABLE Competitions ( ID INT, place VARCHAR(50), held DATE, PRIMARY KEY (ID) ); CREATE TABLE Results ( peopleID INT NOT NULL, competitionID INT NOT NULL, sportID INT NOT NULL,...
CREATE TABLE vendor ( vendor_id int NOT NULL, vendor_name char(50) NOT NULL, contact_name char(50), CONSTRAINT vendors_pk PRIMARY KEY (vendor_id) ); What is the code to add the foreign key for this table? Can you please add the entire code with mine included. Thank you in advance.
Given (Oracle) SQL Script " CREATE TABLE Members( MemberID NUMBER(4) NOT NULL PRIMARY KEY, MFirst VARCHAR(25) NOT NULL, MLast VARCHAR(25) NOT NULL, Street VARCHAR(64) NOT NULL, City VARCHAR(25) NOT NULL, State VARCHAR(2) NOT NULL, ZipCode NUMBER(5) NOT NULL, CreditLimit NUMBER(7,2) NOT NULL, Gender VARCHAR(1) NOT NULL CHECK (Gender IN('F','M')) ); CREATE TABLE Employees( EmployeeID NUMBER(3) NOT NULL PRIMARY KEY, EFirst VARCHAR(25) NOT NULL, ELast VARCHAR(25) NOT NULL, ...
-- Schema definition
create table Customer (
cid smallint not null,
name varchar(20),
city varchar(15),
constraint customer_pk
primary key (cid)
);
create table Club (
club varchar(15) not null,
desc varchar(50),
constraint club_pk
primary key
(club)
);
create table Member (
club varchar(15) not null,
cid
smallint not null,
constraint member_pk
primary key (club,
cid),
constraint mem_fk_club
foreign key (club)
references Club,
constraint mem_fk_cust...
Create a stored procedure named prc_inv_amounts to update the INV_SUBTOTAL, INV_TAX, and INV_TOTAL. The procedure takes the invoice number as a parameter. The INV_SUBTOTAL is the sum of the LINE_TOTAL amounts for the invoice, the INV_TAX is the product of the INV_SUBTOTAL and the tax rate (8 percent), and the INV_TOTAL is the sum of the INV_SUBTOTAL and the INV_TAX /* Database Systems, Coronel/Morris */ /* Type of SQL : SQL Server */ /* WARNING: */ /* WE HIGHLY RECOMEND...
CREATE TABLE Users ( userId varchar (30) NOT NULL, pass varchar (30), fname varchar (50), lname varchar (50), email varchar (50), gender char(1), age integer, banned boolean, PRIMARY KEY (userId), UNIQUE(email)) CREATE TABLE FavSellers ( userId varchar (30), sellerId varchar (30), PRIMARY KEY (userId, sellerId), FOREIGN KEY(userId) references Users, FOREIGN KEY(sellerId) references Users(userId)) CREATE TABLE Items ( itemId integer, title varchar (50), ...