> 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.
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 am trying to build my table in MySQL following this answer. Hower i keep getting errors in MySQL even launching the same query used as example in the answer
this query is from documentation and works correctly
CREATE TABLE triangle (
sidea DOUBLE,
sideb DOUBLE,
sidec DOUBLE AS (SQRT(sidea * sidea + sideb * sideb)) stored
);
this query comes from the answer i've linked and it gives me an error
CREATE TABLE IF NOT EXISTS MyTable (
id int NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
description varchar(50) NOT NULL,
slug text NOT NULL AS (SHA2(CONCAT(name, description), 256) STORED,
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8;
this is the query i am trying to execute based on the answer which is giving me also an error
CREATE TABLE IF NOT EXISTS myTable(
id BIGINT NOT NULL AUTO_INCREMENT,
first VARCHAR(104) NOT NULL DEFAULT '',
second DATETIME NULL,
third DATETIME NULL,
fourth VARCHAR(255) NULL,
table_key varchar(64) NOT NULL AS (SHA2(concat_ws(',',first, second, third, fourth), 256)) stored unique,
PRIMARY KEY (id),
INDEX (first));
can you help me figuring out why it's not working?
Note that SHA2(concat_ws(',',first, second, third, fourth), 256) this works in a normal select statement
EDIT
this is the error i am getting
Query execution failed
Reason:
Errore SQL [1064] [42000]: (conn:28) 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 'AS (SHA2(CONCAT(name, description), 256) STORED,
PRIMARY KEY (id)
) DEFAULT ' at line 5
Query is : CREATE TABLE IF NOT EXISTS MyTable (
id int NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
description varchar(50) NOT NULL,
slug text NOT NULL AS (SHA2(CONCAT(name, description), 256) STORED,
PRIMARY KEY (id)
) DEFAULT CHARSET=utf8
You have to add the NOT NULL after the AS part (computed column definition) of the column like this:
CREATE TABLE IF NOT EXISTS myTable(
id BIGINT NOT NULL AUTO_INCREMENT,
first VARCHAR(104) NOT NULL DEFAULT '',
second DATETIME NULL,
third DATETIME NULL,
fourth VARCHAR(255) NULL,
table_key varchar(64) AS (SHA2(concat_ws(',',first, second, third, fourth), 256)) stored unique NOT NULL,
PRIMARY KEY (id),
INDEX (first)
);
Also have a look at the official document of CREATE TABLE. There you can find the description of the datatype syntax. The definition of the computed column is part of the datatype.
I have to create test table for the course and face an issue with ENUM below:
CREATE TABLE IF NOT EXISTS people (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
Lname varchar(20) DEFAULT NULL,
Fname varchar(20) DEFAULT NULL,
Gender ENUM(‘M’, ’F’),
Specialty ENUM(‘1’, ’2’, ’3’, ’4’),
Grade ENUM (‘I’, ’J’, ’M’, ’S’),
Start_date date DEFAULT NULL,
PRIMARY KEY (id)
);
And it does not work - 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 '‘M’, ’F’),
Specialty ENUM(‘1’, ’2’, ’3’, ’4’),
Grade ENU' at line 5"
Whenever I try to create without ENUM fields - everything is fine. If I try to ALTER TABLE with those ENUMs again - it fails.
What is wrong there?
Please try with below query use ' this quote insteated of `
CREATE TABLE IF NOT EXISTS people_tes (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
Lname varchar(20) DEFAULT NULL,
Fname varchar(20) DEFAULT NULL,
Gender ENUM('M', 'F'),
Specialty ENUM('1', '2', '3', '4'),
Grade ENUM ('I', 'J', 'M', 'S'),
Start_date date DEFAULT NULL, PRIMARY KEY (id) )
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,
Can some one help me in correcting the syntax according to MySQL 5.7?
Table :
CREATE TABLE Employee(
EMPID INT NOT NULL AUTO_INCREMENT,
EMPNAME VARCHAR(20) NOT NULL,
EMPAGE INT NOT NULL,
SALARY BIGINT NOT NULL,
ADDRESS VARCHAR(20) NOT NULL
PRIMARY KEY (ID)
);
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 '(ID)
You forgot comma at the end of this line:
ADDRESS VARCHAR(20) NOT NULL
You dont have a column named ID, you may want EMPID?
Replace
ADDRESS VARCHAR(20) NOT NULL
By
ADDRESS VARCHAR(20) NOT NULL,
There are two problems here:
A primary key clause is it's own clause, and needs to be separated from the previous column definition by a comma.
You don't have an id column - your primary key should (probably) be empid:
CREATE TABLE Employee(
EMPID INT NOT NULL AUTO_INCREMENT,
EMPNAME VARCHAR(20) NOT NULL,
EMPAGE INT NOT NULL,
SALARY BIGINT NOT NULL,
ADDRESS VARCHAR(20) NOT NULL,
PRIMARY KEY (EMPID)
);
I guess this is what you are looking for:
CREATE TABLE Employee(
EMPID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
EMPNAME VARCHAR(20) NOT NULL,
EMPAGE INT NOT NULL,
SALARY BIGINT NOT NULL,
ADDRESS VARCHAR(20) NOT NULL
);