添加外键时SQL Server出错

时间:2015-10-01 21:24:06

标签: sql sql-server database

参考表中没有主要或候选键'员工'与外键中的引用列列表匹配。

我已将我的引用设置为主键,但仍然收到错误。

 Create table Employee 
 (
     Fname varchar(20),
     Mint varchar(1),
     Lname varchar(20),
     Ssn int,
     Bdat date,
     [Address] varchar(50),
     Sex varchar(1),
     Salary int,
     Super_ssn int,
     Dno int
)

Create table Department   
(
     Dname varchar(20),
     Dnumber int,
     Mgr_ssn int,
     Mgr_start_date date
)

Alter table Employee
alter column Ssn int NOT NULL
alter column Super_ssn int NOT NULL

alter table Employee
add primary key (Ssn, Super_ssn)

alter table Department 
add foreign key (Mgr_ssn) 
    References Employee (Ssn)

1 个答案:

答案 0 :(得分:3)

您的Employees表(# ########################################## # Determine if we have Administrator rights Write-Host 'Checking user permissions... ' $windowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent() $windowsSecurityPrincipal = New-Object System.Security.Principal.WindowsPrincipal($windowsID) $adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator If (!($windowsSecurityPrincipal.IsInRole($adminRole))) { Write-Warning 'Current user does not have Administrator rights' Write-Host 'Attempting to copy files to temporary location and restarting script' # Get random file name Do { $temp = [System.IO.Path]::GetTempPath() + [System.IO.Path]::GetRandomFileName() } Until (!(Test-Path -LiteralPath "$temp")) # Create directory Write-Host 'Creating temp directory... ' -NoNewLine New-Item -Path "$temp" -ItemType 'Directory' | Out-Null Write-Host 'done.' # Copy script to directory Write-Host 'Copying script to temp directory... ' -NoNewLine Copy-Item -LiteralPath "$($myInvocation.MyCommand.Path)" "$temp" | Out-Null Write-Host 'done.' $newScript = "$($temp)\$($myInvocation.MyCommand.Name)" # Start new script elevated Write-Host 'Starting script as administrator... ' -NoNewLine $adminProcess = New-Object System.Diagnostics.ProcessStartInfo $adminProcess.Filename = ([System.Diagnostics.Process]::GetCurrentProcess()).Path $adminProcess.Arguments = " -File `"$newScript`"" $adminProcess.Verb = 'runas' Try { [System.Diagnostics.Process]::Start($adminProcess) | Out-Null } Catch { Write-Error 'Could not start process' Exit 1 } Write-Host 'done.' Exit 0 } #Change the execution policy Set-ExecutionPolicy bypass #Import the AD module Import-Module ActiveDirectory #Set variables $title = "Add Users To The Domain" $message = "For which department do you wanna add this user to?" $rn = New-Object System.Management.Automation.Host.ChoiceDescription "&RN", ` "RN" $callcenter = New-Object System.Management.Automation.Host.ChoiceDescription "&Call Center", ` "Call Center" $management = New-Object System.Management.Automation.Host.ChoiceDescription "&Management", ` "Management" $billing = New-Object System.Management.Automation.Host.ChoiceDescription "&Billing", ` "Billing" $options = [System.Management.Automation.Host.ChoiceDescription[]]($rn, $callcenter, $management, $billing) $result = $host.ui.PromptForChoice($title, $message, $options, 0) switch ($result) { 0 { "You selected RN." $OU = "RN" } 1 { "You selected Call Center." $OU = "CallCenter" } 2 { "You selected Management." $OU = "Management" } 3 { "You Selected Billing." $OU = "Billing" } } $UName = Read-Host "What is the username you wanna give? Make sure it matches the username in the email." $FName = Read-Host "What is the Full Name of the user?" New-ADUser ` -Name $FName ` -Path "CN=Users,OU=$OU,DC=Domain,DC=com" ` -SamAccountName $UName ` -DisplayName $FName ` -AccountPassword (ConvertTo-SecureString "password1" -AsPlainText -Force) ` -ChangePasswordAtLogon $true ` -Enabled $true Add-ADGroupMember "Users" "$UName"; )中有一个复合PK,但您尝试使用一列(Ssn, Super_ssn)引用它。

将PK更改为Mgr_ssn,或将另一列添加到外键。