Getting errno: 150 in MySQL while trying to use composite keys - mysql

I'm new to both this site and databases. I am creating a database for a point of sale for a company. Im having problems since I have so many composite primary keys okay so here is my question... I have the table member:
DROP TABLE IF EXISTS member;
CREATE TABLE member (
fName varchar(10) NOT NULL,
lname varchar(10) NOT NULL,
shippingAddress varchar(50) NOT NULL,
billingAddress varchar(50) NOT NULL,
memberNo varchar(15) NOT NULL,
memStartDate date NOT NULL,
memOf varchar(15) ,
PRIMARY KEY(memberNo, shippingAddress)
);
and table shipping:
DROP TABLE IF EXISTS shipping;
CREATE TABLE shipping (
company varchar(10) NOT NULL,
warehouseAddress varchar(50) NOT NULL,
memberAddress varchar(50) NOT NULL,
memberNum varchar(15) NOT NULL,
orderNumber varchar(20) NOT NULL,
PRIMARY KEY(orderNumber)
);
ALTER TABLE shipping ADD CONSTRAINT shipping_memberAddnNUM_refs_member_memberNoNshippingAddress FOREIGN KEY (memberAddress, memberNum)
REFERENCES member (shippingAddress, memberNo);
So it gives me an errno: 150
I dont necessarily need member(shippingAddress) to be part of the primary key. But i do need to reference it my shipping table. Does anyone know how to get past this I have been looking at examples online but none of them seem to fix it.

I do it like this:
CONSTRAINT NAME_OF_PK PRIMARY KEY(Field1, Field2)
But this is Microsoft's SQL way of doing things.

You have the two columns in reverse order - from how they appear in the PRIMARY KEY of member. Try this:
ALTER TABLE shipping
ADD CONSTRAINT shipping_memberAddnNUM_refs_member_memberNoNshippingAddress
FOREIGN KEY (memberNum, memberAddress)
REFERENCES member (memberNo, shippingAddress);

Related

How do i make the output format of a DATE from a select query look the same in vsc as in mySQL workbench?

Im doing an assignment in school which, that includes creating a local database for a booking system. I created the tables like this :
SET foreign_key_checks = 0;
DROP TABLE IF EXISTS Users;
CREATE TABLE Users(
username VARCHAR(20) NOT NULL UNIQUE,
name VARCHAR(20) NOT NULL,
phoneNbr VARCHAR(20) NOT NULL,
address VARCHAR(255),
PRIMARY KEY (username)
);
DROP TABLE IF EXISTS Theatres;
CREATE TABLE Theatres(
theatreName VARCHAR(20) NOT NULL UNIQUE,
seats INT NOT NULL,
PRIMARY KEY (theatreName)
);
DROP TABLE IF EXISTS Movies;
CREATE TABLE Movies(
movieTitle VARCHAR(255) NOT NULL,
PRIMARY KEY (movieTitle)
);
DROP TABLE IF EXISTS Bookings;
CREATE TABLE Bookings(
bookingNbr INT NOT NULL AUTO_INCREMENT,
userName VARCHAR(20) NOT NULL,
day date NOT NULL,
movieTitle VARCHAR(255) NOT NULL,
PRIMARY KEY (bookingNbr),
FOREIGN KEY (userName) REFERENCES Users(userName),
FOREIGN KEY (movieTitle) REFERENCES Movies(movieTitle),
FOREIGN KEY (movieTitle, day) REFERENCES Performances(movieTitle, day),
CONSTRAINT oneUserOneTicket UNIQUE (movieTitle, userName)
);
DROP TABLE IF EXISTS Performances;
CREATE TABLE Performances(
movieTitle VARCHAR(255) NOT NULL,
bookedSeats INT,
theatreName VARCHAR(20) NOT NULL,
day DATE NOT NULL,
PRIMARY KEY(movieTitle, day),
FOREIGN KEY (movieTitle) REFERENCES Movies(movieTitle),
FOREIGN KEY (theatreName) REFERENCES Theatres(theatreName),
CONSTRAINT oneMovieOneDay UNIQUE (movieTitle, day)
);
SET foreign_key_checks = 1;
After creating theese tables, the query SELECT * FROM Performances;
gives me theese outputs in mySQL workbench and vsc respectivly
https://gyazo.com/4dd670122582d7ed5340e3b5a03eafbe
https://gyazo.com/366687f14944134610839cbb7a790081
Is there any way i can make the date format in the output in vsc look like the one in mySQL workbench?
Ive tried different convert statements and nls_format something something although i couldnt get it to work. Im very new to SQL as a whole.

Not sure why this syntax error is happening

