I'm new to programming, and cannot understand the syntax errors that I'm getting.
CREATE SCHEMA IF NOT EXISTS donutsrus;
CREATE TABLE IF NOT EXISTS DONUT (
Donut_ID INT NOT NULL, UNIQUE
Donut_Name VARCHAR (20) NOT NULL, UNIQUE
Description VARCHAR (20) NOT NULL, UNIQUE
Unit_Price NUMERIC (4,2) NOT NULL
PRIMARY KEY (Donut_ID));
CREATE TABLE IF NOT EXISTS SALES_ORDER (
Sales_Order_ID INT NOT NULL, UNIQUE
Sales_Order_Date DATE NOT NULL
Sales_Order_Notes VARCHAR (100)
Customer_ID INT NOT NULL, UNIQUE
PRIMARY KEY (Sales_Order_ID)
FOREIGN KEY (Customer_ID));
CREATE TABLE IF NOT EXISTS CUSTOMER (
Customer_ID INT NOT NULL, UNIQUE
Last_Name VARCHAR (20) NOT NULL
First_Name VARCHAR (20) NOT NULL
Address VARCHAR (50) NOT NULL
Apt VARCHAR (5)
City VARCHAR (20) NOT NULL
State CHAR (2) NOT NULL
Zip NUMBER (5) NOT NULL
Home_Phone NUMBER (10)
Mobile_Phone NUMBER (10)
Other_Phone NUMBER (10)
PRIMARY KEY (Customer_ID));
CREATE TABLE IF NOT EXISTS ORDER_LINE_ITEM (
Donut_ID INT NOT NULL, UNIQUE
Sales_Order_ID INT NOT NULL, UNIQUE
Quantity INT NOT NULL
PRIMARY KEY (Donut_ID, Sales_Order_ID)
FOREIGN KEY (Donut_ID, Sales_Order_ID));
On the Donut table, I get Syntax error: extraneous input found - expected 'opening parenthesis', at the VARCHAR after Donut_Name. At the closing paren after the succeeding 20, I get Syntax error: extraneous input found - expected 'closing parenthesis'.
On the Order Line Item table I get, Syntax Error: 'INT' (int) is not valid input at this position, after the Sales_Order_ID. I also don't understand why it's okay on other lines of code, but not this one.
All the commas before UNIQUE are wrong. Commas are used to separate the columns, not to separate options for a specific column.
CREATE TABLE IF NOT EXISTS DONUT (
Donut_ID INT NOT NULL UNIQUE,
Donut_Name VARCHAR (20) NOT NULL UNIQUE,
Description VARCHAR (20) NOT NULL UNIQUE,
Unit_Price NUMERIC (4,2) NOT NULL,
PRIMARY KEY (Donut_ID));
CREATE TABLE IF NOT EXISTS CUSTOMER (
Customer_ID INT NOT NULL UNIQUE,
Last_Name VARCHAR (20) NOT NULL,
First_Name VARCHAR (20) NOT NULL,
Address VARCHAR (50) NOT NULL,
Apt VARCHAR (5),
City VARCHAR (20) NOT NULL,
State CHAR (2) NOT NULL,
Zip NUMBER (5) NOT NULL,
Home_Phone NUMBER (10),
Mobile_Phone NUMBER (10),
Other_Phone NUMBER (10),
PRIMARY KEY (Customer_ID));
CREATE TABLE IF NOT EXISTS SALES_ORDER (
Sales_Order_ID INT NOT NULL UNIQUE,
Sales_Order_Date DATE NOT NULL,
Sales_Order_Notes VARCHAR (100),
Customer_ID INT NOT NULL,
PRIMARY KEY (Sales_Order_ID),
FOREIGN KEY (Customer_ID) REFERENCES CUSTOMER (Customer_ID));
CREATE TABLE IF NOT EXISTS ORDER_LINE_ITEM (
Donut_ID INT NOT NULL,
Sales_Order_ID INT NOT NULL,
Quantity INT NOT NULL,
PRIMARY KEY (Donut_ID, Sales_Order_ID),
FOREIGN KEY (Donut_ID) REFERENCES DONUT (Donut_ID),
FOREIGN KEY (Sales_Order_ID) REFERENCES SALES_ORDER (Sales_Order_ID));
BTW, the UNIQUE option is not necessary for primary keys, they're automatically made unique. And in ORDER_LINE_ITEM, if the primary key is (Donut_ID, Sales_Order_ID) you probably don't want to make each of those columns unique by themselves. This prevents the same donut from being in multiple orders, or an order having more than one donut type. Similarly, Customer_ID should not be unique in SALES_ORDER; a customer can obviously have more than one order.
Your FOREIGN KEY lines were missing the references of the foreign keys. And the tables with the foreign keys need to be created after the tables they reference, so I moved SALES_ORDER down to after CUSTOMER.
Related
No matter how long I look at it I can't find the error.
I put it in a syntax checker online and it said the error was around the ending line.
CREATE TABLE employee (
emp_ID INT (30) NOT NULL,
position VARCHAR (30) NOT NULL,
emp_FName VARCHAR (30) NOT NULL,
emp_LName VARCHAR (30) NOT NULL,
ohip VARCHAR (15) NOT NULL,
home_Phone INT (15),
start_Date DATE,
team_ID INT (30) NOT NULL,
Constraint employee_emp_ID_PK Primary Key (emp_ID),
Constraint employee_team_ID_FK Foreign Key (team_ID)
)
A foreign key needs to references something. So, presumably:
Constraint employee_team_ID_FK Foreign Key (team_ID) references teams(team_id)
or something like that.
In addition, I'm not sure what you mean by int(30). This is merely the display width for the value, and because integers can have only 10 digits (well, 11 if you include a negative sign), 30 doesn't make sense.
For foreign key please specify reference table and its primary key.
CREATE TABLE employee (
emp_ID INT NOT NULL Primary Key,
position VARCHAR (30) NOT NULL,
emp_FName VARCHAR (30) NOT NULL,
emp_LName VARCHAR (30) NOT NULL,
ohip VARCHAR (15) NOT NULL,
home_Phone INT ,
start_Date DATE,
team_ID INT NOT NULL FOREIGN KEY REFERENCES reftable(ID),
)
I am currently taking a SQL class in my college and am currently stuck on review question my professor assigned. I keep getting an error for my schedule table. I feel it has something to do with my foreign key call but I'm not 100% sure? For the Schedule table, she wants the doctorID to be the foreign key and date/clinic to be the primaries
CREATE TABLE Doctor (
DoctorID numeric(3) not null,
Drname varchar (25) not null,
phonenum varchar (12) not null,
CONSTRAINT DoctorID_pk PRIMARY KEY (DoctorID)
);
CREATE TABLE Patient (
PatientNum numeric (5) not null,
SSNum numeric (9) not null,
PTName varchar (25) not null,
DOB numeric (8) not null,
PAddress varchar (40) not null,
DateAdmit numeric (8) not null,
Clinic varchar (30) not null,
CONSTRAINT PatientNum_pk PRIMARY KEY(PatientNum)
);
CREATE TABLE Treatment (
Treatmentcode numeric (6) not null,
Description varchar (50) not null,
CONSTRAINT Treatmentcode_pk PRIMARY KEY(Treatmentcode)
);
CREATE TABLE PatDocTreatment (
PatientNum numeric (5) not null,
DoctorID numeric (3) not null,
Treatmentcode numeric (6) not null,
DateofTreat numeric (8) not null,
Comments varchar (80) not null,
CONSTRAINT PK_PatDocTreatment PRIMARY
KEY(Patientnum,DoctorID,Treatmentcode,DateofTreat),
FOREIGN KEY(PatientNum) REFERENCES Patient(PatientNum),
FOREIGN KEY(DoctorID) REFERENCES Doctor(DoctorID),
FOREIGN KEY(Treatmentcode) REFERENCES Treatment(Treatmentcode)
);
CREATE TABLE Schedule (
DoctorID numeric (3) not null,
SchDate numeric (8) not null,
Clinic varchar (30) not null,
Hoursworked numeric (3) not null,
CONSTRAINT PK_Schedule PRIMARY KEY(DoctorID,SchDate,Clinic),
FOREIGN KEY (DoctorID) REFERENCES Doctor (DoctorID)
FOREIGN KEY (CLINIC) REFERENCES Patient (Clinic)
);
You have possibly missed a comma between the last two foreign keys declarations in your schedule table.
why does MySQL give me #1063 - Wrong column specification for column 'Date' error if I want to import this query? What I need to have in that row is Date with format: YYYY/MM/DD. Can someone help me and explain whats wrong and why this is wrong so I can learn :) Thank you!
CREATE TABLE Employee (
Employee_number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Lastname VARCHAR (255) NOT NULL,
Firstname VARCHAR (255) NOT NULL,
Residence VARCHAR (255) NOT NULL,
Skilllevel TINYINT NOT NULL,
Salary DECIMAL NOT NULL
);
CREATE TABLE Direct_hours (
Date DATE AUTO_INCREMENT PRIMARY KEY,
FOREIGN KEY Employee_number INT NOT NULL REFERENCES Employee (Employee_number),
FOREIGN KEY Project_number INT NOT NULL REFERENCES Project (Project_number)
);
CREATE TABLE Indirect_hours (
Date DATE NOT NULL AUTO_INCREMENT PRIMARY KEY,
FOREIGN KEY Employee_number INT NOT NULL REFERENCES Employee (Employee_number),
Type VARCHAR (255) NOT NULL,
Number INT NOT NULL
);
CREATE TABLE Customer (
Customer_number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Client_name VARCHAR (255) NOT NULL,
Residence VARCHAR (255) NOT NULL
);
CREATE TABLE Skilllevel (
Levels TINYINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
FOREIGN KEY Employee_number INT NOT NULL REFERENCES Employee (Employee_number),
Description VARCHAR (255) NOT NULL,
Hourly_rate TINYINT NOT NULL
);
CREATE TABLE Project (
Project_number INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
FOREIGN KEY Employee_number INT NOT NULL REFERENCES Employee (Employee_number),
FOREIGN KEY Customer_number INT NOT NULL REFERENCES Customer (Customer_number),
Hourly_rate TINYINT NOT NULL
);
I keep on getting error 1072 when trying to add a foreign key attribute and linking my attribute to another table. I've tried different ways of writing this but I keep on getting the message that "department_id doesn't exist in the table". If you could give me a hand that would be awesome! thank you so so much!
Here is the my code:
Alter table employee
add constraint department_fk
foreign key ( department_id)
references department(department_id);
And here is the rest of my tables:
Create table Department (
department_id integer auto_increment primary key not null,
department_name varchar (50) not null,
Office_number varchar(50) not null,
phone char (20) not null
);
Create table employee (
employee_id integer auto_increment primary key not null,
first_name varchar (25) not null,
last_name varchar (25) not null,
phone char(20) not null,
email varchar (100) not null,
salary decimal (10,2)
);
Create table project (
project_id integer auto_increment primary key not null,
max_hours time not null,
start_date datetime not null,
end_date datetime not null,
Project_lead integer,
Constraint project_fk1
foreign key (employee_id)
references employee(employee_id),
Department_id integer,
Constraint project_fk2
foreign key (department_id)
references department (department_id)
);
Create table assignment (
employee_id integer not null,
project_id integer not null,
hours_worked time not null,
primary key (employee_id, project_id),
constraint assignment_fk1
foreign key(employee_id)
references employee(employee_id),
constraint assignment_fk2
foreign key (project_id)
references project(project_id)
);
Alter table project
drop foreign key department_id;
Thank you in advance!
You need a Foreign key column in your employee table:
Create table employee (
employee_id integer auto_increment primary key not null,
first_name varchar (25) not null,
last_name varchar (25) not null,
phone char(20) not null,
email varchar (100) not null,
dept_id integer,
salary decimal (10,2)
);
Alter table employee
add constraint department_fk
foreign key ( dept_id)
references department(department_id);
Trust the message ;) - the column "department_id" doesn't exist in your employee table!
I have a controlled vocabulary table set up as such:
-- create the film_stock table
DROP TABLE IF EXISTS film_stock;
CREATE TABLE film_stock (
stock varchar (10) NOT NULL,
PRIMARY KEY(stock))
ENGINE = INNODB;
There are only two values assigned to 'stock' - Nitrate and Acetate.
'stock' serves as a foreign key in my table films:
-- create the films table
DROP TABLE IF EXISTS films;
CREATE TABLE films (
title varchar (100) NOT NULL,
alternate_title varchar (50) NULL,
year_of_release varchar (15) NULL,
country varchar (50) NULL,
running_time_minutes int (10) NULL,
footage_lenght_feet int (10) NULL,
stock varchar (10) NULL,
gauge varchar (10) NULL,
bw_color varchar (10) NULL,
notes varchar (255) NULL,
print_publications varchar (255) NULL,
existent_print varchar (255) NULL,
url_1 varchar (100) NULL,
url_2 varchar (100) NULL,
url_3 varchar (100) NULL,
PRIMARY KEY(title),
FOREIGN KEY(country) REFERENCES countries (country),
FOREIGN KEY(stock) REFERENCES film_stock (stock),
FOREIGN KEY(gauge) REFERENCES film_gauge (gauge),
FOREIGN KEY(bw_color) REFERENCES bw_or_color (bw_color))
ENGINE = INNODB;
In this films table the column 'stock' if often empty because I don't yet know what the film stock is for any particular film.
I'm getting this error when I run the script:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`hbm248_BucherFilms1`.`films`, CONSTRAINT `films_ibfk_2` FOREIGN KEY (`stock`) REFERENCES `film_stock` (`stock`))
Am I not allowed to have empty values in my films table when the foreign key it is referencing doesn't have empty values? I thought setting 'stock' to NULL in the films table would allow empty values... Or do I need to set stock to NULL in the film_stock table as well? Right now it is NOT NULL...
Or do I need to enter in some value like 'unknown' into the controlled vocab table 'stock' and then populate the stock column in films with this 'unknown' value and change it at a later date when I'm certain the stock is either Nitrate or Acetate?
This last option seems like it would solve my problems but also feels like it should be unnecessary and there should be a way for MySQL to allow empty values in the film table.
Thanks!
Can child table have null values?
This is not possible with current FK relationship. The sections below will provide guidance on setting up a vocabulary list.
Vocabulary list with Multiple Tables
Create a table for each list using an ID for PK. This ID will then used used as a FK on other tables.
CREATE TABLE film_stock (
`ID` int NOT NULL AUTO_INCREMENT,
stock varchar (10) NOT NULL,
PRIMARY KEY(ID))
ENGINE = INNODB;
and repeat for each list...
Alternative Way with a single table
CREATE TABLE VocabularyList (
`ID` int NOT NULL AUTO_INCREMENT,
groupName varchar (10) NOT NULL,
groupValue varchar (50) NOT NULL,
groupOrder int NOT NULL Default 0,
PRIMARY KEY(ID))
ENGINE = INNODB;
Insert statement for VocabularyList
insert into VocabularyList(groupName, groupValue, groupOrder) VALUES ('DefaultValue','Pending_Value',1);
insert into VocabularyList(groupName, groupValue, groupOrder) VALUES ('stock','nitrate',1);
insert into VocabularyList(groupName, groupValue, groupOrder) VALUES ('stock','acetate',2);
Query to retrieve stock information from VocabularyList
select groupValue
from VocabularyList
where groupName = 'stock'
order by groupOrder
Updated films table using single table alternative
CREATE TABLE films (
title varchar (100) NOT NULL,
alternate_title varchar (50) NULL,
year_of_release varchar (15) NULL,
countryID int NOT NULL DEFAULT 1,
running_time_minutes int (10) NULL,
footage_lenght_feet int (10) NULL,
stockID int NOT NULL DEFAULT 1,
gaugeID int NOT NULL DEFAULT 1,
bw_colorID int NOT NULL DEFAULT 1,
notes varchar (255) NULL,
print_publications varchar (255) NULL,
existent_print varchar (255) NULL,
url_1 varchar (100) NULL,
url_2 varchar (100) NULL,
url_3 varchar (100) NULL,
PRIMARY KEY(title),
FOREIGN KEY(countryID) REFERENCES VocabularyList(ID),
FOREIGN KEY(stockID) REFERENCES VocabularyList(ID),
FOREIGN KEY(gaugeID) REFERENCES VocabularyList(ID),
FOREIGN KEY(bw_colorID) REFERENCES VocabularyList(ID))
ENGINE = INNODB;
For ease of use, the films table primary key could be switched to an int datatype. This will ensure PK uniqueness and reduced keystrokes.