2016-01-31 21 views
0

這是我的SQL代碼基礎教程,用戶鏈接到項目:外國ID到一個錶鏈接到另一個

CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) unsigned NOT NULL AUTO_INCREMENT, 
`user_name` varchar(64) COLLATE utf8_unicode_ci NOT NULL, 
`user_password_hash` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
`user_email` varchar(254) COLLATE utf8_unicode_ci NOT NULL, 
`user_access_level` tinyint(3) unsigned NOT NULL DEFAULT '0', 
`user_active` tinyint(1) NOT NULL DEFAULT '0', 
`user_activation_hash` varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL, 
`user_password_reset_hash` char(40) COLLATE utf8_unicode_ci DEFAULT NULL, 
`user_password_reset_timestamp` bigint(20) DEFAULT NULL, 
`user_failed_logins` tinyint(1) NOT NULL DEFAULT '0', 
`user_last_failed_login` int(10) DEFAULT NULL, 
`user_registration_datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
`user_registration_ip` varchar(39) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0.0.0.0', 
PRIMARY KEY (`user_id`), 
UNIQUE KEY `user_name` (`user_name`), 
UNIQUE KEY `user_email` (`user_email`) 
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 


CREATE TABLE IF NOT EXISTS `item` (
`item_id` int(11) unsigned NOT NULL AUTO_INCREMENT, 
`item_title` varchar(64) COLLATE utf8_unicode_ci NOT NULL, 
`item_location` varchar(64) COLLATE utf8_unicode_ci NOT NULL, 
`item_description` varchar(64) COLLATE utf8_unicode_ci NOT NULL, 
`item_datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
`item_status` int(1) unsigned NOT NULL, 
PRIMARY KEY (`item_id`) 
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

http://sqlfiddle.com/#!9/fd011

我變得有點困惑如何將項目鏈接到用戶。好像我需要在我的項目表中的東西叫上項目外鍵,有點像這樣:

FOREIGN KEY (user_id) REFERENCES user(ID) 

我似乎無法得到它的成功編譯和查詢。任何人都可以告訴我正確的方式來將項目與用戶相關聯。

回答

1

您需要的user_id柱與約束一起添加到items表:

CREATE TABLE IF NOT EXISTS `users` (
`user_id` int(11) unsigned NOT NULL AUTO_INCREMENT, 
`user_name` varchar(64) COLLATE utf8_unicode_ci NOT NULL, 
`user_password_hash` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
`user_email` varchar(254) COLLATE utf8_unicode_ci NOT NULL, 
`user_access_level` tinyint(3) unsigned NOT NULL DEFAULT '0', 
`user_active` tinyint(1) NOT NULL DEFAULT '0', 
`user_activation_hash` varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL, 
`user_password_reset_hash` char(40) COLLATE utf8_unicode_ci DEFAULT NULL, 
`user_password_reset_timestamp` bigint(20) DEFAULT NULL, 
`user_failed_logins` tinyint(1) NOT NULL DEFAULT '0', 
`user_last_failed_login` int(10) DEFAULT NULL, 
`user_registration_datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
`user_registration_ip` varchar(39) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0.0.0.0', 
PRIMARY KEY (`user_id`), 
UNIQUE KEY `user_name` (`user_name`), 
UNIQUE KEY `user_email` (`user_email`) 
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 


CREATE TABLE IF NOT EXISTS `item` (
`item_id` int(11) unsigned NOT NULL AUTO_INCREMENT, 
    user_id int unsigned, 
`item_title` varchar(64) COLLATE utf8_unicode_ci NOT NULL, 
`item_location` varchar(64) COLLATE utf8_unicode_ci NOT NULL, 
`item_description` varchar(64) COLLATE utf8_unicode_ci NOT NULL, 
`item_datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
`item_status` int(1) unsigned NOT NULL, 
PRIMARY KEY (`item_id`), 
    FOREIGN KEY (user_id) REFERENCES users(user_id) 
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

的SQL小提琴是here

我要指出一些其他的東西:

  • 注意你所使用的數據庫引擎。 MyISAM實際上並沒有執行這種關係。
  • 奇怪的日期作爲默認值可能比只使用NULL有用。
  • 我不確定是否有值爲每個字符定義顯式歸類,除非您的數據庫將支持各種各樣的歸類。
  • 請勿爲數字常量使用單引號。因此,如果某個值聲明爲tinyint,請將缺省值設置爲0而不是'0'(這不會影響CREATE TABLE語句中的性能;這只是誤導)。
+1

也可能使用InnoDB代替MyISAM。 –

+0

@MichaelBerkowski邁克爾怎麼樣?我需要在這方面更改我的任何PHP代碼嗎? – Jimmy

+1

@Jimmy MyISAM表實際上並不強制執行外鍵約束,儘管定義它們不是語法錯誤。你的代碼不需要改變,只需要創建表語句。 http://dev.mysql.com/doc/refman/5.7/en/myisam-storage-engine.html –

1

@ GordonLindoff的解決方案是一種方法,但假設每個項目只屬於一個用戶,並且一個項目不能被多個用戶引用。如果你有許多一對多的關係,在這裏用戶可以擁有多個項目,一個項目可以由多個用戶引用,那麼你就需要第三個錶鏈接在一起:

CREATE TABLE IF NOT EXISTS `user_item` (
    `user_id` int(11) unsigned NOT NULL, 
    `item_id` int(11) unsigned NOT NULL, 
    PRIMARY KEY (`user_item`,`item_id`), 
    FOREIGN KEY (user_id) REFERENCES users(user_id), 
    FOREIGN KEY (item_id) REFERENCES items(item_id) 
)ENGINE=Innodb DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

外鍵約束爲user_item中user_id存在於用戶中的每一行強制執行此操作,item_id存在於項目中。正如前面的評論中提到的那樣,您需要Innodb實施外鍵約束。