Mysql alter table error message - mysql

I am trying to alter MySQL table to add columns to the end of existing columns. But for some reasons MySQL does not like the query I am running.
Here is my query below.
alter table hdds add
hdd1 VARCHAR(100),
hdd2 VARCHAR(100),
hdd3 VARCHAR(100),
hdd4 VARCHAR(100),
hdd5 VARCHAR(50),
hdd6 VARCHAR(100),
hdd6 VARCHAR(100),
hdd7 VARCHAR(100),
hdd8 VARCHAR(100),
hdd9 VARCHAR(100),
hdd10 VARCHAR(100),
hdd11 VARCHAR(100),
hdd12 VARCHAR(100),
hdd13 VARCHAR(100),
hdd14 VARCHAR(100),
hdd15 VARCHAR(100),
hdd16 VARCHAR(100),
hdd17 VARCHAR(100),
hdd18 VARCHAR(100),
hdd19 VARCHAR(100),
after comments;
I am getting following error message.
#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 'hdd2 VARCHAR(100),
hdd3 VARCHAR(100),
hdd4 VARCHAR(100),
hdd5 ' at line 3
Don't know where I have gone wrong.

works like a charm:
create table t111
( id int auto_increment primary key,
comments varchar(1000) not null
);
alter table t111
add column hdd19 VARCHAR(100) after comments,
add column hdd18 VARCHAR(100) after comments,
add column hdd17 VARCHAR(100) after comments,
add column hdd16 VARCHAR(100) after comments,
add column hdd15 VARCHAR(50) after comments,
add column hdd14 VARCHAR(100) after comments,
add column hdd13 VARCHAR(100) after comments,
add column hdd12 VARCHAR(100) after comments,
add column hdd11 VARCHAR(100) after comments,
add column hdd10 VARCHAR(100) after comments,
add column hdd9 VARCHAR(100) after comments,
add column hdd8 VARCHAR(100) after comments,
add column hdd7 VARCHAR(100) after comments,
add column hdd6 VARCHAR(100) after comments,
add column hdd5 VARCHAR(100) after comments,
add column hdd4 VARCHAR(100) after comments,
add column hdd3 VARCHAR(100) after comments,
add column hdd2 VARCHAR(100) after comments,
add column hdd1 VARCHAR(100) after comments;
describe t111;
Plus you had a typo, trying to add hdd6 twice.
Edit:
I reversed the order of the columns from 19 to 1 so they all lined up after the comments bubbling them down (as seen in the describe t111;)
As I see it, you have 2 choices to line up the ordering visually after comments from 1 to 19.
1. You can do it the way I did, with 19 first after comments, then 18 after comments (shoving down 19 below it), then 17 after comments shoving down 18 and 19 ... 1 after comments shoving down 2 thru 19.
or
2. You can do it 1 thru 19, and have to modify the after comments chunk individually in the `comments part' and it would look like this:
create table t111
( id int auto_increment primary key,
comments varchar(1000) not null
);
alter table t11
add column hdd1 VARCHAR(100) after comments,
add column hdd2 VARCHAR(100) after hdd1,
add column hdd3 VARCHAR(100) after hdd2,
...
add column hdd19 VARCHAR(100) after hdd18;
I chose choice 1., maybe not the best and fastest, but it satisfied my thirst for bubble-down curiosity.
And all of this hinges on anyone caring in the least how they look, but in your case it probably should, since you have so many and error-prone.

NO, you can't add multiple columns like that in a single ALTER statement. That's wrong as it's not same as CREATE statement and so the error you are receiving. You will have to make it separate ALTER statement like
alter table hdds add hdd1 VARCHAR(100) after comments;
alter table hdds add hdd2 VARCHAR(100) after comments;

Related

Getting error in my mysql code: You have an error in your SQL syntax

I am trying to solve error but i dont have any idea how to solve please help me.
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 'create table jobseeker(j_id first_name varchar(20),last_name varchar(20),phone_' at line 5
create table admin(primary key,admin_name varchar(20),admin_password varchar(20));
create table jobseeker(j_id int primary key,first_name varchar(20),last_name varchar(20),phone_no int(11),email_id varchar(20),address1 varchar(30), address2 varchar(30),qualification varchar(20),birthday_date date,gender varchar(10),field varchar(50),year of passing int(11), board varchar(20),zip int(11),city varchar(20),state varchar(20),username varchar(20),password varchar(20));
create table job seeker job(id int primary key,j_id int(10),c_name varchar(20),post varchar(20),foreign key references(j_id int foregin key references job seeker(id)));
create table company news(n_id int primary key,c_id int(11)primary key,information varchar(50),link varchar(50));
create table company(c_id int primary key,c_name varchar(20),phone_no int(11),address varchar(20),email_id varchar(20),password varchar(20));
Well, you have some errors, but as Bill Karwin said, run SQL statemets one at a time.
In your first statement you dont have a name and a type for your primary key.
create table admin(id int primary key,admin_name varchar(20),admin_password varchar(20));
In your second statement you have a column named 'year of passing'. It's not allowed, you should use underscore.
create table jobseeker(j_id int primary key,first_name varchar(20),last_name varchar(20),phone_no int(11),email_id varchar(20),address1 varchar(30), address2 varchar(30),qualification varchar(20),birthday_date date,gender varchar(10),field varchar(50),year_of_passing int(11), board varchar(20),zip int(11),city varchar(20),state varchar(20),username varchar(20),password varchar(20));
Again, check every statement and I recommend you to use some editor with syntax highlighting.
Because j_id did not have the data type. Which is int.
Also, the year of passing int(11) can not have spaces. Well, it can f that is what you really want to do. I will recomend you do not. Rather use underscore "_"
Write it like below if you need your spaces for your column name: year of passing` int(11)
create table jobseeker(
j_id int primary key,
first_name varchar(20),
last_name varchar(20),
phone_no int(11),
email_id varchar(20),
address1 varchar(30),
address2 varchar(30),
qualification varchar(20),
birthday_date date,
gender varchar(10),
field varchar(50),
`year of passing` int(11),
board varchar(20),
zip int(11),
city varchar(20),
state varchar(20),
username varchar(20),
password varchar(20)
);
notice that I also have spaces for year of passing, this is not recommended. I recommend that you one of the following for that column.
year_of_passing int(11)
or
yearOfPassing int(11)

