setting a constraint - mysql

ALTER TABLE Customers
ADD COLUMN ZipCode INT CONSTRAINT CHK_ZipCode
CHECK ( [ZipCode] LIKE '[0-9][0-9][0-9][0-9][0-9]')
Error Code: 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 '[ZipCode] LIKE '[0-9][0-9][0-9][0-9][0-9]')' at
line 3
it said '[' not valid , expecting an expression
how do i solve this?

In MySQL, you would express this as:
ALTER TABLE Customers
ADD COLUMN ZipCode INT CONSTRAINT CHK_ZipCode CHECK (ZipCode REGEXP '^[0-9](5}$');
The syntax you are using looks like SQL Server.

Related

Add the column in mysql and has the same data like the column existing

I want to add the column name "payroll_date_on" in the table and has the default value as the other table column named "jo_time_on".
ALTER TABLE jobs ADD COLUMN payroll_time_on INT(11) AS (jo_time_on) PERSISTED;
While running this I got an error code
Error Code: 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 'AS (jo_time_on) PERSISTED' at line 1
Help me to solve this error and clone the column jo_time_on
I am using the mysql version 5.6.38
try this:
ALTER TABLE jobs ADD COLUMN payroll_time_on INT(11);
UPDATE TABLE SET payroll_time_on = jo_time_on;

Trying to apply "Not null" and "default" in MYSQL

I am trying to create a column in a table in MySQL that cannot be a null value, and also has a default value. But when trying to, it just shows an error in syntax:
Create table example (
code int(10),
name varchar(20) not null default('My company')
);
And this is my error message:
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 '('My company')
)' at line 3
Problem seems to be the default part.
I have removed the parentheses and it works for me. Verified in sql fiddle here:
http://sqlfiddle.com/#!9/5e571a

How to solve #1064 error in MySQL?

I got MySQL syntax Error 1064 while I am add new column.
ALTER TABLE `customerdetails` ADD `mobile` DOUBLE(12) NOT NULL ;
where customerdetails is the name of the table.
The Error message 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 ') NOT NULL' at line 1
Can somebody help me to solve this problem?
When you are defining column type as DOUBLE you have to define how many decimal places it will use
ALTER TABLE customerdetails ADD mobile DOUBLE(12,2) NOT NULL ;
Also first number (Characteristic) needs to be bigger than second number (Mantissa)

Debugging MySQL CREATE TABLE from existing table statement?

Why is this giving me an error ?
I haven't found any good example code online that combines CREATE TABLE with a SELECT statement with WHERE.
CREATE TABLE tmp_year (source CHAR(3),
target CHAR(3),
val FLOAT,
PRIMARY KEY (source, target))
(SELECT source,
target,
val
WHERE date>='2001-01-01'
AND date<='2001-12-12')
FROM db;
error message:
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 'WHERE date>='2001-01-01' AND date<='2001-12-12') FROM db' at line 1
Try this my friend:
CREATE TABLE tmp_year AS
SELECT * FROM YOURTABLE
WHERE date>='2001-01-01'
AND date<='2001-12-12';
ALTER TABLE tmp_year ADD PRIMARY KEY(source, target);
Here is example in SQL Fiddle
SQL expects FROM to immediately follow a SELECT clause's values and be before conditionals like WHERE.

Mysql INSERT syntax error #1064

I keep getting the following 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
when I try to run this query in phpMyAdmin:
INSERT INTO access_log (
idStaff, validSession, attempts, remoteHost, remoteAddress, remoteTime, banned,
errorNumber,errorMessage,userName )
VALUES ( '1','1','1','voidDNS','188.25.3.105','1388877754','','','','pinochio';
My table has these columns and their type in this order is (except an auto-increment column 'entry"):
INT, TINYINT, INT, TEXT, VARCHAR(20), BIGINT( I was afraid of using TIMESTAMP), TINYINT, INT, TEXT, VARCHAR(24).
The query is generated by php automatically, this is why every value has quotes, which I understand shouldn't generate an error.
Can anyone see what's wrong here?
Lack simply closing parentheses at the end of query