Debugging MySQL CREATE TABLE from existing table statement? - mysql

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.

Related

I need to create a null value in SQL

I am tasked to create a column with a null value but I got an error.
SELECT * FROM test.Persons
ALTER TABLE Persons
ADD mark_percentage FLOAT
This is my error.
12:32:47 use database test ALTER TABLE PERSONS ADD MARKS float SELECT * FROM test.Persons 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 'database test ALTER TABLE PERSONS ADD MARKS float SELECT * FROM test.Persons' at line 1 0.063 sec
Try this :
ALTER TABLE Persons
ADD mark_percentage FLOAT

setting a constraint

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.

SQL Query having trouble creating temporary table

I am trying to use the my table adult3 to create a temporary table with rows from the column class that correspond to the condition:
SELECT class INTO #CLTable
FROM adult3
WHERE (class = '<=50K');
but I keep getting the 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
'FROM adult3
WHERE (class = '<=50K')' at line 2
I don't get what I'm doing wrong.
I believe that you are using SQL Server syntax for creating a temp table.
Try this:
create temporary table CLTable as
select class from adult3
where (class='<=50K');
You could try:
INSERT INTO [tempTableName]
SELECT class FROM adult3
WHERE (class='<=50k')

MySQL Can't create Table i don't know why

I always get this error when i try to create a Table:
Operation failed: There was an error while applying the SQL script to the database.
Executing:
CREATE TABLE wolveserver.new_table (
);
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 '.new_table (
)' at line 1
SQL Statement:
CREATE TABLE wolveserver.new_table (
)
I gotta say i'm a total Noob, but i wanna fix this.
You will need to define your column inside the parenthesis:
CREATE TABLE wolveserver.new_table
(col1 int,
col2 varchar(50)
);
This is rather a basic question, so you should try to google this before asking a question here in SO. To learn more about CREATE TABLE syntax for MySQL, visit here.

SQL syntax error maybe because of the version I use

On phpMyAdmin, when I create this table the SQL is executed correctly but when I add all the sql code
I get an error saying
"#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 'CREATE TABLEbuying(CustomerIDint(10) unsigned NOT NULL,PurchaseI' at line 14`"
Why is that and how can I fix it ?
You have to use a semicolon ; after each CREATE TABLE sometable ( ) statement.
CREATE TABLE `table1` (
...
);
CREATE TABLE `table2` (
...
);
If you are running multiple SQL statements, they need to be ended with a ; (semi-colon)