1

I have a number of tables I need to convert from mySQL to SQL Server.

An Example of a mySQL Table is

CREATE TABLE `required_items` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Unique Barcode ID',
`fk_load_id` INT( 11 ) NOT NULL COMMENT 'Load ID', 
`barcode` VARCHAR( 255 ) NOT NULL COMMENT 'Barcode Value',
`description` VARCHAR( 255 ) NULL DEFAULT NULL COMMENT 'Barcode Description',
`created` TIMESTAMP NULL DEFAULT NULL COMMENT 'Creation Timestamp',
`modified` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Modified Timestamp',
FOREIGN KEY (`fk_load_id`) REFERENCES `loads`(`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET ascii COLLATE ascii_general_ci COMMENT = 'Contains Required Items for the Load';

And a trigger to update the created date

CREATE TRIGGER required_items_before_insert_created_date BEFORE INSERT ON `required_items`
FOR EACH ROW
BEGIN
  SET NEW.created = CURRENT_TIMESTAMP;
END

Now I need to create tables similar to this in SQL Server. There seems to be a lot of different data types available so I am unsure which to use.

  • What data type should I use to the primary key column (uniqueidentifier, bigint, int)?
  • What should I use for the timestamps (timestamp, datatime, datetime2(7))?
  • How should I enforce the created and modified timestamps (currently I am using triggers)?
  • How can I enforce foreign key constraints.
  • Should I be using Varchar(255) in SQL Server? (Maybe Text, Varchar(MAX) is better)

I am using Visual Studio 2010 to create the tables.

jax
  • 34,985
  • 56
  • 167
  • 267

1 Answers1

0

First of all, you can probably use PHPMyAdmin (or something similar) to script out the table creation process to SQL Server. You can take a look at what is automatically created for you to get an idea of what you should be using. After that, you should take a look at SSMS (SQL Server Management Studio) over Visual Studio 2010. Tweaking the tables that your script will create will be easier in SSMS - in fact, most database development tasks will be easier in SSMS.

What data type should I use to the primary key column (uniqueidentifier, bigint, int)? Depending on how many records you plan to have in your table, use int, or bigint. There are problems with uniqueidentfiers that you will probably want to avoid. INT vs Unique-Identifier for ID field in database

What should I use for the timestamps (timestamp, datatime, datetime2(7))? timestamps are different in SQL Server than in MySQL. Despite the name, a timestamp is an incrementing number that is used as a mechanism to version rows. http://msdn.microsoft.com/en-us/library/ms182776%28v=sql.90%29.aspx . In short though, datetime is probably your best bet for compatibility purposes.

How should I enforce the created and modified timestamps (currently I am using triggers)? See above. Also, the SQL Server version of a "Timestamp" is automatically updated by the DBMS. If you need a timestamp similar to your MySQL version, you can use a trigger to do that (but that is generally frowned upon...kind of dogmatic really).

How can I enforce foreign key constraints. You should treat them as you would using innoDB. See this article for examples of creating foreign key constraints http://blog.sqlauthority.com/2008/09/08/sql-server-%E2%80%93-2008-creating-primary-key-foreign-key-and-default-constraint/

Should I be using Varchar(255) in SQL Server? (Maybe Text, Varchar(MAX) is better) That depends on the data you plan to store in the field. Varchar max is equivalent to varchar(8000) and if you don't need varchar(255), you can always set it to a lower value like varchar(50). Using a field size that is too large has performance implications. One thing to note is that if you plan to support unicode (multilingual) data in your field, use nvarchar or nchar.

Community
  • 1
  • 1
gangreen
  • 629
  • 5
  • 7