Insert中的SQL FOREIGN KEY约束

时间:2018-01-28 11:36:20

标签: sql-server sql-insert

我在向表中插入一些值时遇到问题。 Microsoft SQL Server管理显示:

  

INSERT语句与FOREIGN KEY约束“FK__Players__Menager__55D59338”冲突。冲突发生在数据库中   “TransferyProjekt”,表格“dbo.Menagers”,列'idMenager'。

我的创建表脚本。

CREATE TABLE Menagers (
  idMenager INT IDENTITY(1,1) NOT NULL,
  [name] VARCHAR(30) NOT NULL CHECK (name LIKE '[A-Z]%'),
  surname VARCHAR(30) NOT NULL CHECK (surname LIKE '[A-Z]%'),
  phoneNumber VARCHAR(18) NOT NULL CHECK (phoneNumber LIKE '+[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'),
  PRIMARY KEY (idMenager)
  );

CREATE TABLE Players (
  idPlayer INT IDENTITY(1,1) NOT NULL,
  [name] VARCHAR(30) NOT NULL,
  surname VARCHAR(30) NOT NULL,
  age DATE NOT NULL check (DATEDIFF(year,age,GETDATE()) > 18),
  club INT NOT NULL,
  Menager INT NOT NULL,
  PRIMARY KEY (idPlayer),
  FOREIGN KEY (club) REFERENCES Clubs(idClub),
  FOREIGN KEY (Menager) REFERENCES Menagers(idMenager)
  );

我的插页看起来像。

INSERT INTO Menagers VALUES
    ('Adil','Green','+232247832'),
    ('Wicky','Dock','+301494064'),
    ('Alead','King','+447499384'),
    ('Darmian','Dagoly','+445587849'),
    ('Kamila','Dobra','+958789278'),
    ('Mateusz','Jankowiak','+849383098'),
    ('Lendy','Day','+448902920'),
    ('Martin','Lloyd','+501044468'),
    ('Adam','Dosh','+045033739'),
    ('Cristian','Cosy','+307748735'),
    ('Andrew','Lloyd','+635875452'),
    ('Matias','Banega','+520091224'),
    ('Carl','Rossi','+196935415'),
    ('Michał','Rolnik','+156541588'),
    ('Denny','Nowsky','+231785387'),
    ('Micky','Elly','+125774609'),
    ('George','Taylor','+094371433'),
    ('Barack','Obama','+916764868'),
    ('Jin','Chan','+906765545'),
    ('Lee','Konsu','+608935829'),
    ('Adam','Kenzo','+417708081'),
    ('Bryan','Along','+939454178'),
    ('Robert','Leey','+183354912'),
    ('Tom','Vardy','+576176145'),
    ('Kevin','Betword','+721582207');

INSERT INTO Players VALUES
        ('Lionel','Messi','1986-07-13','23','4'),
        ('Cristiano','Ronaldo','1986-04-11','23','5'),
        ('Sergio','Ramos','1986-09-07','23','12'),
        ('Łukasz','Piszczek','1986-11-20','23','14'),
        ('Robert','Lewandowski','1986-12-01','2','13'),
        ('Michał','Pazdan','1986-06-01','3','23'),
        ('Łukasz','Trałka','1986-05-02','7','20'),
        ('Łukasz','Teodorczyk','1986-04-14','6','18'),
        ('Mariusz','Miley','1985-03-06','3','26');

4 个答案:

答案 0 :(得分:0)

您的外键和主键列为INT类型。但是,您正在使用SQL

INSERT INTO Players VALUES
    ('Lionel','Messi','1986-07-13','23','4')

其中最后一列属于外键列,您的值为字符串。

答案 1 :(得分:0)

如果源表和目标表具有不同的列数或不同的顺序

,则应定义列名
INSERT INTO Menagers([name],surname, phoneNumber ) 
VALUES
    ('Adil','Green','+232247832'),
    ('Wicky','Dock','+301494064'),
    ....

INSERT INTO Players([name], surname, age, club, Menager )
VALUES
    ('Lionel','Messi','1986-07-13','23','4'),
    ('Cristiano','Ronaldo','1986-04-11','23','5'),
    ...

答案 2 :(得分:0)

外键是对不同表中唯一值的引用,SQL将确保"引用完整性" - 这意味着它不会让你以#34;孤儿"关键参考。向外键列插入值时,它必须是null或对另一个表中的行的现有引用,并且在删除时,必须首先删除包含外键的行,然后删除它引用的行。 / p>

如果你不这样做,你会收到一个错误,如你描述的那样。

所以将行输入" main"先表,然后输入"依赖"表信息第二。

答案 3 :(得分:0)

在FK专栏中,我不幸地引用了表管理器中不存在的行...

  

( '马里乌什', '麦莉', '1985年3月6日', '3', '的 26 ');   但我在桌子经理中没有排第26行(责备我,现在):(