adding constrain to a column

Can anyone please help me with writing a query to add constraint on BestFigure column with "%/%" i.e. it should be 3/10 format.
Please refer the image.
create table Player(
Player_No int Identity(1,1) Primary Key, Player_Name Varchar(20) Not null,
Category Varchar(20) check (Category='batsman' or Category='bowler' or Category='Allrounder'),
BestFigure Varchar(10) check (Bestfigure like'%/%'))
I think a better solution is to store the two figures separately and then combine them:
BestFigure_left int,
BestFigure_right int,
BestFigure varchar(10) generated always as (concat(BestFigure_Left, '/', BestFigure_Right))
MySQL does not actually enforce check constraints (which is why a trigger is needed). If it did, you would do:
BestFigure Varchar(10) check (Bestfigure regexp '^[0-9]{1,3}/[0-9]{1,6}$')
Or something like that. I am unclear what the "3" and "10" mean in your description.

I have to write an SQL statement to create a table skill using DDL commands. the field description can be NULL and the default value is NULL

CREATE TABLE skill
(
id BIGINT(20),
description VARCHAR(255) DEFAULT NULL,
name VARCHAR(255),
PRIMARY KEY(id)
);
The portal says wrong answer. I don't understand what is wrong in this code.
After execution, it says'0 rows affected'.
DDL statements do not affect any records, they affect objects, so a successful execution of a DDL statement is expected to return "0 rows affected".
Your DDL seems correct, although explicitly stating default null is a tad overly robust. Many (most?) DBAs I've worked with would omit it and simply use something like this:
CREATE TABLE skill
(
id BIGINT(20),
description VARCHAR(255),
name VARCHAR(255),
PRIMARY KEY(id)
);
Try this.
CREATE TABLE skill
(
id BIGINT(20) NOT NULL,
description VARCHAR(255),
name VARCHAR(255),
PRIMARY KEY(id)
);
CREATE table skill
(
id BIGINT(20),
description varchar(255) NULL,
name varchar(255) NOT NULL,
primary key(id)
);
Please use the above query.

#1064 -You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

