Is this mysql correctly translated to sql server? - mysql

I have this, for mysql
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(249) COLLATE utf8mb4_unicode_ci NOT NULL,
`password` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,
`username` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`verified` tinyint(1) unsigned NOT NULL DEFAULT '0',
`registered` int(10) unsigned NOT NULL,
`last_login` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
I'm attempting to convert to sql server:
CREATE TABLE users(
id int PRIMARY KEY IDENTITY NOT NULL,
email varchar(255) UNIQUE,
password varchar(255) NOT NULL ,
username varchar(100) DEFAULT NULL,
verified tinyint NOT NULL DEFAULT '0',
registered int NOT NULL,
last_login int DEFAULT NULL,
);
That worked, but I had to omit some things, mostly the Collate and character set. Can you tell me if I missed anything too important and how to translate those things from mysql to sql server?

Some notes:
PRIMARY KEY means NOT NULL.
Constants should be in the same type as their variable.
For generalized character sets, use NVARCHAR().
DEFAULT NULL is redundant (you can keep it if you like).
Hence:
CREATE TABLE users (
id int PRIMARY KEY IDENTITY,
email nvarchar(255) UNIQUE,
password nvarchar(255) NOT NULL ,
username nvarchar(100),
verified tinyint NOT NULL DEFAULT 0,
registered int NOT NULL,
last_login int
);

Related

Transferring table from MySQL 5.7 to 8.0, showing wrong data on a blob column

I am transferring a single table from MySQL 5.7 to MySQL 8.
First, I used mysqldump db_name table_name > table_name.sql in my current server(MYSQL 5.7); taken from here
Then, in my VM with MySQL 8, I used mysql -u username -p db_name < /path/to/table_name.sql.
The columns with the 'blob' data type are not correct, the other columns are okay.
the 'blob' columns went from "International Master (1973); Grandmaster (1975); FIDE Senior Trainer (2004).Alexander Genrikhovich Beliavsky was..." to "0x3C703E496E7465726E6174696F6E616C204D6173746572202831393733293B204772616E646D617374657220...".
Both databases have character-set = latin1 and collation = latin1_swedish_ci.
How can I properly transfer the 'blob' columns from MySQL 5.7 to MySQL 8?
Output when running SHOW CREATE TABLE table_name:
OLD:
CREATE TABLE `Player` (
`pid` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(34) DEFAULT NULL,
`longname` varchar(64) DEFAULT NULL,
`rating` int(10) unsigned NOT NULL DEFAULT '0',
`born` varchar(10) DEFAULT NULL,
`died` varchar(10) DEFAULT NULL,
`country` varchar(10) DEFAULT NULL,
`nationality` varchar(10) DEFAULT NULL,
`gender` enum('M','F') DEFAULT NULL,
`bio` blob,
`fiderating` int(10) unsigned NOT NULL DEFAULT '0',
`rawbio` blob,
`fidenumber` int(10) unsigned DEFAULT NULL,
`fideblitz` int(10) unsigned DEFAULT '0',
`fiderapid` int(10) unsigned DEFAULT '0',
PRIMARY KEY (`pid`),
KEY `name` (`name`),
KEY `rating` (`rating`),
KEY `country` (`country`),
KEY `nationality` (`nationality`),
KEY `gender` (`gender`),
KEY `fidenumber` (`fidenumber`),
FULLTEXT KEY `longname` (`longname`)
) ENGINE=MyISAM AUTO_INCREMENT=168045 DEFAULT CHARSET=latin1
NEW:
| Player | CREATE TABLE `Player` (
`pid` int unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(34) DEFAULT NULL,
`longname` varchar(64) DEFAULT NULL,
`rating` int unsigned NOT NULL DEFAULT '0',
`born` varchar(10) DEFAULT NULL,
`died` varchar(10) DEFAULT NULL,
`country` varchar(10) DEFAULT NULL,
`nationality` varchar(10) DEFAULT NULL,
`gender` enum('M','F') DEFAULT NULL,
`bio` blob,
`fiderating` int unsigned NOT NULL DEFAULT '0',
`rawbio` blob,
`fidenumber` int unsigned DEFAULT NULL,
`fideblitz` int unsigned DEFAULT '0',
`fiderapid` int unsigned DEFAULT '0',
PRIMARY KEY (`pid`),
KEY `name` (`name`),
KEY `rating` (`rating`),
KEY `country` (`country`),
KEY `nationality` (`nationality`),
KEY `gender` (`gender`),
KEY `fidenumber` (`fidenumber`),
FULLTEXT KEY `longname` (`longname`)
) ENGINE=MyISAM AUTO_INCREMENT=168045 DEFAULT CHARSET=latin1 |

