Giter VIP home page Giter VIP logo

aidencsmith-foreignkey's Introduction

Foreign Key Constraint

We know how to create simple tables. Now we need to relate these tables to each other. This is a crucial part of relational databases. Foreign keys are used to link tables together, and to enforce referential integrity.

A foreign key is a column in a table which matches another column in another table. The most common way this is done is to match the foreign key column to the primary key in another table.

For example we have two tables, artists and songs.

artist song
id artist
1 The Beatles
2 The Eagles
3 Led Zeppelin
id song
1 Let It Be
2 The Eagles
3 Kashmir

These two tables could be related. 'Let It Be' and 'Imagine' are both by The Beetles. 'Kashmir' is Led Zeppelin. One of the two tables will need a foreign key column which will match the primary key in the other table. Let's add this column to the songs table so they look like this:

artist song
id artist
1 The Beatles
2 The Eagles
3 Led Zeppelin
id song artist_fk
1 Let It Be 1
2 Imagine 1
3 Kashmir 3

Note how the artist_fk column's values link the songs with the artists in the other table. With this column and a foreign key constraint in place the tables are linked. If we tried to add a new song to the songs table that did not first have a matching artist in the artist table we would be unable to insert that record. This is referential integrity. The foreign key constraint stops us from violating that integrity.

To create a table with a foreign key constraint we add the constraint after the column definition. For example:

CREATE TABLE song (
    id SERIAL PRIMARY KEY,
    song VARCHAR(100),
    artist_fk INT REFERENCES artist(id)
);

Note that this syntax isn't uniform across all flavors of SQL. The line artist_fk INT REFERENCES artist(id) adds our foreign key constraint. The column, artist_fk holds INT type data and REFERENCES the id column of the artists table.

Additional Reference Material

Lab

For the following problems, consider the site_user table below:

site_user
id firstname lastname
1 'Steve' 'Garcia'
2 'Alexa' 'Smith'
3 'Steve' 'Jones'
4 'Brandon' 'Smith'
5 'Adam' 'Jones'

Problem 1

Create a new table in the file problem1.sql named Post. This table will contain posts linked to specific users. It should have three columns: id (which is of type serial, and is a primary key), post (of type varchar(255)), and user_fk (which is of type int, but is also a foreign key referencing the id column of the table site_user).

Be sure that user_fk column should be an int foreign key that references the id column in the site_user table. The table will look like this:

post
id post user_fk
... ... ...

aidencsmith-foreignkey's People

Contributors

kevinchilds avatar aidencsmith avatar revvingupthathill avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.