如何在 sql fiddle 中用外键和主键创建 2 个表?
how to create 2 tables with foreign and primary key in sql fiddle?
2张表分别是RetailOutlet和SalesMan,两者通过ROID连接,ROID为主,对Retail Outlet唯一。
销售员有主键SID,也有外键ROID。
http://sqlfiddle.com/#!9/b1cd8a
CREATE TABLE IF NOT EXISTS `RetailOutlet` (
`id` int(6) unsigned NOT NULL,
`year` int(4) unsigned NOT NULL,
`location` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `RetailOutlet` (`id`, `year`, `location`) VALUES
('1', '1994', 'oregon'),
('2', '1990', 'amazon'),
('3', '2004', 'california'),
('4', '1997', 'newyork');
CREATE TABLE IF NOT EXISTS `SalesMan` (
`sid` int(6) unsigned NOT NULL,
`sname` varchar(200) NOT NULL,
`manager` int(4) unsigned NOT NULL,
`id` int(6) unsigned NOT NULL,
PRIMARY KEY (`sid`),
FOREIGN KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `SalesMan` (`sid`, `sname`, `manager`, `id`) VALUES
('1', 'john', 'fingao', '2'),
('2', 'bekc', 'dilda', '3'),
('3', 'aaa', 'elda', '4'),
('4', 'erjan', 'rrrrokcks', '1');
我的查询是获取不在纽约且只有 1 名销售员的商店,按名称的字母顺序显示。
SELECT * FROM SalesMan s INNER JOIN RetailOutlet r ON
s.ROID = r.ROID where count(s.SID) = 1 and
r.Location NOT LIKE 'New York' order by s.sname desc
我不知道什么地方不起作用。
您的外键声明有误,应该说明它引用的是哪个主键:
FOREIGN KEY (`id`) REFERENCES RetailOutlet (`id`)
这是完整的 SQL 我正在使用:
CREATE TABLE IF NOT EXISTS `RetailOutlet` (
`id` int(6) unsigned NOT NULL,
`year` int(4) unsigned NOT NULL,
`location` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `RetailOutlet` (`id`, `year`, `location`) VALUES
('1', '1994', 'oregon'),
('2', '1990', 'amazon'),
('3', '2004', 'california'),
('4', '1997', 'newyork');
CREATE TABLE IF NOT EXISTS `SalesMan` (
`sid` int(6) unsigned NOT NULL,
`sname` varchar(200) NOT NULL,
`manager` VARCHAR(200) NOT NULL,
`id` int(6) unsigned NOT NULL,
PRIMARY KEY (`sid`),
FOREIGN KEY (`id`) REFERENCES RetailOutlet (`id`)
);
INSERT INTO `SalesMan` (`sid`, `sname`, `manager`, `id`) VALUES
('1', 'john', 'fingao', '2'),
('2', 'bekc', 'dilda', '3'),
('3', 'aaa', 'elda', '4'),
('4', 'erjan', 'rrrrokcks', '1');
编辑:
并且您的 select 查询也无法与 where 子句中的 COUNT
函数一起使用。
SELECT *
FROM SalesMan s
INNER JOIN RetailOutlet r ON s.id = r.id
WHERE r.Location NOT LIKE 'New York'
ORDER BY s.sname desc
编辑 2:
因为OP里面有错别字:
SELECT *
FROM SalesMan s
INNER JOIN RetailOutlet r ON s.id = r.id
WHERE r.Location NOT LIKE 'newyork'
ORDER BY s.sname desc
为了正确测试,我添加了一些销售人员:
INSERT INTO `SalesMan` (`sid`, `sname`, `manager`, `id`) VALUES
('1', 'john', 'fingao', '2'),
('2', 'bekc', 'dilda', '3'),
('3', 'aaa', 'elda', '4'),
('4', 'erjan', 'rrrrokcks', '1'),
('5', 'john', 'fingao', '1'),
('6', 'benny', 'fingao', '1'),
('7', 'silvia', 'fingao', '1'),
('8', 'peter', 'dilda', '2'),
('9', 'karen', 'dilda', '2');
由于您对 RetailOutlet
中的记录感兴趣,我更改了连接顺序,以便 RetailOutlet
中的记录构成您搜索的基础。然后我执行通常的 JOIN 和 GROUP BY SalesMan
排除不完全有一条记录的记录:
SELECT *
FROM RetailOutlet r
INNER JOIN SalesMan s ON s.id = r.id
WHERE r.Location NOT LIKE 'newyork'
GROUP BY s.id
HAVING count(s.id) = 1
ORDER by s.sname DESC
此 dbfiddle 显示了从创建 table 到最终查询的工作步骤。
注意事项:将 id 用于除 current table id 之外的任何东西都是不正常的做法。将 id 用于引用另一个 table 的列,同时将 sid 命名为本地 tables actual id 很可能会使其他开发人员感到困惑。我建议你改成:
CREATE TABLE IF NOT EXISTS `SalesMan` (
`id` int(6) unsigned NOT NULL,
`sname` varchar(200) NOT NULL,
`manager` int(4) unsigned NOT NULL,
`rid` int(6) unsigned NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`rid`) REFERENCES RetailOutlet (`id`)
) DEFAULT CHARSET=utf8;
..并相应地更改您的插入和查询。
2张表分别是RetailOutlet和SalesMan,两者通过ROID连接,ROID为主,对Retail Outlet唯一。
销售员有主键SID,也有外键ROID。
http://sqlfiddle.com/#!9/b1cd8a
CREATE TABLE IF NOT EXISTS `RetailOutlet` (
`id` int(6) unsigned NOT NULL,
`year` int(4) unsigned NOT NULL,
`location` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `RetailOutlet` (`id`, `year`, `location`) VALUES
('1', '1994', 'oregon'),
('2', '1990', 'amazon'),
('3', '2004', 'california'),
('4', '1997', 'newyork');
CREATE TABLE IF NOT EXISTS `SalesMan` (
`sid` int(6) unsigned NOT NULL,
`sname` varchar(200) NOT NULL,
`manager` int(4) unsigned NOT NULL,
`id` int(6) unsigned NOT NULL,
PRIMARY KEY (`sid`),
FOREIGN KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `SalesMan` (`sid`, `sname`, `manager`, `id`) VALUES
('1', 'john', 'fingao', '2'),
('2', 'bekc', 'dilda', '3'),
('3', 'aaa', 'elda', '4'),
('4', 'erjan', 'rrrrokcks', '1');
我的查询是获取不在纽约且只有 1 名销售员的商店,按名称的字母顺序显示。
SELECT * FROM SalesMan s INNER JOIN RetailOutlet r ON
s.ROID = r.ROID where count(s.SID) = 1 and
r.Location NOT LIKE 'New York' order by s.sname desc
我不知道什么地方不起作用。
您的外键声明有误,应该说明它引用的是哪个主键:
FOREIGN KEY (`id`) REFERENCES RetailOutlet (`id`)
这是完整的 SQL 我正在使用:
CREATE TABLE IF NOT EXISTS `RetailOutlet` (
`id` int(6) unsigned NOT NULL,
`year` int(4) unsigned NOT NULL,
`location` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `RetailOutlet` (`id`, `year`, `location`) VALUES
('1', '1994', 'oregon'),
('2', '1990', 'amazon'),
('3', '2004', 'california'),
('4', '1997', 'newyork');
CREATE TABLE IF NOT EXISTS `SalesMan` (
`sid` int(6) unsigned NOT NULL,
`sname` varchar(200) NOT NULL,
`manager` VARCHAR(200) NOT NULL,
`id` int(6) unsigned NOT NULL,
PRIMARY KEY (`sid`),
FOREIGN KEY (`id`) REFERENCES RetailOutlet (`id`)
);
INSERT INTO `SalesMan` (`sid`, `sname`, `manager`, `id`) VALUES
('1', 'john', 'fingao', '2'),
('2', 'bekc', 'dilda', '3'),
('3', 'aaa', 'elda', '4'),
('4', 'erjan', 'rrrrokcks', '1');
编辑:
并且您的 select 查询也无法与 where 子句中的 COUNT
函数一起使用。
SELECT *
FROM SalesMan s
INNER JOIN RetailOutlet r ON s.id = r.id
WHERE r.Location NOT LIKE 'New York'
ORDER BY s.sname desc
编辑 2:
因为OP里面有错别字:
SELECT *
FROM SalesMan s
INNER JOIN RetailOutlet r ON s.id = r.id
WHERE r.Location NOT LIKE 'newyork'
ORDER BY s.sname desc
为了正确测试,我添加了一些销售人员:
INSERT INTO `SalesMan` (`sid`, `sname`, `manager`, `id`) VALUES
('1', 'john', 'fingao', '2'),
('2', 'bekc', 'dilda', '3'),
('3', 'aaa', 'elda', '4'),
('4', 'erjan', 'rrrrokcks', '1'),
('5', 'john', 'fingao', '1'),
('6', 'benny', 'fingao', '1'),
('7', 'silvia', 'fingao', '1'),
('8', 'peter', 'dilda', '2'),
('9', 'karen', 'dilda', '2');
由于您对 RetailOutlet
中的记录感兴趣,我更改了连接顺序,以便 RetailOutlet
中的记录构成您搜索的基础。然后我执行通常的 JOIN 和 GROUP BY SalesMan
排除不完全有一条记录的记录:
SELECT *
FROM RetailOutlet r
INNER JOIN SalesMan s ON s.id = r.id
WHERE r.Location NOT LIKE 'newyork'
GROUP BY s.id
HAVING count(s.id) = 1
ORDER by s.sname DESC
此 dbfiddle 显示了从创建 table 到最终查询的工作步骤。
注意事项:将 id 用于除 current table id 之外的任何东西都是不正常的做法。将 id 用于引用另一个 table 的列,同时将 sid 命名为本地 tables actual id 很可能会使其他开发人员感到困惑。我建议你改成:
CREATE TABLE IF NOT EXISTS `SalesMan` (
`id` int(6) unsigned NOT NULL,
`sname` varchar(200) NOT NULL,
`manager` int(4) unsigned NOT NULL,
`rid` int(6) unsigned NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`rid`) REFERENCES RetailOutlet (`id`)
) DEFAULT CHARSET=utf8;
..并相应地更改您的插入和查询。