I'm new to PHP and MySQL and ran into a little trouble with a learning project I'm working on.
Whenever I try to create a table
CREATE TABLE transactions(
id int NOT NULL AUTO_INCREMENT,
location varchar(50) NOT NULL,
description varchar(50) NOT NULL,
category varchar(50) NOT NULL,
amount double(10) NOT NULL,
type varchar(6) NOT NULL,
notes varchar(512),
receipt int(10),
)
I get the following error message:
#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 ') NOT NULL, type varchar(6) NOT NULL, notes varchar(512),
receipt int(10), ' at line 6**
Here is some info on what I'm working with
Server type: MySQL
Server version: 5.5.32 - MySQL Community Server(GPL)
phpMyAdmin: 4.0.4.1, latest stable version: 4.1.7
I've spent a day knocking my head against the wall over this and now I think its time to ask for help.I was wondering if anyone can tell me what I'm doing wrong?
Remove the comma
receipt int(10),
And also AUTO INCREMENT should be a KEY
double datatype also requires the precision of decimal places so right syntax is double(10,2)
One obvious thing is that you will have to remove the comma here
receipt int(10),
but the actual problem is because of the line
amount double(10) NOT NULL,
change it to
amount double NOT NULL,
In MySQL, the word 'type' is a Reserved Word.
I see two problems:
DOUBLE(10) precision definitions need a total number of digits, as well as a total number of digits after the decimal:
DOUBLE(10,8)
would make be ten total digits, with 8 allowed after the decimal.
Also, you'll need to specify your id column as a key :
CREATE TABLE transactions(
id int NOT NULL AUTO_INCREMENT,
location varchar(50) NOT NULL,
description varchar(50) NOT NULL,
category varchar(50) NOT NULL,
amount double(10,9) NOT NULL,
type varchar(6) NOT NULL,
notes varchar(512),
receipt int(10),
PRIMARY KEY(id) );
I've faced this problem before, the reason behind that for me was the last comma in the schema definition should be deleted if there's no extra columns would be added.
CREATE TABLE users
(
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL, // this comma should be deleted
);
Rule 1: You can not add a new table without specifying the primary key constraint[not a good practice if you create it somehow].
So the code:
CREATE TABLE transactions(
id int NOT NULL AUTO_INCREMENT,
location varchar(50) NOT NULL,
description varchar(50) NOT NULL,
category varchar(50) NOT NULL,
amount double(10,9) NOT NULL,
type varchar(6) NOT NULL,
notes varchar(512),
receipt int(10),
PRIMARY KEY(id));
Rule 2: You are not allowed to use the keywords(words with predefined meaning) as a field name.
Here type is something like that is used(commonly used with Join Types).
So the code:
CREATE TABLE transactions(
id int NOT NULL AUTO_INCREMENT,
location varchar(50) NOT NULL,
description varchar(50) NOT NULL,
category varchar(50) NOT NULL,
amount double(10,9) NOT NULL,
transaction_type varchar(6) NOT NULL,
notes varchar(512),
receipt int(10),
PRIMARY KEY(id));
Now you please try with this code.
First check it in your database user interface(I am running HeidiSQL, or you can try it in your xampp/wamp server also)and make sure this code works. Now delete the table from your db and execute the code in your program.
Thank You.

Copy/duplicate SQL row with blob/text, How do that?

I would like to copy a SQL's row into the same table.
But in my table, I've a 'text' column.
With this SQL:
CREATE TEMPORARY TABLE produit2 ENGINE=MEMORY SELECT * FROM produit WHERE pdt_ID = 'IPSUMS';
UPDATE produit2 SET pdt_ID='ID_TEMP';
INSERT INTO produit SELECT * FROM produit2;
DROP TABLE produit2;
I get this error :
#1163 - The used table type doesn't support BLOB/TEXT columns
Here is my table :
pdt_ID varchar(6)
pdt_nom varchar(130)
pdt_stitre varchar(255)
pdt_accroche varchar(255)
pdt_desc text
pdt_img varchar(25)
pdt_pdf varchar(10)
pdt_garantie varchar(80)
edit_ID varchar(7)
scat_ID int(11)
pdt_asso1 char(3)
pdt_asso2 char(3)
pdt_online tinyint(4)
It's possible to help me to duplicate row ? How?
You can't store TEXT-columns (which really are blobs) in memory tables. See here
Depending on your ultimate goal, you may insert a md5-hash of the TEXT-column instead to preserve entity identity. Otherwise you need to put pdt_desc and such into another table and refer to it's primary key - that will save you some storage/memory too.