English 中文(简体)
ALTER TABLE 增加一个综合的主要关键因素
原标题:ALTER TABLE to add a composite primary key

我的表格称为provider。 页: 1 可能有重复人员、重复地点和重复工作,但永远不可能有假人所在地的组合。

How would I ALTER TABLE to add a composite primary key for this table in MySQL with the these three columns?

最佳回答
ALTER TABLE provider ADD PRIMARY KEY(person,place,thing);

如果已经存在主要关键,那么你就希望这样做。

ALTER TABLE provider DROP PRIMARY KEY, ADD PRIMARY KEY(person, place, thing);
问题回答

@Adrian Cornish的回答是正确的。 然而,还有另一个告诫要放弃现有的主要钥匙。 如果这一主要钥匙被另一张桌子用作外国钥匙,那么你在试图放弃该钥匙时会遇到错误。 在一些版本的神秘电文中,错误电文被误报(截至5.5.17,这一错误电文仍然存在。

alter table parent  drop column id;
ERROR 1025 (HY000): Error on rename of
 ./test/#sql-a04_b  to  ./test/parent  (errno: 150).

如果你想要放弃另一个表格提到的主要钥匙,你将不得不在另一个表格中首先放弃外国钥匙。 如果你在重新确定主要钥匙后仍然想要外国钥匙,你可以重新研究。

Also, when using composite keys, order is important. These

1) ALTER TABLE provider ADD PRIMARY KEY(person,place,thing);
and
2) ALTER TABLE provider ADD PRIMARY KEY(person,thing,place);

are not the the same thing. They both enforce uniqueness on that set of three fields, however from an indexing standpoint there is a difference. The fields are indexed from left to right. For example, consider the following queries:

A) SELECT person, place, thing FROM provider WHERE person =  foo  AND thing =  bar ;
B) SELECT person, place, thing FROM provider WHERE person =  foo  AND place =  baz ;
C) SELECT person, place, thing FROM provider WHERE person =  foo  AND place =  baz  AND thing =  bar ;
D) SELECT person, place, thing FROM provider WHERE place =  baz  AND thing =  bar ;

B can use the primary key index in ALTER statement 1
A can use the primary key index in ALTER statement 2
C can use either index
D can t use either index

A将指数2的前两个领域作为部分指数。 a 不能使用指数1,因为它不知道指数的中间位置部分。 即便如此,它仍可能只使用部分指数。

D可以不使用指数,因为它没有人知道。

See the mysql docs here, for more information.

You may simply want a UNIQUE CONSTRAINT. Especially if you already have a surrogate key. (example of an already existing surrogate key would be a single column that is an AUTO_INCREMENT )

下面是《联邦制约法》的缩略语。

ALTER TABLE `MyDatabase`.`Provider`
    ADD CONSTRAINT CK_Per_Place_Thing_Unique UNIQUE (person,place,thing)
;
alter table table_name add primary key (col_name1, col_name2);

www.un.org/Depts/DGACM/index_arabic.htm

如@GranadaCoder提供的,使用COMOSITE UNI Request KEY肯定会更好。

<编码> ALTER IGNORE TABLE table_name ADD UNI RequestS INDEX idx_name(about_id, another_id, one_more_id);





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签