I’m a bit new to this and I receive a few errors and only 4 tables appear in my database(phpmyadmin). Could you please assist here?
#@(#) script.ddl
DROP TABLE IF EXISTS collect;
DROP TABLE IF EXISTS build;
DROP TABLE IF EXISTS ROBOT;
DROP TABLE IF EXISTS PART;
DROP TABLE IF EXISTS EMPLOYEE_CAR;
DROP TABLE IF EXISTS EMPLOYEE;
DROP TABLE IF EXISTS DOWNLOADED_PART;
DROP TABLE IF EXISTS CAR;
DROP TABLE IF EXISTS MANIFACTURER;
DROP TABLE IF EXISTS CONTRACT;
DROP TABLE IF EXISTS WAREHOUSE;
DROP TABLE IF EXISTS PART_PROVIDER;
DROP TABLE IF EXISTS DISTRIBUTOR;
DROP TABLE IF EXISTS CEO;
CREATE TABLE CEO
(
name varchar (255) NOT NULL,
surname varchar (255) NOT NULL,
mobile_number int NOT NULL,
email_address varchar (255) NOT NULL,
owned_factory_count int NOT NULL,
PRIMARY KEY(mobile_number)
);
CREATE TABLE DISTRIBUTOR
(
company_id int NOT NULL,
company_name varchar (255) NOT NULL,
country varchar (255) NOT NULL,
contact_phone int NOT NULL,
email_address varchar (255) NOT NULL,
PRIMARY KEY(company_id)
);
CREATE TABLE PART_PROVIDER
(
company_name int NOT NULL,
country varchar (255) NOT NULL,
contact_phone int NOT NULL,
email_address varchar (255) NOT NULL,
PRIMARY KEY(company_name)
);
CREATE TABLE WAREHOUSE
(
address varchar (255) NOT NULL,
capacity int NOT NULL,
storage_type char (10) NOT NULL,
PRIMARY KEY(storage_type),
CHECK(storage_type in ('parts', 'cars'))
);
CREATE TABLE CONTRACT
(
contract_id int NOT NULL,
date date NOT NULL,
car_count int NOT NULL,
price double precision NOT NULL,
contract_status char (8) NOT NULL,
fk_DISTRIBUTORcompany_id int NOT NULL,
fk_CEOmobile_number int NOT NULL,
PRIMARY KEY(contract_id),
CHECK(contract_status in ('accepted', 'declined', 'on hold')),
CONSTRAINT applies for FOREIGN KEY(fk_DISTRIBUTORcompany_id) REFERENCES DISTRIBUTOR (company_id),
CONSTRAINT accept FOREIGN KEY(fk_CEOmobile_number) REFERENCES CEO (mobile_number)
);
CREATE TABLE MANIFACTURER
(
group varchar (255) NOT NULL,
rank int NOT NULL,
country varchar (255) NOT NULL,
contact_phone int NOT NULL,
vehicles_count int NOT NULL,
fk_CEOmobile_number int NOT NULL,
PRIMARY KEY(group),
CONSTRAINT run FOREIGN KEY(fk_CEOmobile_number) REFERENCES CEO (mobile_number)
);
CREATE TABLE CAR
(
id int NOT NULL,
group varchar (255) NOT NULL,
model varchar (255) NOT NULL,
seats_count int NOT NULL,
manifacture_date date NOT NULL,
price double precision NOT NULL,
gearbox char (10) NOT NULL,
fuel_type char (11) NOT NULL,
body_type char (11) NOT NULL,
fk_WAREHOUSEstorage_type char (5) NOT NULL,
PRIMARY KEY(id),
CHECK(gearbox in ('automatic', 'mechanical')),
CHECK(body_type in ('sedan', 'universal', 'hatchback', 'coupe', 'convertible')),
CHECK(fuel_type in ('gasoline', 'electricity', 'diesel', 'hybrid')),
CONSTRAINT is stored in FOREIGN KEY(fk_WAREHOUSEstorage_type) REFERENCES WAREHOUSE (storage_type)
);
CREATE TABLE DOWNLOADED_PART
(
id int NOT NULL,
name varchar (255) NOT NULL,
quantity int NOT NULL,
price double precision NOT NULL,
quality char (6) NOT NULL,
fk_WAREHOUSEstorage_type char (5) NOT NULL,
fk_MANIFACTURERgroup varchar (255) NOT NULL,
fk_PART_PROVIDERcompany_name int NOT NULL,
PRIMARY KEY(id),
CHECK(quality in ('high', 'medium', 'low')),
CONSTRAINT is stored in FOREIGN KEY(fk_WAREHOUSEstorage_type) REFERENCES WAREHOUSE (storage_type),
CONSTRAINT buys FOREIGN KEY(fk_MANIFACTURERgroup) REFERENCES MANIFACTURER (group),
CONSTRAINT supply FOREIGN KEY(fk_PART_PROVIDERcompany_name) REFERENCES PART_PROVIDER (company_name)
);
CREATE TABLE EMPLOYEE
(
id int NOT NULL,
name varchar (255) NOT NULL,
surname varchar (255) NOT NULL,
role varchar (255) NOT NULL,
job_start date NOT NULL,
contact_phone int NOT NULL,
fk_MANIFACTURERgroup varchar (255) NOT NULL,
PRIMARY KEY(id),
CONSTRAINT work at FOREIGN KEY(fk_MANIFACTURERgroup) REFERENCES MANIFACTURER (group)
);
CREATE TABLE EMPLOYEE_CAR
(
name varchar (255) NOT NULL PRIMARY KEY,
surname varchar (255) NOT NULL PRIMARY KEY,
id int NOT NULL,
group varchar (255) NOT NULL,
model varchar (255) NOT NULL,
when_manifactured date NOT NULL,
fk_EMPLOYEEid int NOT NULL,
CONSTRAINT own FOREIGN KEY(fk_EMPLOYEEid) REFERENCES EMPLOYEE (id)
);
CREATE TABLE PART
(
id int NOT NULL,
name varchar (255) NOT NULL,
part_status char (10) NOT NULL,
fk_WAREHOUSEstorage_type char (5) NOT NULL,
fk_CARid int NOT NULL,
PRIMARY KEY(id),
CHECK(part_status in ('finished', 'unfinished')),
CONSTRAINT is stored in FOREIGN KEY(fk_WAREHOUSEstorage_type) REFERENCES WAREHOUSE (storage_type),
CONSTRAINT contain FOREIGN KEY(fk_CARid) REFERENCES CAR (id)
);
CREATE TABLE ROBOT
(
id int NOT NULL,
action varchar (255) NOT NULL,
maintanance_period int NOT NULL,
longevity int NOT NULL,
fk_MANIFACTURERgroup varchar (255) NOT NULL,
fk_responsible_employee_id int NOT NULL,
PRIMARY KEY(id),
CONSTRAINT operate in FOREIGN KEY(fk_MANIFACTURERgroup) REFERENCES MANIFACTURER (group),
CONSTRAINT control FOREIGN KEY(fk_responsible_employee_id) REFERENCES EMPLOYEE (id)
);
CREATE TABLE build
(
fk_PARTid int NOT NULL,
fk_EMPLOYEEid int NOT NULL,
PRIMARY KEY(fk_PARTid, fk_EMPLOYEEid),
CONSTRAINT build FOREIGN KEY(fk_PARTid) REFERENCES PART (id)
);
CREATE TABLE collect
(
fk_PARTid int NOT NULL,
fk_ROBOTid int NOT NULL,
PRIMARY KEY(fk_PARTid, fk_ROBOTid),
CONSTRAINT collect FOREIGN KEY(fk_PARTid) REFERENCES PART (id)
);
I receive these errors:
Error
Static analysis:
3 errors were found during analysis.
A comma or a closing bracket was expected. (near "precision" at position 116)
Unexpected beginning of statement. (near "8" at position 160)
Unrecognized statement type. (near "NOT NULL" at position 163)
SQL query: Copy
CREATE TABLE CONTRACT ( contract_id int NOT NULL, date date NOT NULL, car_count int NOT NULL, price double precision NOT NULL, contract_status char (8) NOT NULL, fk_DISTRIBUTORcompany_id int NOT NULL, fk_CEOmobile_number int NOT NULL, PRIMARY KEY(contract_id), CHECK(contract_status in (‘accepted’, ‘declined’, ‘on hold’)), CONSTRAINT applies for FOREIGN KEY(fk_DISTRIBUTORcompany_id) REFERENCES DISTRIBUTOR (company_id), CONSTRAINT accept FOREIGN KEY(fk_CEOmobile_number) REFERENCES CEO (mobile_number) )
MySQL said: Documentation
#1064 – You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘for FOREIGN KEY(fk_DISTRIBUTORcompany_id) REFERENCES DISTRIBUTOR (company_id)…’ at line 12
2
Answers
Run each create table by hand into mysql, this will help identify where the error is. Then correct the code as needed. If you get success on table creation, that one is working fine.
Somewhere there is an error in your sql code. Debug it!
This error is normally appreared when you try to add constraint before creating relatationship tables.
Try create all the tables then add their keys, constraints like this:
same for other tables.