A primary key is defined as a column or a group of column that their value are always be unique. Normally, NULL value will never be allowed in this column to be part of this column’s records.

EXAMPLE :

Let’s say we want to create a table name Users. The PRIMARY KEY will be user_id for that table.

SQL statement is:

CREATE TABLE USERS (
user_id SMALLINT NOT NULL PRIMARY KEY,
user_name VARCHAR(50),
)

OR

CREATE TABLE USERS (
user_id SMALLINT NOT NULL ,
user_name VARCHAR(50),
PRIMARY KEY(user_id)
)

Foreign key is use to referential to the unique parent table.
Foreign Key will point to the Primary key of the Parent table.
This will not allow primary from parent table to be deleted without clearing the record of the child table.

EXAMPLE :

Let’s say we want to create 2 tables name users and departments.
and have to create FOREIGN KEY in users table to link to the department table

SQL statement :

CREATE TABLE DEPARTMENTS (
department_id SMALLINT NOT NULL PRIMARY KEY,
department_name VARCHAR(50),
)

And the users table will be like this:

SQL statement is:

CREATE TABLE USERS (
user_id SMALLINT NOT NULL PRIMARY KEY,
user_name VARCHAR(50),
department_id SMALLINT NOT NULL ,
FOREIGN KEY (department_ID) references DEPARTMENTS

)

SQL PRIMARY KEY,SQL FOREIGN KEY的更多相关文章

  1. SQLServer 中有五种约束, Primary Key 约束、 Foreign Key 约束、 Unique 约束、 Default 约束和 Check 约束

    一直在关注软件设计方面,数据库方面就忽略了很多,最近在设计数据库时遇到了一些小麻烦,主要是数据库中约束和性能调优方面的应用,以前在学习 Sql Server 2000,还有后来的 Sql Server ...

  2. sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1091, "Can't DROP 'users_ibfk_1'; check that column/key exists") [SQL: ALTER TABLE users DROP FOREIGN KEY users_ibfk_1]

    flask 迁移数据库报错 报错: sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1091, "Can't DROP ...

  3. 对于json对像,怎么遍历json对象的所有key,在使用json对象时,如果无法知道key,怎么通过key变量来获取值

    对于json对像,怎么遍历json对象的所有key,在使用json对象时,如果无法知道key,怎么通过key变量来获取值?请参阅下面的关键代码: <html> <head> & ...

  4. sql编程利器,Sql Prompt下载及安装方法

    Sql Prompt只能提示及其格式化用起来非常方便: 推荐网址:www.4-yecao.com 免费下载地址:http://download.csdn.net/detail/caizz520/455 ...

  5. JavaScript系列-----对象基于哈希存储(<Key,Value>之Key篇) (1)

    1.Hash表的结构 首先,允许我们花一点时间来简单介绍hash表. 1.什么是hash表 hash表是一种二维结构,管理着一对对<Key,Value>这样的键值对,Hash表的结构如下图 ...

  6. 当您尝试再次安装 SQL Server 时,SQL Server 2008年安装将会失败

    症状 当您尝试在一台服务器上安装 Microsoft SQL Server 2008年时,则安装将失败.当您尝试在同一台服务器上重新安装 SQL Server 2008年的相同副本时,此安装也将失败. ...

  7. Flink 自定义source和sink,获取kafka的key,输出指定key

    --------20190905更新------- 沙雕了,可以用  JSONKeyValueDeserializationSchema,接收ObjectNode的数据,如果有key,会放在Objec ...

  8. SQL 语句外键 a foreign key constraint fails

    queryRunner.update("SET FOREIGN_KEY_CHECKS = 0;"); queryRunner.update(sql, pid); queryRunn ...

  9. sql: Query to Display Foreign Key Relationships and Name of the Constraint for Each Table in Database

    --20170505 --塗聚文 Geovin Du CREATE DATABASE DuMailSystem GO USE DuMailSystem GO --1查詢表的及备注说明 SELECT S ...

随机推荐

  1. 提起Ajax请求的方式(POST)

    前言 => 是ES6中的arrow function x=>x+6 就相当于 function(x){ return x+6; } 正文 XMLHttpRequest a=new XMLH ...

  2. CF815C Karen and Supermarket [树形DP]

    题目传送门 Karen and Supermarket On the way home, Karen decided to stop by the supermarket to buy some gr ...

  3. Python并发编程-管道

    管道的作用- 两个进程间传递消息 from multiprocessing import Pipe, Process def func(conn1,conn2): conn2.close() #子进程 ...

  4. Xamarin 2017.9.19更新

     Xamarin 2017.9.19更新   本次更新是添加Xamarin.iOS对iOS 11和Xcode 9的支持.Visual Studio 2017升级到15.3.5获得更新功能.Visual ...

  5. 应用服务攻击工具clusterd

    应用服务攻击工具clusterd   clusterd是一款Python语言编写的开源应用服务攻击工具.该工具支持七种不同的应用服务平台,如JBoss.ColdFusion.WebLogic.Tomc ...

  6. Kali Linux缺少ifconfig命令

    Kali Linux缺少ifconfig命令   ifconfig是配置和查看网络的基础命令.在某些Kali Linux版本中,可能会缺少ifconfig命令.这个时候,用户需要手动安装该命令.该命令 ...

  7. iOS 9音频应用播放音频之播放控制暂停停止前进后退的设置

    iOS 9音频应用播放音频之播放控制暂停停止前进后退的设置 ios9音频应用播放控制 在“iOS 9音频应用播放音频之ios9音频基本功能”一文可以看到AVAudioPlayer类有很多的属性以及方法 ...

  8. android jni c C++ 实现下载

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha android jni c C++ 实现下载

  9. PHP 笔记——操作MySQL数据库

    1. 连接MySQL服务器 ​ mysqli_connect :此函数是该函数的别名: mysqli::__construct() mysqli mysqli_connect ( [string se ...

  10. PHP_EOL 换行符

    换行符unix系列用 \nwindows系列用 \r\nmac用 \rPHP中可以用PHP_EOL来替代,以提高代码的源代码级可移植性 如:  <?php    echoPHP_EOL;    ...