How do you design a database schema for a user profile page

Trying to set up a user profile page for a job site. The database I plan to use is the MySQL database.
Looking into a few database design I came up with this schema.
First, the user management tables
CREATE TABLE `user` (
`user_id` int(11) NOT NULL,
`firstname` varchar(32) NOT NULL,
`lastname` varchar(32) NOT NULL,
`email` varchar(96) NOT NULL,
`mobile_number` varchar(32) NOT NULL,
`password` varchar(40) NOT NULL,
`salt` varchar(9) NOT NULL,
`address_id` int(11) NOT NULL DEFAULT '0',
`ip` varchar(40) NOT NULL,
`status` tinyint(1) NOT NULL,
`approved` tinyint(1) NOT NULL,
`registration_date` datetime NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `user_address` (
`user_id` int(11) NOT NULL,
`city` varchar(128) NOT NULL
`work_city` varchar(128) NOT NULL,
`postal_code` varchar(10) NOT NULL,
`country_id` int(11) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `user_description` (
`user_id` int(11) NOT NULL,
`description` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
and then the education table and work experience
CREATE TABLE `education_detail` (
`user_id` int(11) NOT NULL,
`certificate_degree_name` varchar(255) DEFAULT NULL,
`major` varchar(255) DEFAULT NULL,
`institute_university_name` varchar(255) DEFAULT NULL,
`start_date` date NOT NULL DEFAULT '0000-00-00',
`completion_date` date NOT NULL DEFAULT '0000-00-00'
)
CREATE TABLE `experience_detail` (
`user_id` int(11) NOT NULL,
`is_current_job` int(2) DEFAULT NULL,
`start_date` date NOT NULL DEFAULT '0000-00-00',
`end_date` date NOT NULL DEFAULT '0000-00-00',
`job_title` varchar(255) DEFAULT NULL,
`company_name` varchar(255) DEFAULT NULL,
`job_location_city` varchar(255) DEFAULT NULL,
`job_location_state` varchar(255) DEFAULT NULL,
`job_location_country` varchar(255) DEFAULT NULL,
`job_description` varchar(255) DEFAULT NULL
)
Note that user_id in table user_address, user_description, education_detail and experience_detail is a foreign key referencing it to the table user.
There are a few more table like skills, certification etc to which I plan on using user_id as a FK.
My question, is this database design good enough? Can you suggest me what should be done more to make the design much better?
Keep in mind not all will have work experience, some may be freshers.
Use InnoDB, not MyISAM. (There are many Q&A explaining 'why'.)
NULL or an empty string is perfectly fine for a missing description. Do you have any further argument for disliking such? (Meanwhile, InnoDB is more efficient at handling optional big strings.)
Every table should have a PRIMARY KEY; you don't seem to have any. The first table probably needs user_id as the PK. Read about AUTO_INCREMENT.
As with description, why is address in a separate table?
May I suggest this for country name/code/id:
country_code CHAR(2) CHARACTER SET ascii
Education is 1:many from users, so user_id cannot be the PK. Ditto for jobs.

MySQL 5.7 Windows, table key issue?

What is wrong with this MySQL table script code?:
CREATE TABLE `securities_master`.`symbol` (
`id` INT NOT NULL,
`exchange_id` INT NULL,
`ticker` VARCHAR(32) NOT NULL,
`instrument` VARCHAR(64) NOT NULL,
`name` VARCHAR(255) NULL,
`sector` VARCHAR(255) NULL,
`currency` VARCHAR(32) NULL,
`created_date` DATETIME NOT NULL,
`last_updated_date` DATETIME NOT NULL,
PRIMARY KEY (`id`), FOREIGN KEY 'index_exchange_id' ('exchange_id'))
ENGINE = InnoDB AUTO_INCREMENT = 1
DEFAULT CHARACTER SET = utf8;
I think it is the "FOREIGN KEY" but I am not sure
Single quotes denote string literals in SQL. Object names (such as the constraint name and the columns it refers to) should be denoted with backticks, or nothing at all:
CREATE TABLE `securities_master`.`symbol` (
`id` INT NOT NULL,
`exchange_id` INT NULL,
`ticker` VARCHAR(32) NOT NULL,
`instrument` VARCHAR(64) NOT NULL,
`name` VARCHAR(255) NULL,
`sector` VARCHAR(255) NULL,
`currency` VARCHAR(32) NULL,
`created_date` DATETIME NOT NULL,
`last_updated_date` DATETIME NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY `index_exchange_id` (`exchange_id`)
-- Here ----^-----------------^--^-----------^
)
ENGINE = InnoDB AUTO_INCREMENT = 1
DEFAULT CHARACTER SET = utf8;

Field not inserting or updating , int type in sql

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.

why Mysql is giving me error 1280 "Wrong Index"

Can anyone explain why Mysql is giving me error 1280 ("wrong index for 'fk_chart_aid_aid' ") error whend I try to create the "CHART OF ACCOUNTS" table. I'm completly confused here. How can I fix this so I can create the table? The "ACCOUNT" table already exists in the database and has data in it.
Thanks for the help.
MYSQL Server version: 5.1.54
CHART OF ACCOUNTS:
DROP TABLE IF EXISTS `rst`.`acctg_chart_of_accounts` ;
CREATE TABLE IF NOT EXISTS `rst`.`acctg_chart_of_accounts` (
`acctg_chart_of_accounts_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`account_id` INT UNSIGNED NOT NULL ,
`account_nbr` VARCHAR(45) NULL ,
`description` VARCHAR(45) NULL ,
`account_type` INT UNSIGNED NULL ,
`commissionable` TINYINT UNSIGNED NULL ,
`hidden` TINYINT UNSIGNED NULL ,
`deduct_balance_from_owner_check` TINYINT UNSIGNED NULL ,
PRIMARY KEY (`acctg_chart_of_accounts_id`) ,
CONSTRAINT `fk_chart_aid_aid`
FOREIGN KEY (`account_id` )
REFERENCES `rst`.`account` (`account_id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE INDEX `fk_chart_aid_aid` ON `rst`.`acctg_chart_of_accounts` (`account_id` ASC) ;
ACCOUNTS TABLE THAT IS BEING REFERENCED:
CREATE TABLE IF NOT EXISTS `account` (
`account_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`account_status_id` int(10) unsigned NOT NULL,
`company_name` varchar(155) DEFAULT NULL,
`address1` varchar(155) DEFAULT NULL,
`address2` varchar(155) DEFAULT NULL,
`city` varchar(155) DEFAULT NULL,
`state` varchar(155) DEFAULT NULL,
`zip` varchar(45) DEFAULT NULL,
`country` varchar(255) DEFAULT NULL,
`work_phone` varchar(45) DEFAULT NULL,
`mobile_phone` varchar(45) DEFAULT NULL,
`time_zone` varchar(45) DEFAULT NULL,
`subdomain` varchar(155) DEFAULT NULL,
`cname_URL` varchar(255) DEFAULT NULL,
`promotion_code` varchar(45) DEFAULT NULL,
`can_we_contact_you` tinyint(4) DEFAULT NULL COMMENT '0=false, 1=true',
`units_managed_nbr` varchar(10) DEFAULT NULL,
`a_hear_about_us_list_id` tinyint(3) unsigned DEFAULT NULL COMMENT 'populated from dropdown list.',
`receive_special_offers` tinyint(4) DEFAULT NULL,
`receive_announcements` tinyint(4) DEFAULT NULL,
`receive_newsletter` tinyint(4) DEFAULT NULL,
`create_ts` timestamp NULL DEFAULT NULL,
`expires` timestamp NULL DEFAULT NULL,
`storage_capacity` varchar(255) DEFAULT NULL COMMENT '1073741824 = 1GB',
`logo` varchar(455) DEFAULT NULL,
`max_active_connections` int(11) DEFAULT '3',
`_product_id` int(11) DEFAULT NULL,
`report_footer` varchar(455) DEFAULT NULL,
`welcome_dialog` tinyint(4) DEFAULT '1',
`ARB_subscription_id` int(11) DEFAULT NULL,
`trashbin` tinyint(4) NOT NULL DEFAULT '1',
PRIMARY KEY (`account_id`),
KEY `fk_account_account_status_id` (`account_status_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=58 ;
Are you getting the error after the CREATE TABLE statement, or after the subsequent CREATE INDEX?
Looks like you are attempting to name both a FOREIGN KEY constraint and an INDEX fk_chart_aid_aid. Try choosing a different name for either one of them.
Also, in the accounts table, account_id is INT(10). Try also to change the column definition in acctg_chart_of_accounts to:
`account_id` INT(10) UNSIGNED NOT NULL ,
Though, I think that mysql defaults type INT to INT(10) anyway...
I met the same issue; tried manually rename the index to a different name but don't like the idea of 'manually' and neither I don't really understand why we need to generate the index separately. so I decide to generate it within the create statement by unchecking the option of 'generate separate index statement' in 'forwarding engineer', and this fix the issue.