我无法填充我的MySQL表

时间:2016-10-26 08:28:33

标签: mysql sql

我在MySQL DB上创建了3个表并希望填充它们,但我有一个例外。这是:

  

键'unique_idx'重复输入'1'

也许有人能说出我的错误?

InitialDB:

DELETE FROM lardi.users;
DELETE FROM lardi.user_roles;
DELETE FROM lardi.contacts;

INSERT INTO lardi.users
  (login,password,full_name) VALUES
  ('Bill', '112233', 'user'),
  ('John', '112233', 'user'),
  ('Mark', '112233', 'user');

INSERT INTO lardi.user_roles
  (role,user_id) VALUES
  ('USER_ROLE',1),
  ('USER_ROLE',2),
  ('ADMIN_ROLE',3);

INSERT INTO lardi.contacts
(first_name, last_name, patronymic, mobile_phone_number, home_phone_number, address, email, user_id) VALUES
  ('Bill','Gates','','+380(66)1234567','','USA','bill@gmail.com', 1),
  ('Mark','Zukenberg','','+380(66)9876543','+380(44)1122334','USA','mark@gmail.com', 1),
  ('Barak','Obama','','+380(99)1234567','','USA','barak@gmail.com', 1),
  ('Michel','Obama','','+380(99)9876543','','USA','michel@gmail.com', 1),
  ('David','Camaron','','+380(50)5557799','+380(44)0000009','UK','david@gmail.com', 2),
  ('Steve','Jobs','','+380(00)1100999','','USA','steve@gmail.com', 2),
  ('Tim','Kuk','','+380(00)2244888','','USA','tim@gmail.com', 2),
  ('Jim','Carry','','+380(69)8881188','+380(44)1111119','USA','jim@gmail.com', 3),
  ('David','Backham','','+380(67)90000001','','UK','david@gmail.com', 3);

PopulatorDB:

   $ ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

删除您的唯一索引

CREATE UNIQUE INDEX unique_idx ON contacts (user_id);

contactsuser_id是唯一的。然后,您无法将重复数据插入user_id

答案 1 :(得分:1)

CREATE UNIQUE INDEX unique_idx ON contacts (user_id);

您的contacts表在user_id上有唯一索引。

您正尝试插入一组具有相同用户ID的联系人。

答案 2 :(得分:0)

您的unique_idx必须是唯一的,但您使用INSERT INTO lardi.contacts保存重复的值