I am creating a table where it has a foreign key so that it would be linked to another table, but it kept me giving this error, I already checked the syntax on w3schools, but I still keep getting errors any idea why? here's my SQL script
CREATE TABLE user_profile
(
user_Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
accnt_id INT,
first_name varchar(255),
last_name varchar(255),
biography TEXT,
date_joined DATETIME,
date_of_birth DATE,
email varchar(255),
gender varchar(255),
screenname varchar(255)
country varchar(255),
FOREIGN KEY (accnt_Id) REFERENCES accounts(accnt_Id)
)
Here's the 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 'country varchar(255), FOREIGN KEY (accnt_Id) REFERENCES accounts(accnt_Id) )' at line 13
You are missing , behind screenname varchar(255) change it to
screenname varchar(255),
and it should works.
You are missing a comma after the 'screenname' column
CREATE TABLE user_profile
(
user_Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
accnt_id INT,
first_name varchar(255),
last_name varchar(255),
biography TEXT,
date_joined DATETIME,
date_of_birth DATE,
email varchar(255),
gender varchar(255),
screenname varchar(255),
country varchar(255),
FOREIGN KEY (accnt_Id) REFERENCES accounts(accnt_Id)
)
Related
I am very new to SQL, I am using MySQL running a server set up for me to complete an assignment.
I can't understand what I am doing wrong here when assigning the foreign keys, here is my code
CREATE TABLE Customers (
CustomersID int NOT NULL AUTO_INCREMENT,
CustomerName varchar(50),
AddressLine1 varchar(50),
AddressLine2 varchar(50),
City varchar(50),
State varchar(50),
PostalCode varchar(50),
YTDPurchases decimal(19,2),
PRIMARY KEY (CustomersID)
);
CREATE TABLE TermsCode (
TermsCodeID varchar(50) NOT NULL,
Description varchar(50)
);
CREATE TABLE Invoices (
InvoiceID int NOT NULL AUTO_INCREMENT,
CustomerID int,
InvoiceDate datetime,
TermsCodeID varchar(50),
TotalDue decimal(19,2),
PRIMARY KEY (InvoiceID),
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomersID),
FOREIGN KEY (TermsCodeID) REFERENCES TermsCode(TermsCodeID)
);
I get this error
MySQL said: Documentation
#1005 - Can't create table table.Invoices` (errno: 150 "Foreign key constraint is incorrectly formed")
You need to add a primary key to the second table, as in:
CREATE TABLE TermsCode (
TermsCodeID varchar(50) NOT NULL,
Description varchar(50),
primary key(TermsCodeID)
);
Once you do that, the tables' creation works well. See example at db<>fiddle.
I am trying to figure out what is wrong with my syntax here. I feel like I have matched the examples I have found over the internet... but something must be amiss. Here is the code I am using. I keep getting the error code "cannot add Foreign Key constraint" when trying to run the build the schema.
CREATE TABLE Employe (
employee_id INT PRIMARY KEY,
first_name VARCHAR(30),
last_name VARCHAR(30),
hire_date DATE,
job_title VARCHAR(30),
shop_id INT,
FOREIGN KEY (shop_id) REFERENCES Coffee_Shop(shop_id)
);
CREATE TABLE Coffee_Shop (
shop_id INT PRIMARY KEY,
shop_name VARCHAR(50),
state CHAR(2),
);
CREATE TABLE Coffee (
coffee_id INT PRIMARY KEY,
shop_id INT,
supplier_id INT,
coffee_name VARCHAR(30),
price_per_pound NUMERIC(5,2),
FOREIGN KEY (shop_id) REFERENCES Coffee_Shop(shop_id),
FOREIGN KEY (supplier_id) REFERENCES Supplier(supplier_id)
);
CREATE TABLE Supplier (
supplier_id INT PRIMARY KEY,
company_name VARCHAR(50),
country VARCHAR(30),
sales_contact_name VARCHAR(60),
email VARCHAR(50) NOT NULL,
);
I just need to figure out what part of my foreign key is incorrectly defined. I know it normally results from an improperly defined primary key, but cannot seem to find where I messed up.
Thanks for your help
You are referencing a KEY that is not set up yet. SQL is linear, so the code gets executed line-by-line and cannot reference future lines (in most cases)
Create your tables in this order:
CREATE TABLE Coffee_Shop (
shop_id INT PRIMARY KEY,
shop_name VARCHAR(50),
state CHAR(2),
);
CREATE TABLE Supplier (
supplier_id INT PRIMARY KEY,
company_name VARCHAR(50),
country VARCHAR(30),
sales_contact_name VARCHAR(60),
email VARCHAR(50) NOT NULL,
);
CREATE TABLE Employe (
employee_id INT PRIMARY KEY,
first_name VARCHAR(30),
last_name VARCHAR(30),
hire_date DATE,
job_title VARCHAR(30),
shop_id INT,
FOREIGN KEY (shop_id) REFERENCES Coffee_Shop(shop_id)
);
CREATE TABLE Coffee (
coffee_id INT PRIMARY KEY,
shop_id INT,
supplier_id INT,
coffee_name VARCHAR(30),
price_per_pound NUMERIC(5,2),
FOREIGN KEY (shop_id) REFERENCES Coffee_Shop(shop_id),
FOREIGN KEY (supplier_id) REFERENCES Supplier(supplier_id)
);
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
);
mysql>
CREATE TABLE twitter(
username varchar(255),
created_at varchar(45),tweet text,
retweet_count int(11),
location varchar(100),
place varchar(100),
PRIMARY KEY int(11));
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 'int(11))' at line 1mys
The PRIMARY KEY clause is wrong.
You need to specify a name for your primary key column.
So, that would be ... id int(11), PRIMARY KEY (id));
You can not define PRIMARY KEY like that. The syntax for PRIMARY KEY is
PRIMARY KEY(Id)
Where you'll also have to create a column named Id
Id int(11)
try this
CREATE TABLE twitter (
id int(11) NOT NULL AUTO_INCREMENT,
username varchar(255),
created_at varchar(45),
tweet text,
retweet_count int(11),
location varchar(100),
place varchar(100),
PRIMARY KEY (id)
);
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.