MYSQL到MSSQL代码转换

时间:2016-04-08 12:23:13

标签: mysql sql-server

我有一个来自MYSQL的代码片段,它创建了一个带有约束的2个表,以及一些要插入其中一个表的数据。

我希望等效转换为MSSQL。我尝试了迁移,但它一直给我一个转换错误,我无法弄清楚问题在哪里,因为在mysql这个代码工作正常。

CREATE TABLE `tbl_variable_types`
(
 `variable_type_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `variable_type` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`variable_type_id`),
  UNIQUE KEY `variable_type_id_UNIQUE` (`variable_type_id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;

INSERT INTO `tbl_variable_types` VALUES 
  (1,'BOOL'),(2,'BYTE'),(3,'WORD'),(4,'DWORD'),
  (5,'SINT'),(6,'INT'),(7,'DINT'),(8,'LINT'),(9,'USINT'),(10,'UINT'),
  (11,'UDINT'),(12,'ULINT'),(13,'REAL'),(14,'LREAL'),(15,'STRING');

CREATE TABLE `tbl_variables`
(
 `variable_tag_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Unique tag id to be used when calling variable in the code',  
 `variable_name` varchar(100) NOT NULL COMMENT 'Variable string name from the plc CASE SENSITIVE', 
 `variable_type` int(11) unsigned NOT NULL COMMENT 'The variable type from the twin table',  
 PRIMARY KEY (`variable_tag_id`),  
 UNIQUE KEY `variable_tag_id_UNIQUE` (`variable_tag_id`),  
 KEY `c_var_type_idx` (`variable_type`),  
 CONSTRAINT `c_var_type` FOREIGN KEY (`variable_type`) 
     REFERENCES `tbl_variable_types` (`variable_type_id`) 
     ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

如何将此代码转换为SQL Server?

1 个答案:

答案 0 :(得分:1)

您正在寻找以下查询

CREATE TABLE tbl_variable_types 
(
    variable_type_id int PRIMARY KEY NOT NULL Identity(1,1),  
    variable_type varchar(10) DEFAULT NULL
)  
INSERT INTO tbl_variable_types VALUES (1,'BOOL'),(2,'BYTE'),(3,'WORD'),(4,'DWORD'),(5,'SINT'),(6,'INT'),
                (7,'DINT'),(8,'LINT'),(9,'USINT'),(10,'UINT'),(11,'UDINT'),(12,'ULINT'),(13,'REAL'),
                (14,'LREAL'),(15,'STRING');

CREATE TABLE tbl_variables 
(  
    variable_tag_id int NOT NULL IDentity (1,1) PRIMARY KEY ,
    variable_name varchar(100) NOT NULL,  
    variable_type int NOT NULL REFERENCES tbl_variable_types (variable_type_id),  
);

添加列的注释

EXEC sp_addextendedproperty 
@name = N'Description', @value = 'Hey, here is my description!',
@level0type = N'Schema', @level0name = yourschema,
@level1type = N'Table',  @level1name = YourTable,
@level2type = N'Column', @level2name = yourColumn;
GO