I tried to setup a table in my database:
CREATE TABLE products(
id INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
info TEXT,
price DOUBLE(10)
);
and got the error #1064, but I think there is no error in my SQL syntax.
This is due to Wrong Datatype specification while creating Table.
You can use FLOAT(10) if required, if you want to use Double, you have to mention the precision value as DOUBLE(10,0).
Related
I am trying to make two database table and linked them together. Here is my code:
CREATE TABLE `NYSE_daily_prices_A` (
`StockSymbol` varchar(10) NOT NULL ,
`StockName` varchar(100) NOT NULL ,
`StockExchange` varchar(10) NOT NULL ,
PRIMARY KEY (
`StockSymbol`
)
);
CREATE TABLE `NYSE_stock_names` (
`StockExchange` varchar(10) NOT NULL ,
`StockSymbol` varchar(10) NOT NULL ,
`date` varchar(10) NOT NULL ,
`StockPriceOpen` money NOT NULL ,
`StockPriceHigh` money NOT NULL ,
`StockPriceLow` money NOT NULL ,
`StockPriceClose` money NOT NULL ,
`StockVolume` int NOT NULL ,
`StockPriceAdjClose` money NOT NULL
);
ALTER TABLE `NYSE_stock_names` ADD
CONSTRAINT `fk_NYSE_stock_names_StockSymbol`
FOREIGN KEY(`StockSymbol`)
REFERENCES `NYSE_daily_prices_A` (`StockSymbol`);
I tried to run this script in SQLiteStudio, it reports an error as following:
However, i copied the script into SQL Fiddle and it reports as following:
It really confuses me. How to fix this issue? Thanks in advance.
From SQLite's ALTER TABLE:
The ALTER TABLE command in SQLite allows these alterations of an
existing table: it can be renamed; a column can be added to it; or a
column can be dropped from it.
This means that with SQLite you can't add a foreign key constraint to an existing table.
Also, there are no varchar or money data types (they are interpreted as TEXT and NUMERIC affinities respectively), but this is not the source of the error.
In Mysql there is no money data type.
You can read this thread for an alternative: Best data type to store money values in MySQL
Your code, if you remove the backticks, is valid in SQL Server and Postgresql but not in MySql and SQLite.
I have the next code of an sql query, but i need to adapt it to mysql query, but it gives me an error the the value "number", and i don't know how to change it correctly, if someone knows how can this code work i will appreciate you to answer
Create table SalarioBase(
IdSalario number constraint pk_salariobase primary key,
Salario number)
Change your code to this
Create table SalarioBase(
IdSalario int constraint pk_salariobase primary key,
Salario int)
You can change int to float ... etc. if you need.
Follows T-SQL92,number is not a basic data type
As far as I know, also your code does not work on ms-sql
you may try:
CREATE TABLE `SalarioBase` (
`IdSalario` INT NOT NULL COMMENT '',
`Salario` INT NULL COMMENT '',
PRIMARY KEY (`IdSalario`) COMMENT '');
I try to create a table but I keep getting an SQL Error but I can't figure out why.
This is the error:
check the manual that corresponds to your MySQL server version for the
right syntax to use near '+491634170770 (Id INT NOT NULL PRIMARY KEY
AUTO_INCREMENT,Type VARCHAR(20),Conte' at line 1 ERROR 1064 (42000):
You have an error in your SQL syntax;
This is my statement:
CREATE TABLE +491234175789 (
Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Type VARCHAR(20),
Content MEDIUMBLOB
);
I already tried to find a solution here, but my syntax seems to be correctly. I think its because of the name of the table. But using backticks like this ´+491234175789´ didn't work.
This is the backtick `:
CREATE TABLE `+491234175789` (
Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Type VARCHAR(20),
Content MEDIUMBLOB
);
However, don't create a table name that requires backticks. It is just bad form and makes queries harder to read and write -- you are creating problems for the future. Call it something like:
CREATE TABLE t_491234175789 (
Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Type VARCHAR(20),
Content MEDIUMBLOB
);
I am very new to SQL and having problems with creating a table structure.
I want to create a table with four columns - id,text,date and replace. Problem is this is giving me an error in MySQL, I think because the words TEXT and DATE are also names for data types and REPLACE is another term used in SQL so MySQL is getting confused about what my column names should be or doesn't realise the names I've given are actual names. Is there any way I can get around this to get the column names I want, without having to call the columns something else and then change them back manually once created?
Here's my SQL:
CREATE TABLE message (
id INT UNIQUE AUTO_INCREMENT NOT NULL,
text TEXT,
date INT,
replace INT DEFAULT 0
);
And the error I'm getting:
#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 'replace INT DEFAULT 0 )' at line 5
CREATE TABLE message(
id INT UNIQUE AUTO_INCREMENT NOT NULL ,
TEXT TEXT,
DATE INT,
REPLACE INT DEFAULT 0
);
CREATE TABLE message (
`id` INT UNIQUE AUTO_INCREMENT NOT NULL,
`text` TEXT,
`date` INT,
`replace` INT DEFAULT 0
);
General rule is don't use these keywords, come up with something different than 'text' for your field name. If you must, use ` (back-tick) around the column name to tell SQL it's a column name and not what the keyword means. I recommend against calling your columns 'from' 'select' and 'where' as well ;)
I don't seem to be able to get my SELECT statement to work.
This is the table:
CREATE TABLE clients(
user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
email VARCHAR (70) NOT NULL,
mobile_number int(11) NOT NULL,
UNIQUE KEY (email)
);
The Select Query
SELECT user_id FROM clients WHERE email='info#candy.co.uk';
Whenever I try using this SELECT statement from mysqlADMIN it returns null; this happens even when I enter an email address that I know is in the database.
I would really appreciate some advice on where I am going wrong.
Try the statement without the "WHERE" clause. If it returns the entire table you have narrowed it down to an error in your "email" string.
If it returns nothing and you know there is data in this table then check your connection string and make sure you are using the correct DB.
I think there is some error in your SQL create statement. You should make the unique key to which you have applied auto increment. In this case the database will give an error.
Please try the following creation statement
CREATE TABLE clients(
user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
email VARCHAR (70) NOT NULL,
mobile_number int(11) NOT NULL
);