php mysql "unique" error when table is created - mysql

So I have this:
CREATE TABLE activeSessions (
id VARCHAR NOT NULL UNIQUE,
claimtime int,
mp FLOAT
);
When I put this into the database, this comes up:
#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 'NOT NULL UNIQUE,
claimtime int,
mp FLOAT
)' at line 2
How do I fix this?

You need to specify the constraint after the column declaration:
CREATE TABLE activeSessions (
id VARCHAR(10) NOT NULL,
claimtime int,
mp FLOAT,
UNIQUE(id)
);
Remember to give an INT value to the VARCHAR column.

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.

mysql error referring to Null

$sql1= "CREATE TABLE bookapp(
id int NOT NULL AUTO_INCREMENT,
name VARCHAR(30),
pnumber int(11),
emailID VARCHAR(50),
reg_date datetime()
PRIMARY KEY(id)
)";
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 ''', NULL)' at line 1
You can use the following to create the table:
CREATE TABLE bookapp(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30),
pnumber INT(11),
emailID VARCHAR(50),
reg_date DATETIME,
PRIMARY KEY(id)
);
Explanation:
( and ) on DATETIME is invalid. DATETIME doesn't need a length.
You have to seperate the column definitions with ,. You forgot this after column reg_date.
Hint (to your error message):
Your given CREATE TABLE throws another error message, that is different to yours:
Error Code: 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 ') PRIMARY KEY(id) )' at line 6

how to add identity column for a table using mysql server 5.6?

i am trying to create a table with an identity column. i used the below query.
using mysql server 5.6.
create table t1
{
id int identity(1,1) Primary key
name varchar2(10)
};
but it shows the below 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 int identity(1,1) Primary key name
varchar2(10) }' at line 2
kindly help me with the syntax.
use () instead of {}
Here the sample
http://www.w3schools.com/php/php_mysql_create_table.asp
Your answer should be
CREATE TABLE t1
(
id INTEGER AUTO_INCREMENT PRIMARY KEY,
name VARCHAR2(10)
)
Refer from https://bytes.com/topic/mysql/answers/652076-how-create-identity-column-mysql

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

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
);