在mysql中使用复合键作为外键

时间:2015-07-28 19:28:04

标签: mysql phpmyadmin foreign-keys primary-key composite-primary-key

我的桌子

费用表

//retrieve user info from request
//example: Avatar.php?ID=1 (page will load avatar for user with ID of 1)
$ID = $db->real_escape_string(strip_tags(stripslashes($_GET['ID'])));
$Username = $db->real_escape_string(strip_tags(stripslashes($_GET['Username'])));
if (!$Username) {
    $getUser = mysqli_query($db, "SELECT * FROM Users WHERE ID='".$ID."'");
}
else {
    $getUser = mysqli_query($db, "SELECT * FROM Users WHERE Username='".$Username."'");
}
$gU = mysqli_fetch_object($getUser);

//load clothing
$Body = $gU->Body;
if (empty($Body)) {
    $Body = "/Avatars/Avatar.png";
}

$Background = $gU->Background;
if (empty($Background)) {
    $Background = "thingTransparent.png";
}

$Eyes = $gU->Eyes;
if (empty($Eyes)) {
    $Eyes = "thingTransparent.png";
}

$Mouth = $gU->Mouth;
if (empty($Mouth)) {
    $Mouth = "thingTransparent.png";
}

$Hair = $gU->Hair;
if (empty($Hair)) {
    $Hair = "thingTransparent.png";
}

$Bottom = $gU->Bottom;
if (empty($Bottom)) {
    $Bottom = "thingTransparent.png";
}

$Top = $gU->Top;
if (empty($Top)) {
    $Top = "thingTransparent.png";
}

$TShirt = $gU->TShirt;
if (empty($TShirt)) {
    $TShirt = "thingTransparent.png";
}

$Hat3 = $gU->Hat3;
if (empty($Hat3)) {
    $Hat3 = "thingTransparent.png";
}

$Hat2 = $gU->Hat2;
if (empty($Hat2)) {
    $Hat2 = "thingTransparent.png";
}

$Hat = $gU->Hat;
if (empty($Hat)) {
    $Hat = "thingTransparent.png";
}

$Shoes = $gU->Shoes;
if (empty($Shoes)) {
    $Shoes = "thingTransparent.png";
}

$Accessory = $gU->Accessory;
if (empty($Accessory)) {
    $Accessory = "thingTransparent.png";
}


//render the avatar image
class StackImage
{
    private $image;
    private $width;
    private $height;

    public function __construct($Path)
    {
        if(!isset($Path) || !file_exists($Path))
        return;
        $this->image = imagecreatefrompng($Path);
        imagesavealpha($this->image, true);
        imagealphablending($this->image, true);
        $this->width = imagesx($this->image);
        $this->height = imagesy($this->image);
    }

    public function AddLayer($Path)
    {
        if(!isset($Path) || !file_exists($Path))
            return;
        $new = imagecreatefrompng($Path);
        imagesavealpha($new, true);
        imagealphablending($new, true);
        imagecopy($this->image, $new, 0, 0, 0, 0, imagesx($new), imagesy($new));
    }

    public function Output($type = "image/png")
    {
        header("Content-Type: {$type}");
        imagepng($this->image);
        imagedestroy($this->image);
    }

    public function GetWidth()
    {
        return $this->width;
    }

    public function GetHeight()
    {
        return $this->height;
    }
}

//add layer to image for the items that the user is wearing
//must be in specific order so you don't have unnecessary items overlapping each other incorrectly
//don't touch the bottom code
$Image = new StackImage("Store/Dir/thingTransparent.png");
$Image->AddLayer("Store/Dir/".$Background."");
$Image->AddLayer("Store/Dir/".$Body."");
$Image->AddLayer("Store/Dir/".$Eyes."");
$Image->AddLayer("Store/Dir/".$Mouth."");
$Image->AddLayer("Store/Dir/".$Bottom."");
$Image->AddLayer("Store/Dir/".$Top."");
$Image->AddLayer("Store/Dir/".$TShirt."");
$Image->AddLayer("Store/Dir/".$Hair."");
$Image->AddLayer("Store/Dir/".$Hat."");
$Image->AddLayer("Store/Dir/".$Shoes."");
$Image->AddLayer("Store/Dir/".$Accessory."");

//if the user is online, show online symbol
if (time() < $gU->expireTime) {
    $Image->AddLayer("Images/Online.png");
}
//or else if the user is offline, show offline symbol
else {
    $Image->AddLayer("Images/Offline.png");
}

//if the user is premium, show a little premium icon to the bottom left of their avatar
if ($gU->Premium == 1) {
    $Image->AddLayer("Images/Premium.png");
}

//load up the rendered image
$Image->Output();

住房表

CREATE TABLE IF NOT EXISTS `fee` (
`feeNumber` int(11) NOT NULL,
`feeName` varchar(255) NOT NULL,
`feeDescription` varchar(255) DEFAULT NULL,
`feeAmount` varchar(255) NOT NULL,
`universityName` varchar(255) NOT NULL
) 
ADD PRIMARY KEY (`feeNumber`,`feeName`,`feeAmount`)

我尝试做的是从费用表中引用feeAmount,feeNumber和feeName,并将其添加到住房表中的housingfee,housingfeeName和housingFeenumber列。由于feeAmount和feeName列不是唯一的,我决定用feeNumber,feeName和feeAmount创建一个复合键。我真正需要引用的复合键的唯一部分是feeAmount。 feeName和feeNumber并不重要。使用phpmyadmin。

1 个答案:

答案 0 :(得分:0)

不,不,不。一般来说,我认为最好避免使用复合主键。在这种情况下,特别是您不希望费用金额或名称成为密钥的一部分。要访问该行,您需要知道该金额。这似乎很难实施。

我猜测自动递增的主键可以正常工作。然后,您可以在其他列上添加单独的唯一约束。所以:

CREATE TABLE IF NOT EXISTS `fee` (
    `feeNumber` int(11) NOT NULL auto_increment primary key,
    `feeName` varchar(255) NOT NULL,
    `feeDescription` varchar(255) DEFAULT NULL,
    `feeAmount` varchar(255) NOT NULL,
    `universityName` varchar(255) NOT NULL,
     unique (feeName)
);

然后,另一个表将feeNumber具有适当的外键引用。通过引用,您可以使用join获取其他信息 - 例如名称和金额。