MySQL syntax error uploading database - mysql
I am uploading an existing database to phpmyadmin, but when I try to input the data I get this error.
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'IDENTITY, acct_num varchar(25) NULL, acct_des ' at line 2
I am pretty familiar with phpmyadmin but I am not a programmer. Here is the data I am trying to input:
CREATE TABLE t_acct (
acct_id int IDENTITY,
acct_num varchar(25) NULL,
acct_des varchar(50) NULL,
status tinyint NULL,
PRIMARY KEY (acct_id)
)
go
CREATE TABLE t_asset (
asset_id int IDENTITY,
fund_id int NULL,
acct_id int NULL,
emp_id int NULL,
chkloc_id int NULL,
cond_id int NULL,
type_id int NULL,
class_id int NULL,
storloc_id int NULL,
outdte datetime NULL,
duedte datetime NULL,
bcode char(20) NOT NULL,
notes varchar(255) NULL,
des char(100) NULL,
model varchar(30) NULL,
sn varchar(50) NULL,
recdte datetime NULL,
price real NULL,
curdte datetime NULL,
curval real NULL,
dep real NULL,
deptype tinyint NULL,
image varchar(255) NULL,
status tinyint NOT NULL DEFAULT 1,
bin tinyint NULL,
bin_qty real NULL,
wardte datetime NULL,
purch_from varchar(255) NULL,
consumable tinyint NULL,
salvage_val real NULL,
reord_pt real NULL,
PRIMARY KEY NONCLUSTERED (asset_id)
)
go
CREATE INDEX XIF20t_asset ON t_asset
(
acct_id
)
go
CREATE INDEX XIF21t_asset ON t_asset
(
fund_id
)
go
CREATE INDEX XIF4t_asset ON t_asset
(
class_id
)
go
CREATE INDEX XIF5t_asset ON t_asset
(
type_id
)
go
CREATE INDEX XIF6t_asset ON t_asset
(
cond_id
)
go
CREATE INDEX XIF7t_asset ON t_asset
(
chkloc_id
)
go
CREATE INDEX XIF8t_asset ON t_asset
(
storloc_id
)
go
CREATE INDEX XIF9t_asset ON t_asset
(
emp_id
)
go
CREATE INDEX status ON t_asset
(
status
)
go
CREATE INDEX bcode ON t_asset
(
bcode
)
go
CREATE INDEX wardte ON t_asset
(
wardte
)
go
CREATE TABLE t_audit (
audit_id int IDENTITY,
audit_name varchar(255) NULL,
dte datetime NULL,
status tinyint NULL,
PRIMARY KEY NONCLUSTERED (audit_id)
)
go
CREATE INDEX dte ON t_audit
(
dte
)
go
CREATE INDEX status ON t_audit
(
status
)
go
CREATE TABLE t_audititems (
audititems_id int NOT NULL,
audit_id int NULL,
chk tinyint NULL,
ubcode char(20) NULL,
user_id int NULL,
abcode char(20) NULL,
asset_id int NULL,
ebcode char(20) NULL,
emp_id int NULL,
lbcode char(20) NULL,
loc_id int NULL,
expemp_id int NULL,
exploc_id int NULL,
dte datetime NULL,
status tinyint NULL,
bin_qty real NULL,
rec_type varchar(1) NULL,
asset_status varchar(20) NULL,
audit_reason varchar(50) NULL,
clear_table tinyint NULL,
notes varchar(255) NULL
)
go
ALTER TABLE t_audititems
ADD FOREIGN KEY (audit_id)
REFERENCES t_audit
go
CREATE TABLE t_bctype (
bctype_id int IDENTITY,
bctype varchar(50) NULL,
fntname varchar(50) NULL,
PRIMARY KEY (bctype_id)
)
go
CREATE TABLE t_class (
class_id int IDENTITY,
class char(20) NOT NULL,
des varchar(80) NULL,
status tinyint NOT NULL DEFAULT 1,
PRIMARY KEY NONCLUSTERED (class_id)
)
go
CREATE UNIQUE INDEX class ON t_class
(
class
)
go
CREATE INDEX status ON t_class
(
status
)
go
CREATE TABLE t_cond (
cond_id int IDENTITY,
cond char(20) NOT NULL,
des varchar(80) NULL,
status tinyint NOT NULL DEFAULT 1,
PRIMARY KEY NONCLUSTERED (cond_id)
)
go
CREATE UNIQUE INDEX XAK1t_cond ON t_cond
(
cond
)
go
CREATE INDEX status ON t_cond
(
status
)
go
CREATE TABLE t_curr_conns (
curr_conns_id int IDENTITY,
comp_name char(50) NULL,
tme datetime NULL,
PRIMARY KEY (curr_conns_id)
)
go
CREATE TABLE t_docs (
doc_id int IDENTITY,
asset_id int NULL,
doc_name varchar(255) NULL,
PRIMARY KEY NONCLUSTERED (doc_id)
)
go
CREATE INDEX XIF26t_docs ON t_docs
(
asset_id
)
go
CREATE INDEX doc_name ON t_docs
(
doc_name
)
go
CREATE TABLE t_emp (
emp_id int IDENTITY,
lname char(30) NULL,
fname char(80) NULL,
mname char(1) NULL,
bcode char(20) NULL,
login char(10) NULL,
pass char(10) NULL,
phone varchar(20) NULL,
altphone varchar(20) NULL,
notes varchar(255) NULL,
status tinyint NOT NULL DEFAULT 1,
ass tinyint NOT NULL DEFAULT 0,
res tinyint NOT NULL DEFAULT 0,
chk tinyint NOT NULL DEFAULT 0,
sup tinyint NOT NULL DEFAULT 0,
f_use tinyint NOT NULL DEFAULT 0,
mem tinyint NULL,
mem_ident_num varchar(20) NULL,
email varchar(80) NULL,
addr varchar(255) NULL,
category tinyint NULL,
emp_mem tinyint NULL,
PRIMARY KEY NONCLUSTERED (emp_id)
)
go
CREATE UNIQUE INDEX bcode ON t_emp
(
bcode
)
go
CREATE INDEX login ON t_emp
(
login
)
go
CREATE INDEX fname ON t_emp
(
fname
)
go
CREATE INDEX lname ON t_emp
(
lname
)
go
CREATE INDEX status ON t_emp
(
status
)
go
CREATE INDEX mem_ident_num ON t_emp
(
mem_ident_num
)
go
CREATE TABLE t_fac (
fac_id int IDENTITY,
fac varchar(40) NULL,
address varchar(100) NULL,
city varchar(40) NULL,
state varchar(2) NULL,
zip varchar(10) NULL,
notes varchar(255) NULL,
status tinyint NOT NULL DEFAULT 1,
PRIMARY KEY NONCLUSTERED (fac_id)
)
go
CREATE UNIQUE INDEX fac ON t_fac
(
fac
)
go
CREATE INDEX status ON t_fac
(
status
)
go
CREATE TABLE t_fund (
fund_id int IDENTITY,
fund_des varchar(50) NULL,
status tinyint NULL,
PRIMARY KEY (fund_id)
)
go
CREATE TABLE t_hist (
hist_id int IDENTITY,
fund_id int NULL,
acct_id int NULL,
cond_id int NULL,
fromloc_id int NULL,
loc_id int NULL,
fromemp_id int NULL,
emp_id int NULL,
asset_id int NULL,
dte datetime NULL,
type varchar(30) NULL,
bin_qty real NULL,
user_name varchar(120) NULL,
notes varchar(255) NULL,
PRIMARY KEY NONCLUSTERED (hist_id)
)
go
CREATE INDEX XIF10t_hist ON t_hist
(
asset_id
)
go
CREATE INDEX XIF11t_hist ON t_hist
(
emp_id
)
go
CREATE INDEX XIF12t_hist ON t_hist
(
loc_id
)
go
CREATE INDEX XIF13t_hist ON t_hist
(
cond_id
)
go
CREATE INDEX XIF15t_hist ON t_hist
(
fromemp_id
)
go
CREATE INDEX XIF16t_hist ON t_hist
(
fromloc_id
)
go
CREATE INDEX XIF18t_hist ON t_hist
(
acct_id
)
go
CREATE INDEX XIF19t_hist ON t_hist
(
fund_id
)
go
CREATE INDEX dte ON t_hist
(
dte
)
go
CREATE TABLE t_lbldesign_type (
design_type_id int IDENTITY,
design_name varchar(30) NULL,
design_type varchar(30) NULL,
PRIMARY KEY (design_type_id)
)
go
CREATE TABLE t_lbldesign (
design_id int NULL,
ele_type char(20) NULL,
ele_top smallint NULL,
ele_left smallint NULL,
ele_ht smallint NULL,
ele_wid smallint NULL,
font_name varchar(50) NULL,
font_bold varchar(5) NULL,
font_ital varchar(5) NULL,
font_size float NULL,
text_caption varchar(50) NULL,
text_datafld_seq smallint NULL,
graphic_fname varchar(255) NULL
)
go
CREATE INDEX XIF14t_lbldesign ON t_lbldesign
(
design_id
)
go
CREATE TABLE t_lbldesign_hdr (
design_id int IDENTITY,
design_name char(50) NULL,
design_wid float NULL,
design_ht float NULL,
design_type char(30) NULL,
design_orient varchar(1) NULL,
print_orient varchar(11) NULL,
design_type_id int NULL,
PRIMARY KEY (design_id)
)
go
CREATE TABLE t_lblref (
design_type char(20) NULL,
seq_num smallint NULL,
db_field_name varchar(50) NULL,
descript varchar(30) NULL,
sample_data varchar(50) NULL,
design_type_id int NULL
)
go
CREATE TABLE t_loc (
loc_id int IDENTITY,
emp_id int NULL,
fac_id int NULL,
loc char(50) NOT NULL,
bcode char(20) NULL,
stor tinyint NOT NULL DEFAULT 0,
notes varchar(255) NULL,
status tinyint NOT NULL DEFAULT 1,
PRIMARY KEY NONCLUSTERED (loc_id)
)
go
CREATE UNIQUE INDEX bcode ON t_loc
(
bcode
)
go
CREATE INDEX XIF1t_loc ON t_loc
(
fac_id
)
go
CREATE INDEX XIF17t_loc ON t_loc
(
emp_id
)
go
CREATE INDEX status ON t_loc
(
status
)
go
CREATE INDEX stor ON t_loc
(
stor
)
go
CREATE INDEX loc ON t_loc
(
loc
)
go
CREATE TABLE t_misc (
aclass varchar(25) NULL,
atype varchar(25) NULL,
afac varchar(25) NULL,
aloc varchar(25) NULL,
adep tinyint NULL,
apurge tinyint NULL,
afund varchar(25) NULL,
image_folder varchar(255) NULL,
auditseq int NULL,
cust_ident varchar(4) NULL,
license_key varchar(16) NULL,
bc_prefix varchar(3) NULL,
bc_seqnum varchar(20) NULL,
relocate tinyint NULL,
arepair tinyint NULL,
app_ver varchar(20) NULL,
amaint_rpt tinyint NULL,
afindpop tinyint NULL,
amem varchar(25),
mem_show tinyint NULL,
cat1_nme varchar(25),
cat2_nme varchar(25),
cat3_nme varchar(25),
mem_fldnme1 varchar(25),
mem_fldnme2 varchar(25),
mem_fldnme3 varchar(25),
mem_fldnme4 varchar(25),
smtp_host varchar(50),
smtp_addr varchar(50),
smtp_user varchar(50),
smtp_pass varchar(20),
smtp_type tinyint NULL,
srv_int int,
amodnum varchar(25),
asernum varchar(25),
acond varchar(25),
report_folder varchar(255),
logo varchar(255),
display_emp tinyint NULL,
display_acct tinyint NULL,
semester_enddte datetime NULL,
frd varchar(10),
req_chkin tinyint NULL,
areord_rpt tinyint NULL,
adep_pref tinyint NULL
)
go
CREATE TABLE t_res (
res_id int IDENTITY,
asset_id int NULL,
emp_id int NULL,
loc_id int NULL,
fdte datetime NULL,
tdte datetime NULL,
notes varchar(80) NULL,
prio tinyint NULL,
status tinyint NOT NULL DEFAULT 1,
PRIMARY KEY NONCLUSTERED (res_id)
)
go
CREATE INDEX XIF2t_res ON t_res
(
emp_id
)
go
CREATE INDEX XIF3t_res ON t_res
(
asset_id
)
go
CREATE INDEX loc_id ON t_res
(
loc_id
)
go
CREATE INDEX fdte ON t_res
(
fdte
)
go
CREATE INDEX tdte ON t_res
(
tdte
)
go
CREATE TABLE t_type (
type_id int IDENTITY,
type char(20) NULL,
des varchar(80) NULL,
dep real NULL,
status tinyint NOT NULL DEFAULT 1,
class_id int NULL,
duedte_type tinyint NULL,
duedte_num_days smallint NULL,
PRIMARY KEY NONCLUSTERED (type_id)
)
go
CREATE INDEX type ON t_type
(
type
)
go
CREATE INDEX status ON t_type
(
status
)
go
CREATE TABLE t_user_defined_data (
user_data_id int IDENTITY,
asset_id int NULL,
user_field_id int NULL,
user_data varchar(255) NULL,
dte_entered datetime NULL,
PRIMARY KEY NONCLUSTERED (user_data_id)
)
go
CREATE INDEX XIF24t_user_defined_data ON t_user_defined_data
(
user_field_id
)
go
CREATE INDEX XIF25t_user_defined_data ON t_user_defined_data
(
asset_id
)
go
CREATE INDEX user_data ON t_user_defined_data
(
user_data
)
go
CREATE TABLE t_user_defined_field (
user_field_id int IDENTITY,
type_id int NULL,
class_id int NULL,
user_field_name varchar(50) NULL,
status tinyint NULL,
PRIMARY KEY NONCLUSTERED (user_field_id)
)
go
CREATE INDEX XIF22t_user_defined_field ON t_user_defined_field
(
class_id
)
go
CREATE INDEX XIF23t_user_defined_field ON t_user_defined_field
(
type_id
)
go
CREATE INDEX user_field_name ON t_user_defined_field
(
user_field_name
)
go
ALTER TABLE t_asset
ADD FOREIGN KEY (fund_id)
REFERENCES t_fund
go
ALTER TABLE t_asset
ADD FOREIGN KEY (acct_id)
REFERENCES t_acct
go
ALTER TABLE t_asset
ADD FOREIGN KEY (emp_id)
REFERENCES t_emp
go
ALTER TABLE t_asset
ADD FOREIGN KEY (storloc_id)
REFERENCES t_loc
go
ALTER TABLE t_asset
ADD FOREIGN KEY (chkloc_id)
REFERENCES t_loc
go
ALTER TABLE t_asset
ADD FOREIGN KEY (cond_id)
REFERENCES t_cond
go
ALTER TABLE t_asset
ADD FOREIGN KEY (type_id)
REFERENCES t_type
go
ALTER TABLE t_asset
ADD FOREIGN KEY (class_id)
REFERENCES t_class
go
ALTER TABLE t_docs
ADD FOREIGN KEY (asset_id)
REFERENCES t_asset
go
ALTER TABLE t_hist
ADD FOREIGN KEY (fund_id)
REFERENCES t_fund
go
ALTER TABLE t_hist
ADD FOREIGN KEY (acct_id)
REFERENCES t_acct
go
ALTER TABLE t_hist
ADD FOREIGN KEY (fromloc_id)
REFERENCES t_loc
go
ALTER TABLE t_hist
ADD FOREIGN KEY (fromemp_id)
REFERENCES t_emp
go
ALTER TABLE t_hist
ADD FOREIGN KEY (cond_id)
REFERENCES t_cond
go
ALTER TABLE t_hist
ADD FOREIGN KEY (loc_id)
REFERENCES t_loc
go
ALTER TABLE t_hist
ADD FOREIGN KEY (emp_id)
REFERENCES t_emp
go
ALTER TABLE t_hist
ADD FOREIGN KEY (asset_id)
REFERENCES t_asset
go
ALTER TABLE t_lbldesign
ADD FOREIGN KEY (design_id)
REFERENCES t_lbldesign_hdr
go
ALTER TABLE t_loc
ADD FOREIGN KEY (emp_id)
REFERENCES t_emp
go
ALTER TABLE t_loc
ADD FOREIGN KEY (fac_id)
REFERENCES t_fac
go
ALTER TABLE t_res
ADD FOREIGN KEY (asset_id)
REFERENCES t_asset
go
ALTER TABLE t_res
ADD FOREIGN KEY (emp_id)
REFERENCES t_emp
go
ALTER TABLE t_user_defined_data
ADD FOREIGN KEY (asset_id)
REFERENCES t_asset
go
ALTER TABLE t_user_defined_data
ADD FOREIGN KEY (user_field_id)
REFERENCES t_user_defined_field
go
ALTER TABLE t_user_defined_field
ADD FOREIGN KEY (type_id)
REFERENCES t_type
go
ALTER TABLE t_user_defined_field
ADD FOREIGN KEY (class_id)
REFERENCES t_class
go
CREATE TABLE t_prov (
prov_id int IDENTITY,
prov_des varchar(50) NULL,
address varchar(100) NULL,
city varchar(40) NULL,
state varchar(2) NULL,
zip varchar(10) NULL,
notes varchar(255) NULL,
contact varchar(100) NULL,
status tinyint NOT NULL DEFAULT 1,
PRIMARY KEY NONCLUSTERED (prov_id)
)
go
CREATE INDEX prov_des ON t_prov
(
prov_des
)
go
CREATE INDEX status ON t_prov
(
status
)
go
CREATE TABLE t_maint (
maint_id int IDENTITY,
type_id int NULL,
class_id int NULL,
maint_name varchar(50) NULL,
spec_inst varchar(255) NULL,
m_interval real NULL,
status tinyint NOT NULL DEFAULT 1,
PRIMARY KEY NONCLUSTERED (maint_id)
)
go
IDENTITY is a Microsoft SQL Server keyword.
MySQL uses AUTO_INCREMENT for similar functionality.
Example:
CREATE TABLE t_acct (
acct_id int AUTO_INCREMENT,
acct_num varchar(25) NULL,
acct_des varchar(50) NULL,
status tinyint NULL,
PRIMARY KEY (acct_id)
);
Also "go" is a statement terminator in Microsoft SQL Server. MySQL uses ; or \G.
Related
Nested query in a view is giving me CHECK OPTION on non-updatable view
CREATE VIEW Res_borrow AS SELECT * FROM borrowing WHERE mem_id IN ( SELECT mpd.mem_id FROM ( SELECT mem_id, COUNT(*) AS no_curr_bks FROM borrowing WHERE borrowing.ret_date IS NULL GROUP BY borrowing.mem_id )Y RIGHT JOIN mpd ON y.mem_id = mpd.mem_id WHERE y.no_curr_bks < mpd.nob OR y.no_curr_bks IS NULL ) WITH CHECK OPTION; I tried to create such a view but i am getting error Here is the schema of db CREATE TABLE MEMBER ( mem_id INT PRIMARY KEY, NAME VARCHAR(100) NOT NULL, pass VARCHAR(12) NOT NULL, CHECK(CHAR_LENGTH(pass)>7), ph_no CHAR(11) NOT NULL, gender VARCHAR(10) NOT NULL, CHECK(gender='Female' OR gender='Male'), address VARCHAR(50) NOT NULL, age INT NOT NULL, email VARCHAR(50) NOT NULL, pkg_id INT DEFAULT 0, FOREIGN KEY (pkg_id) REFERENCES packages(pkg_id) ) create table mpd --Member Package Details ( mem_id int primary key foreign key references member(mem_id), nob int not null default 2, dur int not null default 14 ) create table borrowing ( bor_id int primary key, mem_id int foreign key references member(mem_id) not null, book_id int foreign key references book(book_id), iss_date date not null, ret_date date )
Add a foregin key to a different table
I'm trying to add username from my st_accounts column into my table results using mySQL. st_accounts table `id` int(11) NOT NULL, //Primary Key `firstname` varchar(50) NOT NULL, `lastname` varchar(50) NOT NULL, `username` varchar(50) NOT NULL, `password` varchar(255) NOT NULL, `email` varchar(100) NOT NULL, `img` text NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; results table `score_ID` int(11) NOT NULL, //Primary key `score` int(20) NOT NULL, `date` date NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I think you want: create table accounts ( account_id int auto_increment primary key, . . . ); create table results ( result_id int auto_increment primary key, account_id int not null score int NOT NULL, score_date date NOT NULL, constraint fk_results_accounts foreig key (accounts_id) references accounts(account_id) ) ; Notes: Declare the primary key explicitly, not in a comment. My convention is to name the primary key after the name of the table (in singular) with _id after it. The foreign key has the same name as the primary key -- self documenting. I made the integer primary keys auto_increment, so the database can assign unique values.
MYSQL 1215 Cannot add Foreign Key Constraing
Problem is on the bounding table column GroupDim.FacultyID and FacultyDim.FacultyID, data types are identity BIGINT This data store for OLAP cube wherein Rating_facts stores result of question which i exploring. table without signature Dim is transitional and serve for additional data for analysis.Maybe it is needs to divide this table? DROP TABLE IF EXISTS BlockDim; CREATE TABLE BlockDim ( BlockKey bigint NOT NULL AUTO_INCREMENT, BlockID bigint NOT NULL, BlockName varchar(32) NOT NULL, PRIMARY KEY (BlockKey) )ENGINE=InnoDB; DROP TABLE IF EXISTS FacultyDim; CREATE TABLE FacultyDim ( FacultyKey bigint NOT NULL AUTO_INCREMENT, FacultyID bigint NOT NULL, FacultyName varchar(32) NOT NULL, PRIMARY KEY (FacultyKey, FacultyID) )ENGINE=InnoDB; DROP TABLE IF EXISTS `Group`; CREATE TABLE `Group` ( GroupID bigint NOT NULL, FacultyID bigint NOT NULL, GroupName varchar(32) NOT NULL, PRIMARY KEY (GroupID), FOREIGN KEY (FacultyID) REFERENCES FacultyDim(FacultyID) )ENGINE=InnoDB; DROP TABLE IF EXISTS Subblock; CREATE TABLE Subblock ( SubblockKey bigint NOT NULL AUTO_INCREMENT, SubblockID bigint NOT NULL, BlockID bigint NOT NULL, SubblockName varchar(32) NOT NULL, PRIMARY KEY (SubblockKey) )ENGINE=InnoDB; DROP TABLE IF EXISTS Paragraph; CREATE TABLE Paragraph ( ParagraphKey bigint NOT NULL AUTO_INCREMENT, ParagraphID bigint NOT NULL, SubblockID bigint NOT NULL, ParagraphName varchar(64) NOT NULL, PRIMARY KEY (ParagraphKey) )ENGINE=InnoDB; DROP TABLE IF EXISTS RatingDim; CREATE TABLE RatingDim ( RatingKey bigint NOT NULL AUTO_INCREMENT, RatingID bigint NOT NULL, ParagraphID bigint NOT NULL, Score double NOT NULL, StageOfApprove int NOT NULL, Comment varchar(128) NOT NULL, `Date` DATE NOT NULL, PRIMARY KEY (RatingKey) )ENGINE=InnoDB; DROP TABLE IF EXISTS TimeDim; CREATE TABLE TimeDim ( TimeKey bigint NOT NULL AUTO_INCREMENT, `Date` DATE NOT NULL, DayOfWeek varchar(16) NOT NULL, `Month` varchar(16) NOT NULL, `Year` varchar(4) NOT NULL, PRIMARY KEY (`TimeKey`) )ENGINE=InnoDB; DROP TABLE IF EXISTS Rating_Fact; CREATE TABLE Rating_Fact ( TimeKey bigint NOT NULL, BlockKey bigint NOT NULL, FacultyKey bigint NOT NULL, RatingKey bigint NOT NULL, PopularBlock bigint NOT NULL, AvgFacultyScore bigint NOT NULL, StudentsMaxRating bigint NOT NULL, StudentsNotApprovedRating bigint NOT NULL, PRIMARY KEY (TimeKey,BlockKey,FacultyKey,RatingKey), FOREIGN KEY (TimeKey) REFERENCES TimeDim(TimeKey), FOREIGN KEY (BlockKey) REFERENCES BlockDim(BlockKey), FOREIGN KEY (FacultyKey) REFERENCES FacultyDim(FacultyKey), FOREIGN KEY (RatingKey) REFERENCES RatingDim(RatingKey) )ENGINE=InnoDB;
First you have check your table architecture.then try below two solutions Solution 1:- DROP TABLE IF EXISTS FacultyDim; CREATE TABLE FacultyDim ( FacultyKey bigint NOT NULL AUTO_INCREMENT, FacultyID bigint NOT NULL, FacultyName varchar(32) NOT NULL, PRIMARY KEY (FacultyKey, FacultyID) )ENGINE=InnoDB; DROP TABLE IF EXISTS `Group`; CREATE TABLE `Group` ( GroupID bigint NOT NULL, FacultyID bigint NOT NULL, GroupName varchar(32) NOT NULL, PRIMARY KEY (GroupID), FOREIGN KEY (FacultyID) REFERENCES FacultyDim(FacultyID,FacultyKey) )ENGINE=InnoDB; Solution 2:- DROP TABLE IF EXISTS FacultyDim; CREATE TABLE FacultyDim ( FacultyKey bigint NOT NULL AUTO_INCREMENT, FacultyID bigint NOT NULL, FacultyName varchar(32) NOT NULL, PRIMARY KEY (FacultyID) )ENGINE=InnoDB; DROP TABLE IF EXISTS `Group`; CREATE TABLE `Group` ( GroupID bigint NOT NULL, FacultyID bigint NOT NULL, GroupName varchar(32) NOT NULL, PRIMARY KEY (GroupID), FOREIGN KEY (FacultyID) REFERENCES FacultyDim(FacultyID) )ENGINE=InnoDB;
Cannot Add foreign key constraint Error 1215
I am using Workbench 6.0 and having a frustrating issue I am new to SQl and would like to keep this as simple as possible at this point, I believe my tables are ordered properly to add contraints except for my TICKET table, no idea how to find what foreign key is having problems. CREATE TABLE IF NOT EXISTS ACTION_TYPE ( ActionCode int primary key, Description char(50) not null )ENGINE=InnoDB; CREATE TABLE IF NOT EXISTS OFFICER ( PersonnelNo int primary key, OfficerLName char(50) not null, OfficerFName char(50) not null )ENGINE=InnoDB; CREATE TABLE IF NOT EXISTS DRIVER ( DriverLicenseNo int primary key, DriverLastName char(50) not null, DriverFirstName char(50) not null, DriverAddress char(50) not null, DriverCity char(50) not null, DriverProv char(50) not null, DriverPostalCode varchar(6) not null, DriverGender char(1) not null, DriverBirthDate Date not null )ENGINE=InnoDB; CREATE TABLE IF NOT EXISTS REGISTERED_OWNER ( RegOwnerID int primary key auto_increment, RegOwnerLName char(50) not null, RegOwnerFName char(50) not null, RegOwnerAddress char(50) not null, RegOwnerCity char(50) not null, RegOwnerProv char(50) not null, RegOwnerPostalCode varchar(6) not null )ENGINE=InnoDB; CREATE TABLE IF NOT EXISTS VEHICLE_TYPE ( VehicleType int primary key, VehicleDescription char(50) not null )ENGINE=InnoDB; CREATE TABLE IF NOT EXISTS VEHICLE ( VehicleLicense int primary key, ProvinceIssued char(2) not null, VehicleYear int not null, VehicleMake char(10) not null, VehicleType int not null, index (VehicleType), foreign key (VehicleType) references VEHICLE_TYPE (VehicleType) )ENGINE=InnoDB; CREATE TABLE IF NOT EXISTS TICKET ( <-----this table calling the error TicketNo int primary key auto_increment, TicketDateTime datetime not null, TicketLocationCity char(50) not null, TicketLocationProv char(50) not null, TicketLocationRoad char(50) not null, PersonnelNo int not null, VehicleLicense int not null, ActionCode int not null, RegOwnerID int not null, DriversLicenseNo int not null, index (PersonnelNo), foreign key (PersonnelNo) references OFFICER (PersonnelNo), index (VehicleLicense), foreign key (VehicleLicense) references VEHICLE (VehicleLicense), index (ActionCode), foreign key (ActionCode) references ACTION_TYPE (ActionCode), index (RegOwnerID), foreign key (RegOwnerID) references REGISTERED_OWNER (RegOwnerID), index (DriversLicenseNo), foreign key (DriversLicenseNo) references DRIVER (DriversLicenseNo) )ENGINE=InnoDB; CREATE TABLE IF NOT EXISTS VIOLATION_TYPE ( ViolationCode int primary key auto_increment, ViolationDesc char(50) not null, ViolationCurrFineAmt int not null )ENGINE=InnoDB; CREATE TABLE IF NOT EXISTS VIOLATION ( ViolationNo int primary key auto_increment, TicketNo int not null, ViolationCode int not null, AppliedFineAmount int not null, index (TicketNo), foreign key (TicketNo) references TICKET (TicketNo), index (ViolationCode), foreign key (ViolationCode) references VIOLATION_TYPE (ViolationCode) )ENGINE=InnoDB;
Spelling mistake on DriverLicenseNo in TICKET table ane DriversLicenseNo in DRIVER table
#1005 - Can't create table 'workswell_database.skoleni' Novice user with MySQL
sorry for my stupid question,but I do database in MySQL and was so happy,when i finished.Unfortunately,I had found a lot of mistakes in my database n when i will properly fix a bug,so i've another. Also,this is my Database and that guy what really boring right now,please write about my bugs and solution for them.I novice as a programmer and this database i do about 4 days ! CREATE TABLE`Skoleni` ( `sk_id` INT NOT NULL UNIQUE AUTO_INCREMENT, `Cena` MEDIUMINT NOT NULL, `Obsazenost` text NOT NULL, `Kapacita` datetime NOT NULL, `Popis` text, `Prerekvizity` text, `Certifikat` MEDIUMINT NOT NULL, PRIMARY KEY (`sk_id`), FOREIGN KEY (`sk_id`) REFERENCES Skoleni_has_Termin(`Skoleni_sk_id`), FOREIGN KEY (`sk_id`) REFERENCES Skoleni_has_Uzivatel(`Skoleni_sk_id`) ); CREATE TABLE `Skoleni_has_Termin`( `Skoleni_sk_id`INT NOT NULL UNIQUE, `Termin_ter_id` INT NOT NULL UNIQUE, PRIMARY KEY (`Skoleni_sk_id`) ); CREATE TABLE `Termin` ( `ter_id` INT NOT NULL UNIQUE AUTO_INCREMENT, `Datum_cas` DATETIME NOT NULL, `Misto_mo_id` INT NOT NULL, PRIMARY KEY (`ter_id`), FOREIGN KEY (`ter_id`) REFERENCES Skoleni_has_Termin(`Termin_ter_id`) ); CREATE TABLE `Misto` ( `mo_id` INT NOT NULL UNIQUE AUTO_INCREMENT, `ulice` MEDIUMINT NOT NULL, `cislo_popisne` MEDIUMINT NOT NULL, `lat` FLOAT (10,6) NOT NULL, `lng` FLOAT (10,6) NOT NULL, PRIMARY KEY (`mo_id`) )ENGINE = MYISAM; SELECT TABLE.Misto( INSERT (`ulice`, `cislo_popisne`, `lat`, `lng`), VALUES (`dr_Zikmunda_Wintra_376_5``16000 Praha 6 Bubenec``14.407438``50.101049`) ); CREATE TABLE `Skoleni_has_Uzivatel` ( `Skoleni_sk_id` INT NOT NULL UNIQUE, `Uzivatel_uziv_id` INT NOT NULL UNIQUE, PRIMARY KEY (`Skoleni_sk_id`) ); CREATE TABLE `Uzivatel` ( `uziv_id` INT NOT NULL UNIQUE AUTO_INCREMENT, `Jmeno` VARCHAR(30) NOT NULL, `Typ` MEDIUMINT UNIQUE, `Heslo` VARCHAR(32) NOT NULL, `Potvrzeni` VARCHAR(1) NOT NULL, PRIMARY KEY (`uziv_id`), FOREIGN KEY (`uziv_id`) REFERENCES Skoleni_has_Uzivatel(`Uzivatel_uziv_id`) ); CREATE TABLE `Skoleni_has_Lektor` ( `Skoleni_sk_id` INT NOT NULL UNIQUE, `Lektor_lek_id` INT NOT NULL UNIQUE, PRIMARY KEY (`Lektor_lek_id`) ); CREATE TABLE `Lektor` ( `lek_id` INT NOT NULL UNIQUE AUTO_INCREMENT, `Titul'pred'` VARCHAR(10) NOT NULL, `Jmeno` VARCHAR(20) NOT NULL, `Prijmeni` VARCHAR(20) NOT NULL, `Titul'za'` VARCHAR(10), `Firma` VARCHAR(30), `Rodne cislo` CHAR(11) NOT NULL, `Datum narozeni` DATE NOT NULL, PRIMARY KEY (`lek_id`) ); CREATE TABLE `Firma` ( `fir_id` INT NOT NULL UNIQUE AUTO_INCREMENT, `E-mail` VARCHAR(15) NOT NULL, `Telefon` VARCHAR(15) NOT NULL, `Web` VARCHAR(30), `IC` CHAR(8) NOT NULL, `DIC` VARCHAR(12) NOT NULL, `Misto_mo_id` INT NOT NULL, PRIMARY KEY (`fir_id`), PRIMARY KEY (`Misto_mo_id`), FOREIGN KEY (`Misto_mo_id`) REFERENCES Firma(`Misto_mo_id`) );
The tables `skoleni_has_termin' and 'skoleni_has_lektor' are most likely not needed here. I would bring dates to trainings table and leave just lektor_id in skoleni table as relation between trainings and lecturers is 1:n not m:n (unless one training can have more than one lecturer)