ERROR importing sql database - mysql

I am a beginner with the sql databases. I am trying to import a database to phpmyadmin and I am getting an error saying:
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 'DEFAULT_AUTOINCREMENT,
firstName varchar (30) NOT_NULL,
lastName var' at line 2
my sql code looks like that:
use myAddressBook
;
create table names (
personID int DEFAULT_AUTOINCREMENT,
firstName varchar (30) NOT_NULL,
lastName varchar (30) NOT_NULL,
constraint pk_name primary_key (personID)
)
;
create table addresses (
addressID int DEFAULT_AUTOINCREMENT,
personID int NOT_NULL,
address1 varchar (50),
address2 varchar (50),
city varchar (30),
state varchar (2),
zipcode varchar (10),
constraint fk_addresses foreign_key (personID)
references names (personID),
constraint pk_addresses primary_key (addressID)
)
;
create table phoneNumbers (
phoneID int DEFAULT_AUTOINCREMENT,
personID int NOT_NULL,
phoneNumber varchar (20),
constraint fk_phoneNumbers foreign_key (personID)
references names (personID),
constraint pk_phoneNumbers primary_key (phoneID)
)
;
create table emailAddresses (
emailID int DEFAULT_AUTOINCREMENT,
personID int NOT_NULL,
emailAddress varchar (50),
constraint fk_emailAddresses foreign_key (personID)
references names (personID),
constraint pk_emailAddresses primary_key (emailID)
)
;
Could anyone please point me in the right direction, where am I going wrong.
Thanks.

There are some issues with your code. You have to use correct MySQL key words instead :
Wrong | Correct
=========================================
DEFAULT_AUTOINCREMENT ===> AUTO_INCREMENT
NOT_NULL ===> NOT NULL
FOREIGN_KEY ===> FOREIGN KEY
PRIMARY_KEY ===> PRIMARY KEY
It should be like this :
USE myAddressBook
;
CREATE TABLE NAMES (
personID INT AUTO_INCREMENT,
firstName VARCHAR (30) NOT NULL,
lastName VARCHAR (30) NOT NULL,
CONSTRAINT pk_name PRIMARY KEY (personID)
)
;
CREATE TABLE addresses (
addressID INT AUTO_INCREMENT,
personID INT NOT NULL,
address1 VARCHAR (50),
address2 VARCHAR (50),
city VARCHAR (30),
state VARCHAR (2),
zipcode VARCHAR (10),
CONSTRAINT fk_addresses FOREIGN KEY (personID)
REFERENCES NAMES (personID),
CONSTRAINT pk_addresses PRIMARY KEY (addressID)
)
;
CREATE TABLE phoneNumbers (
phoneID INT AUTO_INCREMENT,
personID INT NOT NULL,
phoneNumber VARCHAR (20),
CONSTRAINT fk_phoneNumbers FOREIGN KEY (personID)
REFERENCES NAMES (personID),
CONSTRAINT pk_phoneNumbers PRIMARY KEY (phoneID)
)
;
CREATE TABLE emailAddresses (
emailID INT AUTO_INCREMENT,
personID INT NOT NULL,
emailAddress VARCHAR (50),
CONSTRAINT fk_emailAddresses FOREIGN KEY (personID)
REFERENCES NAMES (personID),
CONSTRAINT pk_emailAddresses PRIMARY KEY (emailID)
)
;

Try to change all the DEFAULT_AUTOINCREMENT to AUTO_INCREMENT and all the NOT_NULL to NOT NULL and primary_key to primary key and foreign_key to foreign key
In general it is because the code does not match the syntax supported by MySQL.

I am not an expert in php but i also experience this kind of problem . what i did was :
1. export the tables using phpmyadmin.
2. copy the exported tables inside the c:\xampp\mysql\bin DIRECTORY
3. go to COMMAND PROMPT and type
4. cd\
5. cd c:\xampp\mysql\bin
6. execute this simple command
mysql -u root -p newdatabase < names.sql
note : newdatabase is DATABASE_NAME
names.sql is table.sql that you exported from your database.
i hope it can help you.

Related

MySQL is not creating the table from an imported SQL file correctly

