不同表上的外键

时间:2016-02-01 17:09:22

标签: mysql sql

我有一张图片表:

图片

  • image_id
  • image_type('foo','bar')
  • TYPE_ID

如果image_typefootype_id应与foo表相关,如果image_type为{{1>,我该如何创建外键?到bar表。

1 个答案:

答案 0 :(得分:1)

您尝试做的是创建超类型。在SQL中,这最好建模如下(IMO至少)。如果语法不适合MySQL,我很抱歉。我通常使用MS SQL Server。

<html>
<head>
<script type = "text/javascript">

function conversion(n) {
<!--if(n<1);-->
return (n/0.62137).toFixed(2);
}

function conversionTable(rangeStart, rangeEnd) {
if(atLeastOneRadio()) {
    divStr="<table border=1><tr><td>Miles</td><td>Kilometres</td></tr>";
    if(isNaN(i)){document.write("Please input an integer")}
    else {
    for(i=rangeStart;i<=rangeEnd;i++) {
        divStr+="<tr><td>" + i + "</td><td>" + conversion(i) + "</td></tr>";
  }
    document.getElementById("divResult").innerHTML=divStr;
}}
}

function getnputValue() {
return parseInt(document.getElementById("rangeTxt").value);
}

function check() {
var radios = document.getElementsByName("choice");

for (var i = 0, len = radios.length; i < len; i++) {
if (radios[i].checked) {
    return true;
}
}

return false;
}

function atLeastOneRadio() {
return ($('input[type=radio]:checked').length > 0);
}
</script>
</head>
<body>
<p>
Start : <input type=textbox id=rangeTxt value=""/>
Finish : <input type=textbox id=rangeTxt2 value=""/>
<input type=radio name="convert" id="mtokm" value ="Miles to Kilometre"/>     Miles to Kilometre
<input type=radio name="convert" id="kmtom" value ="Kilometre to Miles"/>   Kilometre to Miles
<br>
<br>
<button       onClick="conversionTable(getnputValue(),parseInt(document.getElementById('rangeTxt2').value))">Convert</button>
</p>
<div id="divResult">
</div>
</body>
</html>

如果您的RDBMS支持,另一种方法是在CREATE TABLE Image_Types ( image_type_id INT NOT NULL, image_type CHAR(3) NOT NULL CONSTRAINT CHK_Image_Types_image_type CHECK (image_type IN ('Foo', 'Bar')), CONSTRAINT PK_Image_Types PRIMARY KEY (image_type_id, image_type) ) CREATE TABLE Foo ( image_type_id INT NOT NULL, image_type CHAR(3) NOT NULL CONSTRAINT CHK_Foo_image_type CHECK (image_type IN ('Foo')), some_foo_specific_column VARCHAR(100) NOT NULL, CONSTRAINT PK_Foo PRIMARY KEY (image_type_id), CONSTRAINT FK_Foo_ImageTypes FOREIGN KEY (image_type_id, image_type) REFERENCES Image_Types (image_type_id, image_type) ) CREATE TABLE Bar ( image_type_id INT NOT NULL, image_type CHAR(3) NOT NULL CONSTRAINT CHK_Bar_image_type CHECK (image_type IN ('Bar')), some_bar_specific_column VARCHAR(100) NOT NULL, CONSTRAINT PK_Bar PRIMARY KEY (image_type_id), CONSTRAINT FK_Bar_ImageTypes FOREIGN KEY (image_type_id, image_type) REFERENCES Image_Types (image_type_id, image_type) ) CREATE TABLE Images ( image_id INT NOT NULL, image_type CHAR(3) NOT NULL CONSTRAINT CHK_Images_image_type CHECK (image_type IN ('Foo', 'Bar')), image_type_id INT NOT NULL, CONSTRAINT PK_Images PRIMARY KEY (image_id), CONSTRAINT FK_Images_ImageTypes FOREIGN KEY (image_type_id, image_type) REFERENCES Image_Types (image_type_id, image_type) ) 上使用过滤的索引视图,然后将Image_TypesFoo中的FK指向这些视图。这样您就可以从某些表中删除Bar列。有些人喜欢上述模式,即使过滤后的索引视图可用。