Creating table on php having error on sql syntax - mysql

The program that I want to build is to create a table in mysql using php. The table name is the value entered in a textbox from a form of a previous page.
I am having a problem stating:
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 '2014( ID_Number INT NOT NULL, PRIMARY KEY (ID_Number), First_Name VARCH' at line 1'
CREATE TABLE $filename(
ID_Number INT NOT NULL,
PRIMARY KEY (ID_Number),
First_Name VARCHAR(50),
Middle_Name VARCHAR(50),
Last_Name VARCHAR(50),
Year INT,
Course VARCHAR(50),
Position VARCHAR(50),
Party VARCHAR(50),
Picture VARCHAR(50),
vote INT,
)

You need to remove the comma after the last column in your create statement
vote INT,

(Error creating table: 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 '2014( ID_Number INT NOT NULL, PRIMARY KEY (ID_Number), First_Name VARCH' at line 1')
Besides other possible errors (as suggested in other answers), above error message returned specifically because you were trying to create a table named 2014 which is not allowed.
"Identifiers may begin with a digit but unless quoted may not consist solely of digits". [MySql Manual : 9.2 Schema Object Names]
You can escape digits only table name by using backticks :
CREATE TABLE `2014`
....

String concatenation is wrong. Try this -
$sql = "CREATE TABLE $filename(".
"ID_Number INT NOT NULL,".
"PRIMARY KEY (ID_Number),".
"First_Name VARCHAR(50),".
"Middle_Name VARCHAR(50),".
"Last_Name VARCHAR(50),".
"Year INT,".
"Course VARCHAR(50),".
"Position VARCHAR(50),".
"Party VARCHAR(50),".
"Picture VARCHAR(50),".
"vote INT".
")";

Related

correct syntax from MSSQL to MySQL

Good Day!
what is the proper syntax query in MySQL? I'm getting error in mysql during execution (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 'EmpCode AS CONCAT('EMP' , RIGHT(Concat('0000', CONVERT(EmpId, ' at line 3)
CREATE TABLE tbEmployee
(
EmpId INT NOT NULL, PRIMARY KEY,
EmpCode AS CONCAT('EMP' , RIGHT(Concat('0000', CONVERT(EmpId, CHAR(5))),5)) PERSISTED,
EmployeeName VARCHAR(50),
Age INT,
Gender VARCHAR(10)
)
MySQL generated columns are either VIRTUAL (the default) or STORED, the latter corresponding to SQL Server's persisted. Try this version:
CREATE TABLE tbEmployee (
EmpId INT NOT NULL PRIMARY KEY,
EmpCode VARCHAR(50) AS (CONCAT('EMP', LPAD(EmpId, 5, '0'))) STORED,
EmployeeName VARCHAR(50),
Age INT,
Gender VARCHAR(10)
);
Note also that MySQL is a bit more lax with regards to casting numeric columns to text. Also, we can use MySQL's LPAD function to left pad the employee ID with zeroes, to a width of 5 digits.

SQL syntax error 'GENERATED ALWAYS AS IDENTITY'

I am trying to upload this file to netbeans however I am unable to create the authors table and continue to receive the following error:
CREATE TABLE authors (
authorID INT NOT NULL GENERATED ALWAYS AS IDENTITY,
firstName varchar (20) NOT NULL,
lastName varchar (30) NOT NULL,
PRIMARY KEY (authorID)
);
INSERT INTO authors (firstName, lastName)
VALUES
('Paul','Deitel'),
('Harvey','Deitel'),
('Abbey','Deitel'),
('Dan','Quirk'),
('Michael','Morgano');
[Warning, Error code 1,064, SQLState 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 'GENERATED ALWAYS AS IDENTITY,
firstName varchar (20) NOT NULL,
lastName va' at line 2
[Exception, Error code 1,064, SQLState 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 'GENERATED ALWAYS AS IDENTITY,
firstName varchar (20) NOT NULL,
lastName va' at line 2
Line 2, column 1
[Warning, Error code 1,146, SQLState 42S02] Table 'books.authors' doesn't exist
I have been searching all over the web for hours and have yet to find an answer! I am new to SQL so I apologize if this is a duplicate question. I am using MYSQL version 5.7.18
There are seval things wrong:
identity is SQL Server syntax, MySQL uses auto_increment
<name> <type> generated always as <calculation> applies to calculated columns.
Try:
CREATE TABLE authors (
authorID INT NOT NULL AUTO_INCREMENT,
firstName varchar (20) NOT NULL,
lastName varchar (30) NOT NULL,
PRIMARY KEY (authorID)
);

Create table error with MySQL

I try to create a table in MySQL:
mysql> create table order ( ID varchar(30) not null,
-> Cname varchar(100) not null,
-> name varchar(30),
-> Type varchar(30),
-> primary key(ID, Cname));
but an error occured:
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 'order ( ID varchar(30) not null, Cname
varchar(100) not null, name varchar(30), ' at line 1
I have checked for thousand time and I still find no error here.
Can anyone help me?
Its is because of the table name order.
There is an order by reserved word. Change the table name and it will work fine.
If you want order as table name use back ticks around the table name. It will workfine

MySQL Create Table Error Syntax

Hello i am having an issue when trying to create a table inside of my database webhostc_MyRadContactForm
When i try to execute the statement below in phpMyAdmin i get this error
CREATE TABLE Contacts (
-> ContactID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> ContactName VARCHAR(100),
-> ContactEmail VARCHAR(100),
-> ContactLeastFavoriteColor VARCHAR(10)
-> ContactDateCreated DATETIME
-> );
#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 '-> ContactID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> ContactName VARCHA' at line 2
Also phpMyAdmin flags these lines:
My server is running: 10.0.22-MariaDB
You are missing a comma just after ContactLeastFavoriteColor VARCHAR(10) and those arrows, ->, are not supposed to be there. The following is the correct syntax for creating your table:
CREATE TABLE Contacts (
ContactID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
ContactName VARCHAR(100),
ContactEmail VARCHAR(100),
ContactLeastFavoriteColor VARCHAR(10),
ContactDateCreated DATETIME
);
Good luck!!
There are two problems:
Those -> symbols aren't part of SQL syntax. They're the prompts that the MySQL monitor prints when you enter a multi-line query. You can't copy them into PhpMyAdmin.
You're missing a comma at the end of the ContactLeastFavoriteColor line.

SQL Expression wrong

I try to create a table but I keep getting an SQL Error but I can't figure out why.
This is the error:
check the manual that corresponds to your MySQL server version for the
right syntax to use near '+491634170770 (Id INT NOT NULL PRIMARY KEY
AUTO_INCREMENT,Type VARCHAR(20),Conte' at line 1 ERROR 1064 (42000):
You have an error in your SQL syntax;
This is my statement:
CREATE TABLE +491234175789 (
Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Type VARCHAR(20),
Content MEDIUMBLOB
);
I already tried to find a solution here, but my syntax seems to be correctly. I think its because of the name of the table. But using backticks like this ´+491234175789´ didn't work.
This is the backtick `:
CREATE TABLE `+491234175789` (
Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Type VARCHAR(20),
Content MEDIUMBLOB
);
However, don't create a table name that requires backticks. It is just bad form and makes queries harder to read and write -- you are creating problems for the future. Call it something like:
CREATE TABLE t_491234175789 (
Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Type VARCHAR(20),
Content MEDIUMBLOB
);