What to use instead of "Key" for SQL Server?

I have a script of MySQL queries that I am using and that are working.

I am trying to execute the same queries on a Microsoft SQL Server and there is one thing that I do not understand.

MySql uses "key" to define a key made up of different fields.

How do I do the same in SQL Server?

Thank!

-Adeena

0


source to share


2 answers


You can declare a primary key consisting of multiple columns in TSQL (SQL Server Query Language)

ALTER TABLE product
    ADD CONSTRAINT prim_prod PRIMARY KEY(product_foo, product_bar)

      



If you are using SQL Server Management studio, you can also achieve this through Modify Table.

+2


source


In MySQL, the keyword KEY

is just synonymous INDEX

. The following two are equivalent:

CREATE TABLE foo (
  id     SERIAL PRIMARY KEY,
  ctime  DATETIME,
  KEY ctkey (ctime)
);

CREATE TABLE foo (
  id     SERIAL PRIMARY KEY,
  ctime  DATETIME,
  INDEX ctidx (ctime)
);

      

In Microsoft SQL Server, the closest equivalent INDEX

. As far as I can tell, to create an index on a column in Microsoft SQL Server you are using CREATE INDEX

. You can also create constraints that create indexes as part of the statement CREATE TABLE

, but if you just need the index, use CREATE INDEX

.



CREATE TABLE foo (
  id     BIGINT IDENTITY PRIMARY KEY,
  ctime  DATETIME
);

CREATE INDEX ctidx ON foo(ctime);

      

See also the documentation for CREATE INDEX

.

+1


source







All Articles