Although i think the syntax of the following mysql code is correct as per documentation, SQLFiddle pops up an error and since i think the syntax of the line that springs up the error is correct, i dont know what to change.
CREATE TABLE students
(
student_id DECIMAL(38) NOT NULL,
username VARCHAR(30),
email VARCHAR(80),
password VARCHAR(30),
f_name VARCHAR(30),
l_name VARCHAR(30),
bio VARCHAR(350),
dp VARCHAR(15),
is_suspended CHAR(1) DEFAULT '0' NOT NULL,
suspension_reason VARCHAR(150),
role_id DECIMAL(38) NOT NULL,
created_on DATETIME(6) DEFAULT SYSDATE() NOT NULL,
updated_on DATETIME(6),
is_active CHAR(1) DEFAULT '1' NOT NULL,
city VARCHAR(15) NOT NULL,
state VARCHAR(15) NOT NULL,
zip VARCHAR(6) NOT NULL,
b_day DATETIME,
CONSTRAINT students_id_pk PRIMARY KEY(student_id),
CONSTRAINT students_role_id_fk FOREIGN KEY(role_id) REFERENCES user_role(role_id),
CONSTRAINT students_username_uq UNIQUE(username),
CONSTRAINT students_email_uq UNIQUE(email)
);
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 'SYSDATE() NOT NULL,
updated_on DATETIME(6),
is_active CHAR(1) DEFAULT '1' at line 14
Please help me resolve this issue.
As explained in the documentation, synonyms for CURRENT_TIMESTAMP are allowed for the default value. SYSDATE() is not such a synonym. You can use NOW() (or several other constructs).
So, the correct syntax for that column is:
created_on DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
Related
CREATE TABLE users(
user_id int(11) auto_increment primary key,
f_name VARCHAR(255) NOT NULL,
l_name VARCHAR(255) not NULL,
u_name VARCHAR NOT NULL UNIQUE,
phone int(15) not null UNIQUE,
email VARCHAR not null UNIQUE,
pwd VARCHAR not null,
confpwd VARCHAR not null,
state VARCHAR(255),
lga VARCHAR(255) DEFAULT null,
preference enum('m','f','mf') DEFAULT 'mf',
gender enum('m','f') DEFAULT null,
profile_image VARCHAR(255),
bio VARCHAR(1000) DEFAULT null,
forgot_pwd_code VARCHAR (255),
forgot_pwd_time TIMESTAMP DEFAULT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
hobbies_id int(11),
FOREIGN KEY (hobbies_id) REFERENCES hobbies (userId) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
i keep getting this error
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 'NOT NULL UNIQUE, phone int(15) not null UNIQUE, email VARCHAR not null' at line 5
The VARCHAR datatypes requires a length (that represents the maximum number of characters allowed) - not providing it is a syntax error.
This is true not only for column uname, but for also for pwd and confpwd. Each of these columns should be declared like varchar(<N>), where <N> is the length of the column, instead of just varchar.
I was following W3Schools' guide on how to create a Foreign Key but every time I try to run my code it gives me an error.
My code
CREATE TABLE students(
`Student Number` INT NOT NULL PRIMARY KEY,
`First Name` VARCHAR(255) NOT NULL,
`Last Name` VARCHAR(255) NOT NULL,
Address VARCHAR(255) NOT NULL,
City VARCHAR(255) NOT NULL,
State VARCHAR(255) NOT NULL,
Zip INT NOT NULL,
`Date of Birth` DATE NOT NULL,
`Major ID` INT,
Advisor VARCHAR(255),
`Enroll Date` DATE,
`Total Credits Complete` INT,
FOREIGN KEY `Major ID` REFERENCES Major(`Major ID`)
);
I'm trying to give 'Major ID' a Foreign Key but instead I get this error code
Error SQL query:
CREATE TABLE students(
Student Number INT NOT NULL PRIMARY KEY,
First Name VARCHAR(255) NOT NULL,
Last Name VARCHAR(255) NOT NULL,
Address VARCHAR(255) NOT NULL,
City VARCHAR(255) NOT NULL,
State VARCHAR(255) NOT NULL,
Zip INT NOT NULL,
Date of Birth DATE NOT NULL,
Major ID INT,
Advisor VARCHAR(255),
Enroll Date DATE,
Total Credits Complete INT,
FOREIGN KEY Major ID REFERENCES Major(Major ID) ) MySQL said:
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 'REFERENCES Major(Major ID) )' at line 14
I would really appreciate it if someone could gear me in the right direction.
Thank you for taking your time to help me.
> CREATE TABLE If NOT EXISTS HARDWARE
(
'RECORD_ID' int PRIMARY KEY NOT NULL,
'Asset_Number' int(10) unique not null index,
'Type' char(20) not null,
'Name' varchar(50) not null index,
'Device Description' varchar(50) not null,
'Building' char(40) not null index,
'Date Created' date DEFAULT CURDATE() not null,
'Serial Number' varchar(50) index not null,
'Owner' char(50) index,
'User Created' CHAR ON CREATE USER() INDEX,
'Date Modified' date ON UPDATE CURDATE(),
'Purchase Year' int(4),
'Model' char(50),
'User Modified' CHAR(50),
'PO Number' int(20),
'Location' VARCHAR(50) index,
'OS' VARCHAR(20),
'RAM' int(4),
'Notes' text,
'Verified_By_t' char(50) on update user(),
'Internal IP Address' ON UPDATE USER(),
'Verified_Date_d' date ON UPDATE CURDATE(),
'Ethernet MAC' VARCHAR(20) index,
'Wireless MAC' VARCHAR(20) index,
'Usage' VARCHAR(20),
'Funding Source' VARCHAR(20),
'Lock Combo' int(10),
'ServiceLog::Date Created' VARCHAR(20),
'ServiceLog::Issue' VARCHAR(20),
'Ext. Ethernet Adapter' VARCHAR(20),
'AD' bit,
'Updated' date on update CURDATE(),
'Asset LI Hardware::ID Link' VARCHAR(20),
'Hardware' VARCHAR(20),
'Hardware Asset LI::Description' VARCHAR(20),
'Hardware Asset LI::Location' VARCHAR(20),
'Hardware Asset LI::Type' VARCHAR(20),
'Main Menu::Company' VARCHAR(20)
)
and the error I am getting is
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 ''RECORD_ID' int PRIMARY KEY NOT NULL, 'Asset_Number' int unique not null index, ' at line 3
I have tried varying this with:
CREATE TABLE If NOT EXISTS HARDWARE
(
RECORD_ID int PRIMARY KEY NOT NULL,
Asset_Number int unique not null index,
CREATE TABLE If NOT EXISTS HARDWARE
(
Asset_Number int unique not null index,
and other variations.
This is what I currently have and instead of one error it shows 5 errors.
CREATE TABLE If NOT EXISTS HARDWARE
(
RECORD_ID int PRIMARY KEY NOT NULL,
Asset_Number int unique not null index,
Type char(20) not null,
Name varchar(50) not null index,
Device_Description varchar(50) not null,
Building char(40) not null index,
Date_Created date DEFAULT CURDATE() not null,
Serial_Number varchar(50) index not null,
Owner char(50) index,
User_Created CHAR ON CREATE USER() INDEX,
Date_Modified date ON UPDATE CURDATE(),
Purchase_Year int(4),
Model char(50),
User_Modified CHAR(50),
PO_Number int(20),
Location VARCHAR(50) index,
OS VARCHAR(20),
RAM int(4),
Notes text,
Verified_By_t char(50) on update user(),
Internal_IP_Address ON UPDATE USER(),
Verified_Date_d date ON UPDATE CURDATE(),
Ethernet_MAC VARCHAR(20) index,
Wireless_MAC VARCHAR(20) index,
Usage VARCHAR(20),
Funding_Source VARCHAR(20),
Lock_Combo int(10),
AD bit,
Updated_date on update CURDATE()
)
and the error I see for it:
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 'index, Type char(20) not null, Name varchar(50) not null index, Device_Descripti' at line 4
You have numerous errors in your code. Here are some that I've spotted:
The index should be a separate line, not inline.
MySQL supports ON UPDATE DEFAULT CURRENT_TIMESTAMP . . . for one column and that column should be a datetime.
There is no ON UPDATE USER().
The IP Address does not have a type.
There is no ON CREATE.
(Note: Perhaps the most recent version of MySQL does support some of these options, but they don't look familiar to me.)
I would suggest that you create the table starting with the first column -- say by commenting out all the other columns. Then, add one column at a time and fix the errors as you encounter them.
This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
I just can't understand why I'm having a syntax error on this table creation:
CREATE TABLE IF NOT EXISTS Tasks (
ID INT UNSIGNED NOT NULL auto_increment,
Name VARCHAR(255) DEFAULT '' NOT NULL,
Description TEXT,
Date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
Status ENUM('New', 'Assigned', 'In Progress', 'On Hold', 'Done', 'Canceled'),
Priority ENUM('Urgent', 'High', 'Normal', 'Low'),
Creator SMALLINT UNSIGNED DEFAULT '0' NOT NULL,
Assignee SMALLINT UNSIGNED,
Starting DATETIME,
Deadline DATETIME,
Completion DATETIME,
PRIMARY KEY(ID)
) ENGINE = INNODB;
Gives me:
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
'Starting DATETIME,
Deadline DATETIME,
Completion DATETIME,
PRIMARY KEY(ID)
) ENG' at line 10
Looks like its giving me trouble with datetime but why? I've used this in another table and it's working.
Thanks.
STARTING is a reserved keyword, It should be escape with backtick,
CREATE TABLE IF NOT EXISTS Tasks
(
ID INT UNSIGNED NOT NULL auto_increment,
Name VARCHAR(255) DEFAULT '' NOT NULL,
Description TEXT,
Date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
Status ENUM('New', 'Assigned', 'In Progress', 'On Hold', 'Done', 'Canceled'),
Priority ENUM('Urgent', 'High', 'Normal', 'Low'),
Creator SMALLINT UNSIGNED DEFAULT '0' NOT NULL,
Assignee SMALLINT UNSIGNED,
`Starting` DATETIME,
Deadline DATETIME,
Completion DATETIME,
PRIMARY KEY(ID)
) ENGINE = INNODB;
MySQL Reserved Keyword List
Starting is a reserved keyword. Perhaps you could use a different one.
I cannot find this error in the sql, could anyone help?
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 'ID MEDIUMINT(9) NOT NULL AUTO_INCREMENT, datetimeTIMESTAMP, title TEXT NOT' at line 1
CREATE TABLE tableName1 (
ID MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
timestamp TIMESTAMP NOT NULL,
title TEXT NOT NULL,
titleEdited TEXT,
description TEXT NOT NULL,
descriptionEdited TEXT,
URL VARCHAR(255) NOT NULL,
URLEdited VARCHAR(255),
imgPath VARCHAR(255) NOT NULL,
PRIMARY KEY (ID),
UNIQUE KEY (URL, imgPath)
)
CREATE TABLE tableName2 (
ID MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
linkID MEDIUMINT(9) NOT NULL,
timestamp TIMESTAMP NOT NULL,
comment TEXT NOT NULL,
userID BIGINT(20) NOT NULL,
PRIMARY KEY(ID),
FOREIGN KEY(linkID) REFERENCES tableName1(ID),
FOREIGN KEY(userID) REFERENCES users(ID)
)
I think the only thing that is wrong here is that mySQL needs a semicolon after each execution. You should just have to end each create table with a ; and this should work
Here is a SQL Fiddle to prove that. (Minus the FK to users since that table is not in your example)
Are you sure you are creating this tables and getting error?
I created them succesfuly.
And with looking error, it seems you are trying to create another field named datetime which type is timestamp? datetime is reserverd word. You need to use backticks.
'ID MEDIUMINT(9) NOT NULL AUTO_INCREMENT, datetimeTIMESTAMP, title TEXT NOT' at line 1