I have SQL CREATE statements for MySQL. They have KEY.
Example :
CREATE TABLE a (
a varchar(25) NOT NULL DEFAULT '',
b varchar(20) NOT NULL DEFAULT '',
KEY location (a)
);
What is the CREATE statement for this table in MSSQL ? The KEY keyword does cause problem.
CREATE TABLE database1.dbo.a (
a nvarchar(25),
b nvarchar(25),
)
GO
CREATE INDEX a_index
ON database1.dbo.a(a)
GO
...change db and schema names
if key is the column name, you could use square brackets to surround it..and then run the sql statement, like this:
[KEY]
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 want to create a table in MySQL with the name "123 Product", with numbers at the beginning and a space in between. I can do this using phpMyAdmin but I want to make this table using PHP. But, unfortunately, I am unable to do this.
You can simply try this: enclose table name with backticks (`)
CREATE TABLE `123 Product`
(
`product_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`product_name` VARCHAR(255) NOT NULL
)
ENGINE = InnoDB;
Hi I have created two tables in a database. one is omconst another one is omstarline using the following sql:
CREATE TABLE "omconst" ([id] INTEGER NOT NULL UNIQUE,
[hr] INTEGER,
[name] TEXT,
[vmag] REAL,
PRIMARY KEY(id)
)
CREATE TABLE [omstarline] ([id] INTEGER NOT NULL UNIQUE,
[begin] INTEGER,
[end] INTEGER,
[name] TEXT,
PRIMARY KEY(id)
)
actually, I want to delete the record of table omconst
if omconst.hr != omstarline.begin
or
omconst.hr != omstarline.end.
How to use the SQL Query to do this? thanks in advance...
You can issue a delete statement using the not exists operator:
DELETE FROM omconst
WHERE NOT EXISTS (SELECT *
FROM omstarline
WHERE omconst.he IN (begin ,end)
I have the following script that I want to convert to SQL Server, but how?
MySQL:
CREATE TABLE mytable (
id INTEGER UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
uri VARBINARY(1000),
INDEX(uri(100))
);
How does this index looks like in MSSQL??
CREATE TABLE mytable (
id INTEGER NOT NULL PRIMARY KEY IDENTITY(1,1),
uri VARBINARY(1000),
--INDEX ???
);
sql-server doesn't have an inline index creation clause in its create table syntax. However, you can do it afterwards:
CREATE TABLE mytable (
id INTEGER NOT NULL PRIMARY KEY IDENTITY(1,1),
uri VARBINARY(1000),
);
CREATE INDEX my_table_uri_ind ON mytable(uri);
EDIT:
To address the comment below, you can use computed columns to gain the effect of indexing only part of your uri:
CREATE TABLE mytable (
id INTEGER NOT NULL PRIMARY KEY IDENTITY(1,1),
uri VARBINARY(1000),
uri100 AS SUBSTRING (uri, 1, 100)
);
CREATE INDEX my_table_uri_ind ON mytable(uri100);
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.