What is the equivalent of MSSQL IDENTITY Columns in MySQL? How would I create this table in MySQL?
CREATE TABLE Lookups.Gender
(
GenderID INT IDENTITY(1,1) NOT NULL,
GenderName VARCHAR(32) NOT NULL
);
CREATE TABLE Lookups.Gender
(
GenderID INT NOT NULL AUTO_INCREMENT,
GenderName VARCHAR(32) NOT NULL
);
CREATE TABLE `Persons` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`LastName` varchar(255) NOT NULL,
`FirstName` varchar(255) DEFAULT NULL,
`Address` varchar(255) DEFAULT NULL,
`City` varchar(255) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=latin1;
This above example uses the AUTO_INCREMENT syntax. You can specify a starting offset specific to the table.
The Increment, however, has to be set globally.
SET ##auto_increment_increment=10;
You can also set a global default for the offset like follows:
SET ##auto_increment_offset=5;
To view your current values, type SHOW VARIABLES LIKE 'auto_inc%';
Related
i want to change database of my simple application from mysql to sqlite, this is my sql command :
CREATE TABLE `Todo` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`Title` varchar(255) DEFAULT NULL,
`Category` varchar(255) DEFAULT NULL,
`State` varchar(255) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
im try to create sqllite but return me this error : Error: near "AUTO_INCREMENT": syntax error how i can fix it ?
Instead of using primary key separately, please mention it with your AUTO INCREMENT key
CREATE TABLE `Todo` (
`Id` integer primary key AUTOINCREMENT,
`Title` varchar(255) DEFAULT NULL,
`Category` varchar(255) DEFAULT NULL,
`State` varchar(255) DEFAULT NULL
)
I have this query in mysql:
DROP TABLE IF EXISTS `ARTICLES`;
CREATE TABLE `ARTICLES` (
`ART_ID` int(11) NOT NULL,
`ART_ARTICLE_NR` varchar(66) NOT NULL,
`ART_SUP_ID` smallint(6) DEFAULT NULL,
`ART_DES_ID` int(11) DEFAULT NULL,
`ART_COMPLETE_DES_ID` int(11) DEFAULT NULL,
`ART_PACK_SELFSERVICE` smallint(6) DEFAULT NULL,
`ART_MATERIAL_MARK` smallint(6) DEFAULT NULL,
`ART_REPLACEMENT` smallint(6) DEFAULT NULL,
`ART_ACCESSORY` smallint(6) DEFAULT NULL,
`ART_BATCH_SIZE1` int(11) DEFAULT NULL,
`ART_BATCH_SIZE2` int(11) DEFAULT NULL,
PRIMARY KEY (`ART_ID`),
KEY `ART_SUP_ID` (`ART_SUP_ID`),
KEY `ART_DES_ID` (`ART_DES_ID`),
KEY `ART_COMPLETE_DES_ID` (`ART_COMPLETE_DES_ID`),
KEY `ART_ARTICLE_NR` (`ART_ARTICLE_NR`(18))
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I want the MSSQL version of this. I also read about the SSMA converter.
IF OBJECT_ID('ARTICLES','U') IS NOT NULL
DROP TABLE ARTICLES;
GO
CREATE TABLE ARTICLES
(
ART_ID INT NOT NULL PRIMARY KEY,
ART_ARTICLE_NR varchar(66) NOT NULL,
ART_SUP_ID INT ,
ART_DES_ID INT ,
ART_COMPLETE_DES_ID INT ,
ART_PACK_SELFSERVICE INT ,
ART_MATERIAL_MARK INT ,
ART_REPLACEMENT INT ,
ART_ACCESSORY INT ,
ART_BATCH_SIZE1 INT ,
ART_BATCH_SIZE2 INT
);
GO
/*
KEY ART_SUP_ID (ART_SUP_ID),
KEY ART_DES_ID (ART_DES_ID),
KEY ART_COMPLETE_DES_ID (ART_COMPLETE_DES_ID),
KEY ART_ARTICLE_NR (ART_ARTICLE_NR(18))
*/
For these indexes you will need to create them in separate SQL Statements. for example SQL Server equivalent of "KEY ART_SUP_ID (ART_SUP_ID)" will be
CREATE INDEX IX_SomeIdexName
ON ARTICLES (ART_SUP_ID)
GO
... and so on for each key statement in your mysql statement.
How to convert MySQL *.sql scripts(statements) to MS SQL SERVER *.sql script(statements)?
For example, here is the MySQL sql statements:
DROP TABLE IF EXISTS `qs_ad_category`;
CREATE TABLE `qs_ad_category` (
`id` smallint(5) unsigned NOT NULL auto_increment,
`alias` varchar(100) NOT NULL,
`type_id` int(10) unsigned NOT NULL,
`categoryname` varchar(100) NOT NULL,
`admin_set` tinyint(3) unsigned NOT NULL default '0',
PRIMARY KEY (`id`)
) TYPE=MyISAM ;
DROP TABLE IF EXISTS `qs_admin`;
CREATE TABLE `qs_admin` (
`admin_id` smallint(5) unsigned NOT NULL auto_increment,
`admin_name` varchar(40) NOT NULL,
`email` varchar(40) NOT NULL,
`pwd` varchar(32) NOT NULL,
`pwd_hash` varchar(30) NOT NULL,
`purview` TEXT NOT NULL,
`rank` varchar(32) NOT NULL,
`add_time` int(10) NOT NULL,
`last_login_time` int(10) NOT NULL,
`last_login_ip` varchar(15) NOT NULL,
PRIMARY KEY (`admin_id`)
) TYPE=MyISAM ;
Sure I cannot execute this script in MS SQL SERVER2012, because some syntax is different. Is there any quick way to convert? Or the best way to do it is still manually rewrite a MS SQL version?
Try this:
use master;
go
if db_id('name_of_the_database')is not null
begin
drop database name_of_the_database;
end
go
create database name_of_the_database;
go
use name_of_the_database;
go
if object_id ('qs_ad_category')is not null
begin
drop table qs_ad_category;
end
create table qs_ad_category
(
id smallint check (id between 1 and 5) NOT NULL IDENTITY(1, 1) primary key,
alias varchar(100) NOT NULL,
type_id int check (type_id between 1 and 10) NOT NULL,
categoryname varchar(100) NOT NULL,
admin_set tinyint check(admin_set between 1 and 3) NOT NULL default '0'
);
go
I am working on magento platform.I face a problem regarding values insertion to specific field: My query run perfect but one specific column not working for any query.I try my best but didn't find why .When i change the column type from int to varchar type it works.This is my table structure.
CREATE TABLE `followupemails_emaillogs` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`schedule_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`email_status` varchar(100) DEFAULT NULL,
`client_name` varchar(250) DEFAULT NULL,
`client_email` varchar(250) DEFAULT NULL,
`followupemails_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1.
the "followupemails_id" column not working in insert and update query.This is one update query where record exist that id(29). UPDATE followupemails_emaillogs SET followupemails_id=5 WHERE id =29.
This is insertion query INSERT INTO followupemails_emaillogs SET followupemails_id=4, schedule_time='2013-10-23 08:10:00', email_status='pending', client_name='ayaz ali'.this works fine on fiddle but not on my sqlyog ? what could be the issue.At last i find query that work perfect
.INSERT INTO followupemails_emaillogs (followupemails_id,schedule_time,email_status,client_name,client_email) VALUES (26,'2013-10-23 08:10:00','pending','ayaz ali','mamhmood#yahoo.com');
Can anyone tell me why set query not working but second query works perfect.so that i can accept his answer.Thanks for all your help
Try like this
To Create,
CREATE TABLE followupemails_emaillogs (
id int(8) NOT NULL AUTO_INCREMENT PRIMARY KEY,
schedule_time datetime DEFAULT NULL,
sent_time datetime DEFAULT NULL,
email_status varchar(100) DEFAULT NULL,
client_name varchar(250) DEFAULT NULL,
client_email varchar(250) DEFAULT NULL,
followupemails_i int(11) DEFAULT NULL,
UNIQUE (id)
)
To Insert,
INSERT INTO followupemails_emaillogs (schedule_time,sent_time,email_status,client_name,client_email,followupemails_i)
VALUES
('2012-05-05','2012-05-06',"sent","sagar","sagar#xxxx.com",2)
the whole query is ok
CREATE TABLE `followupemails_emaillogs` (
`id` int NOT NULL AUTO_INCREMENT,
`schedule_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`email_status` varchar(100) DEFAULT NULL,
`client_name` varchar(250) DEFAULT NULL,
`client_email` varchar(250) DEFAULT NULL,
`followupemails_id` int DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1.
but at the last there is dot which is actually error so remove the dot and create the table
latin1.
so remove the dot sign and not null in id filed use this line by default fields are null so don't use default null
id int (8) AUTO_INCREMENT
CREATE TABLE `followupemails_emaillogs` (
`id` int (8) AUTO_INCREMENT,
`schedule_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`email_status` varchar(100),
`client_name` varchar(250),
`client_email` varchar(250),
`followupemails_id` int,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1
don't need (11) in the sql query for int operator , This get for length of the nvarchar,varchar datatype column only not a int datatype,So change and write int instead of int(11) and int(8)
Try this query instead of your query
CREATE TABLE `followupemails_emaillogs` (
`id` int NOT NULL AUTO_INCREMENT,
`schedule_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`email_status` varchar(100) DEFAULT NULL,
`client_name` varchar(250) DEFAULT NULL,
`client_email` varchar(250) DEFAULT NULL,
`followupemails_id` int DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1.
When I create table, MySQL changes everything except row names.
Example:
CREATE TABLE MY_TABLE
(
table_id int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(table_id),
table_1 varchar(45),
table_2 varchar(45),
table_3 varchar(999),
table_4 varchar(45)
)"
MySQL sets everything primary except table_id and sets everything unique, index and full text.
If I make my custom table everything goes on random mode. Something is primary, something unique.
I'm on shared hosting on asmallorange.com.
CREATE TABLE `MY_TABLE` ( `table_id` int(11) NOT NULL AUTO_INCREMENT, `table_1` varchar(45) DEFAULT NULL, `table_2` varchar(45) DEFAULT NULL, `table_3` varchar(999) DEFAULT NULL, `table_4` varchar(45) DEFAULT NULL, PRIMARY KEY (`table_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1
Here is SHOW CREATE TABLE MY_TABLE
I'm using phpAdmin 3.5.5.
CREATE TABLE `my_table` (
`table_id` int(10) NOT NULL AUTO_INCREMENT,
`title` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`content` longtext COLLATE utf8_unicode_ci NOT NULL,
`created_at` datetime NOT NULL,
PRIMARY KEY (`table_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Here is an example of creating table with different fields.