• 0

Foreign Keys


Question

Its been a while since I did any seriously complex SQL, and so I've forgotten :s

Is it possible to have multiple foreign keys in a table? and if that is possible, can I have a foreign key which might contain a null?

My reason for doing this is that i have a list of countries in one table and a list of US states in another, and as everyone registering might not be from the US obviously the state field will contain a null.

I'm using MySQL and using the innodb engine so i have foreign key constraints.

Link to comment
https://www.neowin.net/forum/topic/280665-foreign-keys/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Oh this is not a good day anyone have any ideas why my SQL script for MySQL 4.1 won' t execute it looks correct to me, theres a problem with the foreign keys but i can't see what it is. I've created the correct indices for each of the foreign keys and i've even added constraints the way the MySQL manual demonstrats

CREATE TABLE country (
cid int NOT NULL auto_increment,
name varchar(40) NOT NULL,
shortname varchar(3),
PRIMARY KEY (cid)
)type=myisam;

CREATE TABLE state (
sid int NOT NULL auto_increment,
name varchar(40) NOT NULL,
shortname varchar(3),
PRIMARY KEY (sid)
)type=myisam;

CREATE TABLE users (
uid int NOT NULL auto_increment,
email varchar(250) NOT NULL,
password varchar(32) NOT NULL,
firstname varchar(50) NOT NULL,
lastname varchar(50) NOT NULL,
dob varchar(10) NOT NULL,
address1 varchar(200) NOT NULL,
address2 varchar(100),
address3 varchar(100),
city varchar(100) NOT NULL,
postcode varchar(20),
phone varchar(16) NOT NULL,
company varchar(200),
cid int NOT NULL DEFAULT 0, /*country id from country table*/
sid int NOT NULL DEFAULT 0, /*state id from states table*/
PRIMARY KEY (uid),
UNIQUE KEY (email),
INDEX user_auth (email, password),
INDEX country_id (cid),
INDEX state_id (sid),
CONSTRAINT FOREIGN KEY country_fk(cid) REFERENCES country (cid) ON DELETE SET DEFAULT ON UPDATE CASCADE,
CONSTRAINT FOREIGN KEY state_fk(sid) REFERENCES state (sid) ON DELETE SET DEFAULT ON UPDATE CASCADE
)TYPE=INNODB;

Ok i've worked it out all should work perfectly if I change the ON DELETE SET DEFAULT to ON DELETE SET NULL

Edited by StuRReaL
Link to comment
https://www.neowin.net/forum/topic/280665-foreign-keys/#findComment-585413741
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • That's just silly imo. The lengths that man goes to just to avoid W11 is just nuts. Very, very few home users would do that. I will say this though, he is committed. Btw, I note on askwoody that Woody Leonhard passed away March, 2025 aged 73. His site was one of my favorites back in the day. Belated yes but RIP Woody.
    • Because of the EU (a good thing) newer android devices been getting 5 years worth of security patches. Except some Motorola which found the loop hole, and offer ZERO updates. In addition, Google for years have been making where it can patch some stuff by updating the core Google Play Store itself.  As echoed earlier,  you take the security risk in to your own hand beyond supported.
    • Win11Debloat 06.11.2026 by Razvan Serea Win11Debloat is a lightweight, easy to use PowerShell script that allows you to quickly declutter and customize your Windows experience. It can remove pre-installed bloatware apps, disable telemetry, remove intrusive interface elements and much more. The script also includes many features that system administrators and power users will enjoy. Such as a powerful command-line interface, support for Windows Audit mode and the option to make changes to other Windows users. All changes made by Win11Debloat can be easily reversed, and most removed apps can be restored via the Microsoft Store. A full guide on how to undo the changes is available here. Win11Debloat features: Below is an overview of the key features and functionality offered by Win11Debloat. Please refer to the wiki for more information about the default settings preset. Remove a wide variety of preinstalled apps. Click here for more info. Disable telemetry, diagnostic data, activity history, app-launch tracking & targeted ads. Disable tips, tricks, suggestions & ads across Windows. Disable Windows location services & app location access. Disable Find My Device location tracking. Disable 'Windows Spotlight' and tips & tricks on the lock screen. Disable 'Windows Spotlight' desktop background option. Disable ads, suggestions and the MSN news feed in Microsoft Edge. Hide Microsoft 365 ads on the Settings 'Home' page, or hide the 'Home' page entirely. Disable & remove Microsoft Copilot. Disable Windows Recall. Disable Click to Do, AI text & image analysis tool. Prevent AI service (WSAIFabricSvc) from starting automatically. Disable AI Features in Edge. Disable AI Features in Paint. Disable AI Features in Notepad. Disable the Drag Tray for sharing & moving files. Restore the old Windows 10 style context menu. Turn off Enhance Pointer Precision, also known as mouse acceleration. Disable the Sticky Keys keyboard shortcut. Disable Storage Sense automatic disk cleanup. Disable fast start-up to ensure a full shutdown. ...and more. Once you’ve downloaded the Win11Debloat file (Get.ps1), just follow these quick steps: Locate the Get.ps1 script file. Right-click the file and select Run with PowerShell from the context menu. If prompted by User Account Control (UAC), select Yes to grant the script the necessary administrative permissions. Win11Debloat 06.11.2026 fixes: Fix lock screen spotlight option being disabled when disabling the start recommended section by @Raphire in #619 Fix log message formatting by @Raphire Note The -RemoveCommApps and -RemoveW11Outlook command-line parameters for uninstalling a few specific apps have been removed with this release. If you previously relied on these parameters, please see this wiki page for alternative methods of removing these apps. Download: Win11Debloat 06.11.2026 | Open Source View: Win11Debloat Home Page | Screenshots 1| 2 Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Rookie
      restore went up a rank
      Rookie
    • Very Popular
      AndrewSteel earned a badge
      Very Popular
    • Veteran
      Taliseian went up a rank
      Veteran
    • One Month Later
      Clizby earned a badge
      One Month Later
    • One Month Later
      Timaximus earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      502
    2. 2
      +Edouard
      162
    3. 3
      PsYcHoKiLLa
      154
    4. 4
      ATLien_0
      83
    5. 5
      Steven P.
      79
  • Tell a friend

    Love Neowin? Tell a friend!