Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql Server create table queries

I am using Sql Server Migration assistant to migrate my DB from MySql to SQL Server and in the process learning Sql Server.

The following is the create table syntax for the autogenerated schema.

CREATE TABLE [dbo].[TABLE1] (
    [COLUMN1] BIGINT IDENTITY (131556, 1) NOT NULL,
    [COLUMN2] INT CONSTRAINT [DF__TABLE_1__PRD_I__24E777C3] DEFAULT ((0)) NULL,
    [COLUMN3] INT CONSTRAINT [DF__TABLE1__PROMO__2AA05119] DEFAULT ((0)) NULL,
    CONSTRAINT [PK_TABLE1] PRIMARY KEY CLUSTERED ([COLUMN1] ASC)
);

GO
CREATE NONCLUSTERED INDEX [COLUMN3]
    ON [dbo].[TABLE1]([COLUMN3] ASC);

GO
EXECUTE sp_addextendedproperty @name = N'MS_SSMA_SOURCE', @value = N'TABLE1', 
@level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE',
@level1name = N'TABLE1';

I am trying to understand and cleanup the schema.Can someone please help with the following (naive) questions?

  1. Why should the primary key (COLUMN1) specified as a PRIMARY KEY CLUSTERED?
  2. In the original MySql table, COLUMN3 indexed. Is NONCLUSTERED INDEX the equivalent for Sql Server? What is the meaning of

    CREATE NONCLUSTERED INDEX [COLUMN3] ON [dbo].[TABLE1]([COLUMN3] ASC);

  3. I did not understand the following:

    EXECUTE sp_addextendedproperty @name = N'MS_SSMA_SOURCE', @value = N'TABLE1', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'TABLE1';

    Can someone explain what it does?

  4. Is the above create table syntax the minimal syntax to achieve what it does?
  5. I have 131555 rows in my MySql table. Should I be specifying IDENTITY (131556, 1) to start auto increment of key from 131556 after I migrate data?
like image 562
softwarematter Avatar asked Mar 18 '23 14:03

softwarematter


1 Answers

1.Why should the primary key (COLUMN1) specified as a "PRIMARY KEY CLUSTERED"?

It is generally best for every SQL Server table to have a clustered index, and only one clustered index is allowed per table because the b-tree leaf nodes of the clustered index are the actual data pages. The index supporting the primary key is often the best candidate but you can make the PK non-clustered and have different index as the clustered one if that's advantageous for your particular situation. For example, if your queries of most often range searches on COLUMN3 instead of queries that select or join by COLUMN1, the COLUMN3 index might be a better choice along with a NONCLUSTERED primary key.

2.In the original MySql table, COLUMN3 indexed. Is NONCLUSTERED INDEX the equivalent for Sql Server? What is the meaning of

A non-clustered index is also a b-tree index, allowing rows to be located by the index key more efficiently than a table scan.

3.I did not understand the following:

EXECUTE sp_addextendedproperty @name = N'MS_SSMA_SOURCE', @value = N'TABLE1', @level0type = N'SCHEMA', @level0name = N'dbo', @level1type = N'TABLE', @level1name = N'TABLE1';

Can someone explain what it does?

SQL Server has an extended property feature that allows you to attach meta-data to database objects. In this case, SSMA added an extended property to indicate the table was created by the tool.

4.Is the above create table syntax the minimal syntax to achieve what it does?

No. For example, one could omit the constraint names and SQL Server would generate a name automatically. However, the best practice is to explicitly name constraints to facilitate subsequent DDL changes and so that the purpose is self-documenting. Personally, I'd name the default constraints like DF_TABLENAME_COLUMNNAME.

5.I have 131555 rows in my MySql table. Should I be specifying IDENTITY (131556, 1) to start auto increment of key from 131556 after I migrate data?

If you were to create the table with IDENTITY(1,1) and then insert rows with the IDENTITY INSERT ON option, SQL Server will automatically adjust the next IDENTITY according to the highest value inserted. I don't know much about SSMA but it looks like SSMA already did that for you.

like image 127
Dan Guzman Avatar answered Mar 26 '23 02:03

Dan Guzman