Hi I'm not very familiar with MySQL as I have only started using it today and I keep getting this syntax error and am not really sure what the problem is. I have attached a screenshot of the code and also pasted it below with the error in bold.
I'm sorry if this is a silly error that is easily fixed I'm just not sure how to fix it and would be very appreciative of any help.
CREATE TABLE copy (
`code` INT NOT NULL,
isbn CHAR(17) NOT NULL,
duration TINYINT NOT NULL,
CONSTRAINT pkcopy PRIMARY KEY (isbn, `code`),
CONSTRAINT fkcopy FOREIGN KEY (isbn) REFERENCES book (isbn));
CREATE TABLE student (
`no` INT NOT NULL,
`name` VARCHAR(30) NOT NULL,
school CHAR(3) NOT NULL,
embargo BIT NOT NULL,
CONSTRAINT pkstudent PRIMARY KEY (`no`));
CREATE TABLE loan (
`code` INT NOT NULL,
`no` INT NOT NULL,
taken DATE NOT NULL,
due DATE NOT NULL,
`return` DATE NULL,
CONSTRAINT pkloan PRIMARY KEY (taken, `code`, `no`),
CONSTRAINT fkloan FOREIGN KEY (`code`, `no`) REFERENCES copy, student **(**`code`, `no`));
Create the tables first, then use the ALTER TABLE statement to add the foreign keys one by one. You won't be able to call two different tables on the foreign key, so you'll have to use an ID that maps to both. Here is an example to add the foreign keys after the table has been created:
Add a new table named vendors and change the products table to include the vendor id field:
USE dbdemo;
CREATE TABLE vendors(
vdr_id int not null auto_increment primary key,
vdr_name varchar(255)
)ENGINE=InnoDB;
ALTER TABLE products
ADD COLUMN vdr_id int not null AFTER cat_id;
To add a foreign key to the products table, you use the following statement:
ALTER TABLE products
ADD FOREIGN KEY fk_vendor(vdr_id)
REFERENCES vendors(vdr_id)
ON DELETE NO ACTION
ON UPDATE CASCADE;

MySQL Error with CREATE TABLE

I am new to creating/testing/working with MySQL.
In this I have created a database and have used the command to USE the Database, however when trying to create a table, Error Code:1064 keeps coming up.
I have no previous tables in this database, this would be the first table.
I am unsure where the error is and would greatly appreciate it if someone could help me identiy the error and the reason why?
CREATE TABLE Customers(
customerNumber INT NOT NULL,
firstName VARCHAR(60),
lastName VARCHAR(60),
address VARCHAR(50) NOT NULL,
city VARCHAR(20) NOT NULL,
state ENUM('QLD','VIC','NSW','WA','TAS','NT','SA') NOT NULL,
postCode INT(4) NOT NULL,
region VARCHAR(60),
email VARCHAR(254),
PRIMARY KEY(customerNumber),
FOREIGN KEY(customerNumber)
);
A foreign key must reference another table.
The other table must exist before you can reference it in a foreign key.
I contributed to a checklist for foreign keys in this post:
https://stackoverflow.com/a/4673775/20860

database using foreign keys

please excuse me if i'm doing things wrong, as I am new to this forum.. I am trying to figure out what columns to have for foreign keys... My teacher redid my database and i can't understand what to do. Here is my ERD: http://s1.postimg.org/3l2j6rh4v/Picture.png
I have looked up tutorials but I can't seem to find out how to use them..
Here is my MySQL code:
DROP TABLE PaymentType;
DROP TABLE ProjectType;
DROP TABLE Projects;
DROP TABLE Payment;
DROP TABLE Customer;
CREATE TABLE Customer
(
Customer_ID varchar(100) NOT NULL,
FName varchar(15) NOT NULL,
LName varchar(20) NOT NULL,
CustAddress varchar(20) NOT NULL,
CustCity varchar(30) NOT NULL,
CustState varchar(20),
CustZip char(5),
CustBal numeric(7,2) NOT NULL,
PRIMARY KEY (Customer_ID)
);
CREATE TABLE Payment
(
Payment_ID varchar(100) NOT NULL,
PaymentType varchar(15),
Date date NOT NULL,
AmntPaid numeric(7,2) NOT NULL,
PRIMARY KEY (Payment_ID)
);
CREATE TABLE Projects
(
Project_ID varchar(100) NOT NULL,
ProjectType varchar(30) NOT NULL,
LaborHrs char(3) NOT NULL,
Date date NOT NULL,
PRIMARY KEY (Project_ID)
);
CREATE TABLE ProjectType
(
Project_ID varchar(100) NOT NULL,
ProjectDesc varchar(100) NOT NULL,
PRIMARY KEY (Project_ID)
);
CREATE TABLE PaymentType
(
Payment_ID varchar(100) NOT NULL,
PaymentDesc varchar(100) NOT NULL,
PRIMARY KEY (Payment_ID)
);
Hopefully I didn't do anything wrong, thanks!
1) You should change your types for the id columns. Using varchar as id column should be avoided, as this will just blow off the performance (+ some other problems which might occur). Using any type of int (int, tinyint,..) would be much better.
2) The table "ProjectType" should have a column "ProjectType_ID" instead of "Project_ID" - same goes for the PaymentType ("PaymentType_ID").
3) Rename the fields in "Project" and "Payment" table as well.
4) The code for foreign-keys will look like:
ALTER TABLE Project
ADD CONSTRAINT Project_ProjectType
FOREIGN KEY (ProjectType_ID)
REFERENCES ProjectType(ProjectType_ID);
Do the same with Payment table.
Important: add "alter table"-command to the end of your sql file, so all tables are created
Important: your database schema should be innodb, or any other schema which supports foreign-keys (MyIsam does NOT support foreign-keys)
5) For the relation between Customer and Projects you will need an extra table (so one project can have multiple users | might be a change to your erd). A cross-relation table (called something Like CustomerProject) with fields "Project_ID" and "Customer_ID". Then add a foreign-key to the project table and add a foreign-key to customer table. (extra points for adding a unique key to "Project_ID" + "Customer_ID")
For the payment and user, just add a "Customer_ID" field in "Payment" table and add a foreign-key as explained in #4.
(offtopic): your naming convention is a bit wierd. Mixing underscore and camelcase should be avoided. You can just stick to camelcase - even for your id fields - but this is just a personal opinion

Can a table have two foreign keys?

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)
);