I converted a Microsoft SQL creation table syntax into MySQL and let MySQL import the SQL file, I keep getting an error. This is the code:
CREATE TABLE Adminstration (
AdminID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
ContactID int constraint fk_Admin_Contact foreign key (ContactID) references ContactInfo(ContactID),
EEID int constraint fk_Admin_Employee foreign key (EEID) references Employees(EEID),
LName VARCHAR(30) NOT NULL,
FName VARCHAR(30) NOT NULL,
EContactID int constraint fk_Admin_EContact foreign key (EContactID) references EmergencyContacts(ECID),
Position VARCHAR(50),
DOB DATE
);
However, I keep getting an error saying:
contraitnt" is not valid at this position for this server version, expecting AS, AUTO_INCREMENT, CHECK< COLLATE, COLUMN_FORMAT, COMMENT...
What is wrong with it?
You have made a small error, the constraints are not valid in the column definition that way.
CREATE TABLE Adminstration (
AdminID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
ContactID int,
constraint fk_Admin_Contact foreign key (ContactID) references ContactInfo(ContactID),
EEID int,
constraint fk_Admin_Employee foreign key (EEID) references Employees(EEID),
LName VARCHAR(30) NOT NULL,
FName VARCHAR(30) NOT NULL,
EContactID int
,constraint fk_Admin_EContact foreign key (EContactID) references EmergencyContacts(ECID),
Position VARCHAR(50),
DOB DATE
);

Foreign key constraint are are incompatible

I have a problem add FOREIGN KEY
CREATE TABLE Persons (
ID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (ID)
);
CREATE TABLE users (
userID int NOT NULL,
username varchar(20) NOT NULL,
password varchar(20) NOT NULL,
personID int NOT NULL,
PRIMARY KEY (userID),
FOREIGN KEY (personID) REFERENCES Persons(ID)
);
The error show:
MySQL said: Documentation
#3780 - Referencing column 'personID' and referenced column 'ID' in foreign key constraint 'users_ibfk_1' are incompatible.
Why not use the same name as in the persons table: ID instead of
personID
i.e
Change the column personID in Table users to ID:
personID ==> ID
Then try again to assign the foreign key

What's wrong with the syntax of this SQL command

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

ERROR 1064 (42000) && ERROR 1005 (HY000): Can't create table Contact_Info_Tbl (errno: 150)

I am receiving these errors when creating my tables, they work when I take out the foreign keys. I am relatively new to the building of a database, so I may be missing something simple
Create Table IF NOT EXISTS Classroom_Address_Mailing_Tbl (
ID int auto_increment primary key,
Street_Address varchar (25),
City varchar (25),
State_Abbr varchar (2),
Zipcode varchar (10)
);
Create Table IF NOT EXISTS Classroom_Address_Physical_Tbl (
ID int auto_increment primary key,
Street_Address varchar (25),
City varchar (25),
State_Abbr varchar (2),
Zipcode varchar (10)
);
Create Table IF NOT EXISTS Classroom_Info_Tbl (
ID int auto_increment primary key,
School_Name varchar (25),
Classroom_Name_Offical varchar (25),
Classroom_URL varchar (25),
Classroom_Address_Physical_ID int,
Classroom_Address_Mailing_ID int,
foreign key (Classroom_Address_Mailing_ID) references
Classroom_Address_Mailing_Tbl (ID) on delete cascade,
foreign key (Classroom_Address_Physical_ID) references
Classroom_Address_Physical_Tbl (ID) on delete cascade,
);
Create Table IF NOT EXISTS Contact_Info_Tbl (
Classroom_ID int,
Contact_Name varchar (20),
Contact_Title varchar (20),
Contact_Email varchar (20),
Contact_Phone varchar (20),
foreign key (Classroom_ID) references
Classroom_Info_Tbl (ID) on delete cascade
);
These are the errors I get :
ERROR 1064 (42000): 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 ')' at line 7
ERROR 1005 (HY000): Can't create table 'neClassCertForm.Contact_Info_Tbl' (errno: 150)
Ok the issue was a comma in the create table
Classroom_Address_Physical_Tbl (ID) on delete cascade,
which was breaking the table creation. Also add ENGINE=innodb to enforce the foreign key constraints.
Create Table IF NOT EXISTS `Classroom_Address_Mailing_Tbl` (
ID int auto_increment primary key,
Street_Address varchar (25),
City varchar (25),
State_Abbr varchar (2),
Zipcode varchar (10)
)ENGINE=InnoDB;
Create Table IF NOT EXISTS `Classroom_Address_Physical_Tbl` (
ID int auto_increment primary key,
Street_Address varchar (25),
City varchar (25),
State_Abbr varchar (2),
Zipcode varchar (10)
)ENGINE=InnoDB;
Create Table IF NOT EXISTS `Classroom_Info_Tbl` (
ID int auto_increment primary key,
School_Name varchar (25),
Classroom_Name_Offical varchar (25),
Classroom_URL varchar (25),
Classroom_Address_Physical_ID int,
Classroom_Address_Mailing_ID int,
foreign key (Classroom_Address_Mailing_ID) references
Classroom_Address_Mailing_Tbl (ID) on delete cascade,
foreign key (Classroom_Address_Physical_ID) references
Classroom_Address_Physical_Tbl (ID) on delete cascade
)ENGINE=InnoDB;
Create Table IF NOT EXISTS `Contact_Info_Tbl` (
Classroom_ID int,
Contact_Name varchar (20),
Contact_Title varchar (20),
Contact_Email varchar (20),
Contact_Phone varchar (20),
index ci (Classroom_ID),
foreign key (Classroom_ID) references
Classroom_Info_Tbl (ID) on delete cascade
)ENGINE=InnoDB;

