List those publishers who publish paperbacks and the number of paperbacks published by each.
I'm having a difficult time with the counting the paperbacks for each publisher. Any help will be much appreciated. Thanks!
Henry Books Table Schema
**author**
authorNum INT PRIMARY KEY
authorLast VARCHAR(12),
authorFirst VARCHAR(10)
**publisher**
publisherCode CHAR(3) PRIMARY KEY
publisherName VARCHAR(25)
city VARCHAR(20)
**book**
bookCode CHAR(4) PRIMARY KEY
title VARCHAR(40)
publisherCode CHAR(3)
bookType CHAR(3)
paperback ENUM('No', 'Yes')
CONSTRAINT book_fk_publisher
FOREIGN KEY (publisherCode)
REFERENCES publisher(publisherCode)
**branch**
branchNum INT
branchName VARCHAR(50)
branchLocation VARCHAR(50)
**copy**
bookCode CHAR(4)
branchNum INT
copyNum INT PRIMARY KEY
quality ENUM('Excellent', 'Fair', 'Good', 'Poor')
price DECIMAL(8,2)
CONSTRAINT copy_pk
PRIMARY KEY (bookCode, branchNum, copyNum
CONSTRAINT copy_fk_book
FOREIGN KEY (bookCode)
REFERENCES book(bookCode),
CONSTRAINT copy_fk_branch
FOREIGN KEY (branchNum)
REFERENCES branch(branchNum)
**wrote**
bookCode CHAR(4)
authorNum INT
sequence INT
PRIMARY KEY (BookCode, AuthorNum),
CONSTRAINT wrote_fk_book
FOREIGN KEY (bookCode)
REFERENCES book(bookCode),
CONSTRAINT wrote_fk_author
FOREIGN KEY (authorNum)
REFERENCES author(authorNum)
This is what I have:
SELECT publisherName, COUNT(paperback) AS "numPaperback"
FROM publisher, book
WHERE paperback = "Yes";
Seems like you are new. So I will try to walk you through this...
Join the publisher table to the book table. They share a publisher code which appears to link them together.
Filter the joined table by book type using a where clause. You want paperbacks.
Perform a group by on publisher code and name, and then count the book column
Does this help get you started?
You have to use count(*) and Group by for the count of paperbacks. This is how I would write the query:
SELECT publisherName,count(*) AS "Number of paperbacks" FROM
publisher p INNER JOIN book b WHERE
p.publisherCode=b.publisherCode AND b.paperback="Yes"
GROUP BY publisherName;
Related
Basically I need to create a new table that uses specific information from two other tables.
For example, I have a table called person with the elements person_id, first_name, last_name, gender, age, and fav_quote. I have a second table called department with the elements dept_id, dept_name, and building. I now need to create and intersection table with the person_id and dept_id elements included. And both must be the primary key (which I assume just means PRIMARY KEY (person_id, dept_id) command in my source).
CREATE TABLE person (
person_id INT(8) NOT NULL auto_increment,
first_name VARCHAR(25) NOT NULL,
last_name VARCHAR(25) NOT NULL,
gender VARCHAR(1),
age INT(8),
fav_quote TEXT,
PRIMARY KEY (person_id)
);
CREATE TABLE department (
dept_id INT(8) NOT NULL auto_increment,
dept_name VARCHAR(25) NOT NULL,
building VARCHAR(25) NOT NULL,
PRIMARY KEY (dept_id)
);
That is the code I have for the initial two tables I'm just not sure how to create an intersection and, having gone back over my notes, I can't find the instructions on how to write it.
You got the primary key part right. I'd add foreign keys to your existing table in order to prevent creating interactions with people or departments that don't exist:
CREATE TABLE person_department
person_id INT(8) NOT NULL,
dept_id INT(8) NOT NULL,
PRIMARY KEY(person_id, dept_id),
FOREIGN KEY(person_id) REFERENCES person(person_id),
FOREIGN KEY(dept_id) REFERENCES department(dept_id)
)
You need a table with 2 fields; person_id and dept_id. The table will have foreign keys to the two tables person and department primary keys’ and a composite primary key of both.
Also, this table is only necessary if there is a one to many relationship of person to department. Otherwise just add dept_id as a foreign key in person.
im trying to create a products table with a primary and foreign key with this.
...
here is the code that i have tried so far
CREATE TABLE products
(
prod_id int NOT NULL,
prod_name int NOT NULL,
price varchar(15)
5on_hand varchar(15),
supp_id varchar(20),
PRIMARY KEY (prod_id),
FOREIGN KEY (supp_id)
);
any help would be greatly appreciated
A foreign key is a reference to a field in another table. To specify a foreign key, you have to specify what field(s) it refers to. The database cannot guess this.
So, guessing that supp_id refers to the id in the Suppliers table, your foreign key clause should look like this:
FOREIGN KEY (supp_id) REFERENCES Supplier(id)
I would strongly encourage the following:
All tables have an primary key column that is an integer and auto-incremented.
Names should be character strings.
Prices should be decimals.
So, I'm thinking something like this is appropriate:
CREATE TABLE Products (
ProductId int NOT NULL auto_increment primary key,
Name varchar(255) NOT NULL,
Price decimal(19, 4),
OnHand integer, -- assuming this is quantity on-hand
SupplierId int,
FOREIGN KEY (SupplierId) REFERENCES Suppliers(SupplierId)
);
So I need to make a query where 4 columns are listed from a table named 'tutorial'. However, items in these columns can only be listed if there is more than one student in them.
I currently have the columns I need from the TUTORIAL table, but I am not sure how to only list tutorials with more than one student. Any help is appreciated.
Table Setups:
STUDENT
(REGNUM NUMERIC(10) NOT NULL PRIMARY KEY,
FAMILY_NAME VARCHAR(30),
FIRST_NAME VARCHAR(30),
YOB SMALLINT
) ;
ROOM
(LOCATION VARCHAR(8) NOT NULL PRIMARY KEY,
CAPACITY DECIMAL(3)
) ;
TOPIC
(TOPIC_NUMBER VARCHAR(10) NOT NULL PRIMARY KEY,
TOPIC_NAME VARCHAR(40)
) ;
ENROLMENT
(
REGNUM NUMERIC(10),
TOPIC_NUMBER VARCHAR(10),
FOREIGN KEY (REGNUM) REFERENCES
STUDENT(REGNUM),
FOREIGN KEY (TOPIC_NUMBER) REFERENCES
TOPIC(TOPIC_NUMBER),
PRIMARY KEY (REGNUM,TOPIC_NUMBER)
) ;
TUTORIAL
(TUTEID DECIMAL(10) NOT NULL PRIMARY KEY,
TOPIC_NUMBER VARCHAR(10),
LOCATION VARCHAR(8),
TUTEDAY VARCHAR(10),
TUTETIME VARCHAR(8),
FOREIGN KEY(TOPIC_NUMBER) REFERENCES TOPIC(TOPIC_NUMBER),
FOREIGN KEY(LOCATION) REFERENCES ROOM(LOCATION)
) ;
TUTORIALBOOKING
(REGNUM NUMERIC(10),
TUTEID DECIMAL(10),
FOREIGN KEY(REGNUM) REFERENCES STUDENT(REGNUM),
FOREIGN KEY(TUTEID) REFERENCES TUTORIAL(TUTEID),
PRIMARY KEY(REGNUM,TUTEID)
);
How about:
SELECT * FROM TUTORIAL WHERE TUTEID in (SELECT TUTEID FROM TUTORIALBOOKING HAVING COUNT(TUTEID)>1)
That may not be quite right. I don't have a test DB to plug it into at the moment.
When trying to create the foreign keys on the last table I get the error "cannot add foreign key constraint" -
create database library_PW;
use library_PW;
create table title(
title_id varchar(20)primary key,
name varchar(50)not null,
reservation_no numeric(10),
lending_time varchar(15));
create table item(
title_id varchar(20)not null,
item_id varchar(20)not null,
constraint pk_item primary key(title_id,item_id));
create table magazine(
mag_id varchar(20)not null,
mag_date varchar(15)not null,
constraint pk_magazine primary key(mag_id,mag_date));
create table book(
ISBN varchar(20)primary key,
date_added date not null);
create table author(
author_id varchar(20)primary key,
author_name varchar(30)not null);
create table book_author(
ISBN varchar(20),
author_id varchar(20),
index (ISBN),
index (author_id),
constraint pk_book_author primary key(ISBN,author_id),
constraint fk_ISBNCode foreign key (ISBN) references book(ISBN),
constraint fk_authorcode foreign key (author_id) references author(author_id));
create table borrower(
membership_id varchar(20)primary key,
name varchar(20)not null,
address varchar(60)not null,
dob date not null,
date_joined date not null,
telephone numeric(12),
email varchar(30));
create table reservation(
title_id varchar(20),
membership_id varchar(20),
reserve_date varchar(20),
index (title_id),
index (membership_id),
constraint pk_reservation primary key(title_id, membership_id,reserve_date),
constraint fk_title foreign key(title_id) references title(title_id),
constraint fk_mem_id foreign key(membership_id) references borrower(membership_id));
create table loan(
title_id varchar(20),
item_id varchar(20),
borrower_date varchar(20),
index (title_id),
index (item_id),
constraint pk_reservation primary key(title_id,item_id,borrower_date),
constraint fk_loantitle foreign key(title_id) references title(title_id),
constraint fk_loanitem foreign key(item_id) references item(item_id));
Thanks in advance!
When I run into this issue, its always because I choose the wrong type data type.. The data type for the child column must match the parent column exactly.
Example: table.key = int & table.child=vchar
For me, its always that! Hope that helps.
Thanks all! It wasn't the data type (for once), it was the problem with the table item as suggested, and SHOW ENGINE INNODB STATUS\G pointed me to the answer.
All that was wrong was in the table item I should have had item_id before title_id.
Thanks again.
I have the following tables (Primary key in bold. Foreign key in Italic)
Customer table
ID---Name---Balance---Account_Name---Account_Type
Account Category table
Account_Type----Balance
Customer Detail table
Account_Name---First_Name----Last_Name---Address
Can I have two foreign keys in the Customer table and how can I implement this in MySQL?
Updated
I am developing a web based accounting system for a final project.
Account Category
Account Type--------------Balance
Assets
Liabilities
Equity
Expenses
Income
Asset
Asset_ID-----Asset Name----Balance----Account Type
Receivable
Receivable_ID-----Receivable Name-------Address--------Tel-----Asset_ID----Account Type
Receivable Account
Transaction_ID----Description----Amount---
Balance----Receivable_ID----Asset_ID---Account Type
I drew the ER(Entity relationship) diagram using a software and when I specify the relationship it automatically added the multiple foreign keys as shown above. Is the design not sound enough?
create table Table1
(
id varchar(2),
name varchar(2),
PRIMARY KEY (id)
)
Create table Table1_Addr
(
addid varchar(2),
Address varchar(2),
PRIMARY KEY (addid)
)
Create table Table1_sal
(
salid varchar(2),`enter code here`
addid varchar(2),
id varchar(2),
PRIMARY KEY (salid),
index(addid),
index(id),
FOREIGN KEY (addid) REFERENCES Table1_Addr(addid),
FOREIGN KEY (id) REFERENCES Table1(id)
)
Yes, MySQL allows this. You can have multiple foreign keys on the same table.
Get more details here FOREIGN KEY Constraints
The foreign keys in your schema (on Account_Name and Account_Type) do not require any special treatment or syntax. Just declare two separate foreign keys on the Customer table. They certainly don't constitute a composite key in any meaningful sense of the word.
There are numerous other problems with this schema, but I'll just point out that it isn't generally a good idea to build a primary key out of multiple unique columns, or columns in which one is functionally dependent on another. It appears that at least one of these cases applies to the ID and Name columns in the Customer table. This allows you to create two rows with the same ID (different name), which I'm guessing you don't want to allow.
Yes, a table have one or many foreign keys and each foreign keys hava a different parent table.
CREATE TABLE User (
user_id INT NOT NULL AUTO_INCREMENT,
userName VARCHAR(100) NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
userImage LONGBLOB NOT NULL,
Favorite VARCHAR(255) NOT NULL,
PRIMARY KEY (user_id)
);
and
CREATE TABLE Event (
EventID INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (EventID),
EventName VARCHAR(100) NOT NULL,
EventLocation VARCHAR(100) NOT NULL,
EventPriceRange VARCHAR(100) NOT NULL,
EventDate Date NOT NULL,
EventTime Time NOT NULL,
EventDescription VARCHAR(255) NOT NULL,
EventCategory VARCHAR(255) NOT NULL,
EventImage LONGBLOB NOT NULL,
index(EventID),
FOREIGN KEY (EventID) REFERENCES User(user_id)
);