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.
Related
CREATE TABLE Employee (
emp_id SMALLINT PRIMARY KEY,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR (20),
birth_date DATE NOT NULL,
sex VARCHAR (20),
salary INT,
super_id INT,
branch_id SMALLINT
);
ALTER TABLE Employee
ALTER COLUMN sex VARCHAR(1);
I am a beginner so go easy on me, please. I am not able to understand what am I doing wrong.
error:
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 'VARCHAR(1)' at line 2
in mysql you need to use MODIFY
ALTER TABLE Employee
MODIFY COLUMN sex VARCHAR(1);
by the way if you already have data in your table , any value more than 1 character in sex column will be truncated , so you have to take care of that before altering column length
I am trying to create a table, but it gives me 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 touse near ' INT NOT NULL AUTO_INCREMENT
Here is my code:
CREATE TABLE bowling_scores (
PlayerID, INT NOT NULL AUTO_INCREMENT
, LName VARCHAR(100) NOT NULL
, FName VARCHAR(100) NOT NULL
, Game1 INT NULL
, Game2 INT NULL
, Game3 INT NULL
, Game4 INT NULL
, PRIMARY KEY (PlayerID)
);
Here's a tip about syntax error messages: the message tells you where the error is.
check the manual that corresponds to your MySQL server version for the right syntax to use near ' INT NOT NULL AUTO_INCREMENT
This is telling you that MySQL became confused because it wasn't expecting to see that text. This usually means you made a mistake at that point, or immediately before it.
This can help you narrow down your search in the SQL statement to the point where it went wrong.
The error message also tells you what to do: check the manual. You should help yourself become better at SQL by reading documentation and examples.
I've been using MySQL for nearly 20 years, but I still refer to the reference docs every day. In fact, it's because I am experienced with MySQL that I know this is the right thing to do.
All SQL statements have a reference page in the manual. Here's the page for CREATE TABLE: https://dev.mysql.com/doc/refman/5.7/en/create-table.html
The MySQL manual is large, so it's easy to think "I don't know where the right page is." Gradually you can learn how the manual is organized. Pay attention to the hierarchy of links on the left column. When in doubt, just use Google for phrases like "mysql create table syntax".
You should be able to answer simple syntax errors for yourself instead of posting to Stack Overflow. You'll get your answer more quickly!
This is an obvious error:
PlayerID, INT NOT NULL AUTO_INCREMENT
You have an extraneous comma. It should be:
PlayerID INT NOT NULL AUTO_INCREMENT
Stop putting commas on the next line of your SQL statements as well!
This should work. You had a comma after PlayerID
CREATE TABLE bowling_scores (
PlayerID INT NOT NULL AUTO_INCREMENT,
LName VARCHAR(100) NOT NULL,
FName VARCHAR(100) NOT NULL,
Game1 INT NULL,
Game2 INT NULL,
Game3 INT NULL,
Game4 INT NULL,
PRIMARY KEY (PlayerID)
);
You should be writing something like:
creat table Bowling_score(Playerid int, Firstname varchar(100), Lastname varchar(100), Game1 int, Game2 int, Game3 int);
I am getting a SQL query syntax error while creating a table. Here is the SQL query:
CREATE TABLE ACCOUNT (
ACCNO NUMBER(5) NOT NULL,
NAME VARCHAR(20) NOT NULL,
BA L NUMBER(8,2) NOT NULL,
CREATION-DT DATE NOT NULL,
PRIMARY KEY ( ACCNO )
);
Here is the error:
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 'NUMBER(5) NOT NULL.
What is wrong with my syntax?
Using SQLfiddle you can see this is not valid.
You have multiple problems with your syntax; invalid data types, invalid column names, etc. You can encapsulate the invalid names in backticks but then you will have to remember to encapsulate them later.
Instead of Number you probably meant numeric but i would suggest trying Integer instead.
Or just try this:
CREATE TABLE ACCOUNT (
ACCNO INTEGER(5) NOT NULL,
NAME VARCHAR(20) NOT NULL,
BAL INTEGER(8) NOT NULL,
CREATIONDT DATE NOT NULL,
PRIMARY KEY ( ACCNO )
);
NUMBER is not a valid datatype in MySQL, you should choose the one from the list (maybe you've meant NUMERIC).
You should also escape some identifiers (in your case, column names BA L, which includes a space within it, and CREATION-DT, which includes a dash) with backticks ` in MySQL:
CREATE TABLE `ACCOUNT` (
`ACCNO` NUMERIC(5) NOT NULL,
`NAME` VARCHAR(20) NOT NULL,
`BA L` NUMERIC(8,2) NOT NULL,
`CREATION-DT` DATE NOT NULL,
PRIMARY KEY ( `ACCNO` )
);
SQLFiddle
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.
I am having the following error when trying to execute the following code in phpMyAdmin. I am using WAMP Server with MySQL version 5.5.8 ::
The error is:
#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 'NUMERIC(6) not null, payee VARCHAR2(20) not null, amount DECIMAL(6,2) no' at line 3
the SQL is:
CREATE TABLE checks
(
check NUMERIC(6) not null,
payee VARCHAR2(20) not null,
amount DECIMAL(6,2) not null,
remarks VARCHAR2(20) not null
)
EDIT
I am also trying to execute this but it gives also error:
CREATE TABLE deposits
(
deposit NUMBER(8),
whopaid VARCHAR(25),
amount DECIMAL(6,2),
remarks VARCHAR(20)
)
You need to escape check with backticks since it is a reserved word. And in MySQL there is no varchar2 data type, just varchar.
CREATE TABLE checks
(
`check` NUMERIC(6) not null,
payee VARCHAR(20) not null,
amount DECIMAL(6,2) not null,
remarks VARCHAR(20) not null
)
Also replace NUMBER with NUMERIC in your 2nd create statement.
CREATE TABLE deposits
(
deposit NUMERIC(8),
whopaid VARCHAR(25),
amount DECIMAL(6,2),
remarks VARCHAR(20)
)
SQLFiddle example
See MySQL String data types and MySQL numeric data types
check is a reserved work, that's why you have this error
you need to put quotes around it
CHECK is a reserved keyword in mysql. If you choose a different name it should work fine.
http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html