• 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

    • Segra 1.6.0 by Razvan Serea Segra is a free, open-source OBS-powered game recorder offering fast gameplay capture, instant clips, AI highlights, deep game integration, and seamless uploads—perfect for gamers, streamers, and content creators. Lightweight, fast, zero bloat. Segra key features: Automatic Game Recording: Begin capturing gameplay the moment your game launches, with zero manual setup. Instant Clipping: Save important moments instantly using a customizable hotkey—perfect for highlights, montages, or quick shares. Segra AI Highlights: Let Segra automatically detect kills, assists, deaths, and key events to generate polished highlight reels without manual editing. Gameplay Uploads: Upload recordings and clips directly to Segra.tv for fast sharing and cloud access. Deep Game Integration: Enjoy advanced game-data tracking across hundreds of supported titles, enabling smart highlight generation and stat-informed clipping. High-Performance Capture: Record up to 4K at 144 FPS using OBS-powered technology with minimal performance impact, supporting NVENC, AMD VCE, and custom quality controls. Segra Editor: Edit recordings easily with timeline controls, segment management, and event-based navigation to build the perfect clip. Customization Options: Adjust hotkeys, output formats, storage paths, codecs, capture quality, and performance settings for a tailored recording experience. Segra 1.6.0 changelog: Recording: Added HDR support. Grand Theft Auto: Added game integration for deaths (FiveM and RAGE MP supported). Highlights: Added customizable padding for highlights. Replay Buffer: Added a shockwave visual effect when a replay buffer clip is saved. Audio: Increased the maximum sound effects volume from 100% to 200%. Hotkeys: Fixed hotkeys not triggering while unrelated keys were held. Installer: Added code signing to verify publisher identity, branded the installer, and reduced OS security warnings. OBS: Updated the supported OBS version to 32.1.2. Download: Segra 1.6.0 | 74.4 MB (Open Source) View: Segra Homepage | Github | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • According to the developer(s):    
    • Most of the sellers are selling under a business name, and you can't jail a business. The best option would be to blanket ban brick chargers and regulate fixed PD faceplates for wall as a spur from existing sockets. That way they'd be checked with during during an EICR.
    • Totally ridiculous! They wonder why people can't stand their company...
  • Recent Achievements

    • One Month Later
      Clizby earned a badge
      One Month Later
    • One Month Later
      Timaximus earned a badge
      One Month Later
    • Week One Done
      Timaximus earned a badge
      Week One Done
    • Rookie
      FBSPL went up a rank
      Rookie
    • First Post
      davidbazooked earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      508
    2. 2
      PsYcHoKiLLa
      176
    3. 3
      +Edouard
      163
    4. 4
      Steven P.
      86
    5. 5
      ATLien_0
      79
  • Tell a friend

    Love Neowin? Tell a friend!