【发布时间】:2017-01-26 07:00:46
【问题描述】:
有这个 SQL。
CREATE TABLE `accounts` (
`account_id` int(3) NOT NULL,
`username` varchar(36) NOT NULL,
`password` varchar(36) NOT NULL,
`email` varchar(60) NOT NULL,
`access_level` int(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `normal_profile` (
`normal_profileid` int(11) NOT NULL,
`first_name` varchar(64) NOT NULL,
`middle_name` varchar(64) NOT NULL,
`last_name` varchar(64) NOT NULL,
`age` int(2) NOT NULL,
`gender` varchar(6) NOT NULL,
`account_id` int(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `normal_profile`
ADD CONSTRAINT `normal_profile_ibfk_1` FOREIGN KEY (`account_id`) REFERENCES `accounts` (`account_id`);
以上是我设置约束的方式。
每当我在我的帐户表中插入一些内容时,我是否还必须查询一个插入到 normal_profile 表中的内容?或者我可以自动做到这一点,以便数据库本身只会在 account_id = accounts.account_id 的 normal_profile 中添加一行?
我对插入约束有一个模糊的记忆,或者我弄错了?
【问题讨论】:
-
数据库应该如何知道不相关字段中的内容??
-
我已经在 normal_profile 表中将 account_id 设置为 FK。我必须设置更多吗?
标签: php mysql phpmyadmin