I am new to SQL databases. I am using MySQL for my current project
Here is my query
CREATE TABLE FRIENDS (
F_ID VARCHAR(50) NOT NULL PRIMARY KEY,
USER1_ID VARCHAR(50) NOT NULL FOREIGN KEY REFERENCES users(U_ID),
USER2_ID VARCHAR(50) NOT NULL FOREIGN KEY REFERENCES users(U_ID),
TIME_OF_FRIENDSHIP TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
When I run this query I get this error.
>[Error] Script lines: 1-22 -------------------------
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 'CREATE TABLE FRIENDS (
F_ID VARCHAR(50) NOT NULL PRIMARY KEY,
USER1_ID V' at line 17
Warnings: --->
W (1): 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 'CREATE TABLE FRIENDS (
F_ID VARCHAR(50) NOT NULL PRIMARY KEY,
USER1_ID V' at line 17
<---
[Executed: 18/11/2020 8:14:46 AM] [Execution: 0ms]
Can someone help me out with this problem.
I guess you are using MS SQL Server syntax and you try to run it on MySQL. Here is MySQL syntax for creating table with FK.
CREATE TABLE FRIENDS (
F_ID VARCHAR(50) NOT NULL PRIMARY KEY,
USER1_ID VARCHAR(50) NOT NULL,
USER2_ID VARCHAR(50) NOT NULL,
TIME_OF_FRIENDSHIP TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (USER2_ID) REFERENCES USERS(U_ID),
FOREIGN KEY (USER1_ID) REFERENCES USERS(U_ID)
);
Reference https://www.w3schools.com/sql/sql_foreignkey.asp
Related
I am trying to create a table which references two other tables that I have planned to make, but have not made yet. I am wondering if that is the issue here or if there is a syntax error that I am missing. If anyone can help me out it would be greatly appreciated
mysql> CREATE TABLE items (
items$id INT NOT NULL AUTO_INCREMENT,
sales$id INT NOT NULL AUTO_INCREMENT,
img$id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
desc VARCHAR(255) NOT NULL,
PRIMARY KEY(items$id),
FOREIGN KEY(sales$id) REFERENCES sales(sales$id),
FOREIGN KEY(img$id) REFERENCES image(img$id)
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 'desc VARCHAR(255) NOT NULL,
PRIMARY KEY(items$id),
FOREIGN KEY(sales$id) REFEREN' at line 6
I tried to remove the references, as in just do 'FOREIGN KEY(sales$id)' and 'FOREIGN KEY(img$id)' to see if that would work (I am new to mysql), but that also did not work. Any help is appreciated.
The problem is your field name: desc
desc is a keyword in mysql see here
Your sql would be better like this:
CREATE TABLE items ( items$id INT NOT NULL AUTO_INCREMENT, sales$id INT NOT NULL AUTO_INCREMENT, img$id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, descr VARCHAR(255) NOT NULL, PRIMARY KEY(items$id),
FOREIGN KEY(sales$id) REFERENCES sales(sales$id),
FOREIGN KEY(img$id) REFERENCES image(img$id))
Ps: you missed closing bracket ')' at the end of your sql query.
I have this code:
CREATE TABLE Company ( Comp_Code INT(5) NOT NULL AUTO_INCREMENT=1000, Comp_Name VARCHAR(15) NOT NULL, PRIMARY KEY (Comp_Code) );
but when i run it in MYSQL from WAMPServer it gives an error:
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 '=1000,
Comp_Name VARCHAR(15) NOT NULL,
PRIMARY KEY (Comp_Code)
)' at line 2
Why is this happening? I can't seem to find any solution to this particular issuer.
AUTO_INCREMENT is column option.
Initial AUTO_INCREMENT=1000 value is table option.
CREATE TABLE Company (
Comp_Code INT(5) NOT NULL AUTO_INCREMENT, -- specify that the column is AI
Comp_Name VARCHAR(15) NOT NULL,
PRIMARY KEY (Comp_Code)
) AUTO_INCREMENT=1000; -- specify initial AI value
I'm running the following code in MySQL Workbench:
CREATE TABLE beer_type ( -- create beer table
type_id INTEGER NOT NULL PRIMARY KEY, -- PK "type_id"
beer_name VARCHAR(30) NOT NULL,
beer_type VARCHAR(30) NOT NULL,
beer_id INTEGER NOT NULL,
FOREIGN KEY (beer_id) REFERENCES beer (beer_id));
I'm getting:
0 5 21:27:10 CREATE TABLE beer_type ( -- create beer table Error Code: 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 '' at line 1 0.000 sec
I'm new to SQL so this is a bit confusing to me and my professor isn't the best of help...
Any way anyone can help would be greatly appreciated!!
Copy this and paste :
CREATE TABLE beer_type (
type_id INTEGER NOT NULL PRIMARY KEY,
beer_name VARCHAR(30) NOT NULL,
beer_type VARCHAR(30) NOT NULL,
beer_id INTEGER NOT NULL,
FOREIGN KEY (beer_id) REFERENCES beer (beer_id));
I'm trying to put together a MySQL database for a forum, And when I try to make a section table I keep encountering a problem
#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 'TYPE = INNODB' at line 7
Here's the code:
CREATE TABLE sections (
sect_id INT(8) NOT NULL AUTO_INCREMENT,
sect_name VARCHAR(255) NOT NULL,
sect_desc VARCHAR(255) NOT NULL,
UNIQUE INDEX sect_name_unique (sect_name),
PRIMARY KEY (sect_id)
) TYPE=INNODB;
Use the following query
CREATE TABLE IF NOT EXISTS sections (
sect_id INT(8) NOT NULL AUTO_INCREMENT,
sect_name VARCHAR(255) NOT NULL,
sect_desc VARCHAR(255) NOT NULL,
UNIQUE INDEX sect_name_unique (sect_name),
PRIMARY KEY (sect_id)
) ENGINE=InnoDB
To mention the type of engine use ENGINE keyword.
I've looked at previous posts but I am struggling to figure out where i am going wrong.
I am trying to create a table using this code:
mysql> CREATE TABLE Engine
-> (
-> ID VARCHAR(255) UNASSSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
-> Name VARCHAR(255) NOT NULL,
-> Displacement VARCHAR(10) NOT NULL,
-> Code VARCHAR(10) NOT NULL,
-> PowerOutput VARCHAR(10),
-> MadeAt VARCHAR(255) NOT NULL,
-> PRIMARY KEY (ID),
-> FOREIGN KEY (MadeAt)
-> );
However I am repeatedly getting this error:
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 'UNASSSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
Name VARCHAR(255) NOT NULL,
Displace' at line 3
Any help would be much appreciated.
Try to change
ID VARCHAR(255) UNASSSIGNED NOT NULL AUTO_INCREMENT UNIQUE
into:
ID INT NOT NULL AUTO_INCREMENT
UNASSIGNED should be UNSIGNED, but it is not necessary to define this, because AUTO_INCREMENT will start at 1 by default when not defined otherwise. UNIQUE is redundant as well, because the ID will increment and the numbers will be unique anyway.
In most cases VARCHAR is not recommended to use as PRIMARY KEY.