MYSQL Foreign Key Errno (150) Can't create table

I just started learning MYSQL in college and I have an important assignment to do for my class. I have to create a small database and I can't seem to add a table with foreign keys because of the errno(150)
Here's what I have.
create table Country
(CountryName varchar (50) not null,
Primary Key (CountryName));
create table InterestGroup
(IntrestgrpName varchar (30) not null,
Primary Key (IntrestgrpName));
create table Organisation
(OrgName varchar (50) not null,
OrgAddress varchar (30),
OrgTelNo.varchar (30),
Primary Key (OrgName));
create table Qualification
(QualName varchar (50) not null,
Primary Key (QualName));
create table Member
(MemberID varchar (15) not null,
MemberName varchar (30),
MemberAdd varchar (50) not null,
CountryName varchar (50) not null,
IntrestgrpName varchar (30) not null,
QualName varchar (50) not null,
OrgName varchar (50) not null,
Primary Key (MemberID),
Foreign Key (CountryName) References Country (CountryName),
Foreign Key (IntrestgrpName) References InterestGroup (InterestgrpName),
Foreign Key (QualName) References Qualification (Qualname),
Foreign Key (OrgName) References Organisation (OrgName));
I cant seem to get the Member table to be created, It gives this error,
ERROR 1005 (HY000): Can't create table 'iicp.member' (errno: 150)
Thanks in advance for the help, I really need to solve this
here the working query
create table Country
(CountryName varchar (50) not null,
Primary Key (CountryName));
create table InterestGroup
(IntrestgrpName varchar (30) not null,
Primary Key (IntrestgrpName));
create table Organisation
(OrgName varchar (50) not null,
OrgAddress varchar (30),
OrgTelNo varchar (30),
Primary Key (OrgName));
create table Qualification
(QualName varchar (50) not null,
Primary Key (QualName));
create table Member
(MemberID varchar (15) not null ,
MemberName varchar (30),
MemberAdd varchar (50) not null,
CountryName varchar (50) not null,
IntrestgrpName varchar (30) not null,
QualName varchar (50) not null,
OrgName varchar (50) not null,
Primary Key (MemberID),
Foreign Key (CountryName) References Country (CountryName),
Foreign Key (IntrestgrpName) References InterestGroup (IntrestgrpName),
Foreign Key (QualName) References Qualification (Qualname),
Foreign Key (OrgName) References Organisation (OrgName));
DEMO HERE SQLFIDDLE
You SQL is correct.
It worked for me with change the following change:
OrgTelNo.varchar (30) to OrgTelNo varchar (30)
SHOW ENGINE INNODB STATUS;
...
------------------------
LATEST FOREIGN KEY ERROR
------------------------
130211 15:09:26 Error in foreign key constraint of table test/member:
Foreign Key (IntrestgrpName) References InterestGroup (InterestgrpName),
Foreign Key (QualName) References Qualification (Qualname),
Foreign Key (OrgName) References Organisation (OrgName)):
Cannot resolve column name close to:
),
Foreign Key (QualName) References Qualification (Qualname),
Foreign Key (OrgName) References Organisation (OrgName))
...
You referred column named InterestgrpName in table InterestGroup but actual name of column is IntrestgrpName
Foreign Key (IntrestgrpName) References InterestGroup (InterestgrpName),
You have type error here -----------------------------------^
OrgTelNo.varchar (30),
Instead of this you should write
OrgTelNo varchar (30),
And also it is a spell mistake in creation of last table member.
FOREIGN KEY ( IntrestgrpName ) REFERENCES InterestGroup ( IntrestgrpName)
Try this out instead.