Insert Column Name into Table if Column Name does not already exist - mysql

IF COL_LENGTH('Characters', 'name') IS NULL
BEGIN
ALTER TABLE `Characters` ADD `name` INT(32) UNSIGNED NOT NULL;
END
I am trying to write some sql that will insert a column name into the table "Characters" if it is not already existant, however I am getting errors which I do not understand (quite new to SQL) and I could use some help understanding why it is not working. I have gotten the IF part from another Question, and the ALTER part from the DBMS I'm using, PhpMyAdmin 2.11.4.
It is using MySQL 5.6 apparently and this is the error:
MySQL said: Documentation
#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 'IF COL_LENGTH('Characters', 'name') IS NULL
BEGIN
ALTER TABLE `Characters` AD' at line 1

Related

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

I am trying to execute a PL/SQL statement in MySQL. but, when I try to create a table it shows a syntax error. I get the following error
CREATE TABLE supplier(supid NUMBER(5) PRIMARY KEY, suppname VARCHAR2(15));
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 'NUMBER(5) PRIMARY KEY, suppname VARCHAR2(15))' at line 1
full code is as follows:
DROP TABLE supplier;
DROP SEQUENCE practical2_sequence;
*/
CREATE TABLE supplier(supid NUMBER(5) PRIMARY KEY, suppname VARCHAR2(15));
CREATE SEQUENCE practical2_sequence START WITH 1 MAXVALUE 100;
BEGIN
LOOP
INSERT INTO supplier VALUES(practical2_sequence.NEXTVAL, 1);
EXIT WHEN practical2_sequence.CURRVAL >= 100;
END LOOP;
END;
/
SELECT * FROM supplier;
Help me, as I am not that experienced in MySQL.
You seem to be trying to use Oracle syntax with MySQL.
MySQL doesn't have sequences, it uses the AUTO_INCREMENT attribute of the column. When inserting, either assign NULL to the column or leave it out of the column list, and it will get the next value in the sequence.
MySQL doesn't have the NUMBER datatype, it uses INT. It doesn't have VARCHAR2, just VARCHAR.
DECLARE counter INT DEFAULT 0;
DROP TABLE IF EXISTS supplier;
CREATE TABLE supplier(
supid INT(5) PRIMARY KEY AUTO_INCREMENT,
suppname VARCHAR(15)
);
WHILE counter < 100 DO
INSERT INTO supplier (suppname) VALUES('1');
END WHILE;
Note that variable declarations and WHILE loops can only be used inside stored procedures, not as ordinary queries.

mysql error for a create table statement from shell client

Im trying to run the following from the mysql shell client:
MySQL localhost:33060+ ssl plaything SQL > CREATE TABLE areas (id SERIAL PRIMARY KEY, area_name VARCHAR);
and keep getting the following error:
ERROR: 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 ')' at line 1
where could I be going wrong?
solved by yoss:
try this: CREATE TABLE areas ( id int NOT NULL AUTO_INCREMENT, area_name varchar(255), PRIMARY KEY (id) );
varchar did not have number of characters specified in the original statement

sql create table errors MySQL server version

write a sql sentence to create a table in MySQL
CREATE TABLE fb_web_user_feeds(id bigint not null primary key auto_increment,host_id bigint,,author varchar(64),time varchar(64),user_feed text)
BUT IT ERRORS:
pymysql.err.ProgrammingError: (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 'author varchar(64),time varchar(64),user_feed text)' at line 1")
could you please tell me the reason
It's because you have 2 commas here:
host_id bigint,,author varchar(64)
It should just be 1 comma making your syntax be:
CREATE TABLE fb_web_user_feeds(id bigint not null primary key auto_increment,host_id bigint,author varchar(64),time varchar(64),user_feed text)

Unable to create table with column name "schemas" [duplicate]

This question already has answers here:
Is "key" a reserved word in MySqli? I'm getting an error
(2 answers)
Closed 6 years ago.
When I tried to execute following statement.
create table user_schemas (user_id varchar(255) not null, schemas varchar(255));
I am getting following 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 'schemas varchar(255))' at line 1
When I change the column name "schemas" to something else, it is working fine.
mysql> create table user_schemas (user_id varchar(255) not null, schemas varchar(255));
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 'schemas varchar(255))' at line 1
mysql>
mysql> create table user_schemas (user_id varchar(255) not null, rules varchar(255));
Query OK, 0 rows affected (0.01 sec)
Any idea on how to solve this problem??
If you want to use reserved words in the SQL syntax you must use Backticks (`) like:
create table user_schemas (
user_id varchar(255) not null,
`schemas` varchar(255)
);

How to alter an existing column to default to the current time on insert, in MySQL?

I'm trying to change the existing column "time_sent" to default to the time during insertion. My SQL is:
ALTER TABLE `email_history` alter `time_sent` set DEFAULT CURRENT_TIMESTAMP
I'm getting this error though:
MySQL said:
#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 'CURRENT_TIMESTAMP' at line 1
I've read the documentation and other (similar, but not identical) examples, and no luck.
My version of MySQL is 5.0.67 I believe.
It should be ..
ALTER TABLE 'table' MODIFY collumn_1 TIMESTAMP DEFAULT CURRENT_TIMESTAMP;