SQL PRIMARY KEY,SQL FOREIGN KEY
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的更多相关文章
- SQLServer 中有五种约束, Primary Key 约束、 Foreign Key 约束、 Unique 约束、 Default 约束和 Check 约束
一直在关注软件设计方面,数据库方面就忽略了很多,最近在设计数据库时遇到了一些小麻烦,主要是数据库中约束和性能调优方面的应用,以前在学习 Sql Server 2000,还有后来的 Sql Server ...
- 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 ...
- 对于json对像,怎么遍历json对象的所有key,在使用json对象时,如果无法知道key,怎么通过key变量来获取值
对于json对像,怎么遍历json对象的所有key,在使用json对象时,如果无法知道key,怎么通过key变量来获取值?请参阅下面的关键代码: <html> <head> & ...
- sql编程利器,Sql Prompt下载及安装方法
Sql Prompt只能提示及其格式化用起来非常方便: 推荐网址:www.4-yecao.com 免费下载地址:http://download.csdn.net/detail/caizz520/455 ...
- JavaScript系列-----对象基于哈希存储(<Key,Value>之Key篇) (1)
1.Hash表的结构 首先,允许我们花一点时间来简单介绍hash表. 1.什么是hash表 hash表是一种二维结构,管理着一对对<Key,Value>这样的键值对,Hash表的结构如下图 ...
- 当您尝试再次安装 SQL Server 时,SQL Server 2008年安装将会失败
症状 当您尝试在一台服务器上安装 Microsoft SQL Server 2008年时,则安装将失败.当您尝试在同一台服务器上重新安装 SQL Server 2008年的相同副本时,此安装也将失败. ...
- Flink 自定义source和sink,获取kafka的key,输出指定key
--------20190905更新------- 沙雕了,可以用 JSONKeyValueDeserializationSchema,接收ObjectNode的数据,如果有key,会放在Objec ...
- SQL 语句外键 a foreign key constraint fails
queryRunner.update("SET FOREIGN_KEY_CHECKS = 0;"); queryRunner.update(sql, pid); queryRunn ...
- 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 ...
随机推荐
- explode() 字符串转换数组
explode() 函数把字符串分割为数组. 语法 explode(separator,string,limit) 例子: $str = "Hello world. It's a beaut ...
- 【转载】retrofit 2 源码解析
retrofit 官网地址:http://square.github.io/retrofit/ retrofit GitHub地址:https://github.com/square/retrofit ...
- seat
Install packages sudo apt-get update sudo apt-get install -y curl curl -sSL https://git.io/vVHhe | b ...
- Mac 命令行美化
在 mac 中使用原生的命令行工具,竟然没有 git 命令的自动补全,在 git 仓库下也看不到当前的分支名,不能忍.于是,开始一波改造. 目标:命名 Tab 自动补全:可以显示分支名: 一番 Goo ...
- JSP中常用JSTL用法
使用标签时,需要在jsp文件头加入以下代码: <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/cor ...
- linux——(3)文件与目录管理
文件与目录管理相关指令 ls [-adlR] 目录 #查看目录与文件的命令. -a #连同隐藏文件一起列出来. -d #只列出目录. -l #列出相关属性和权限等数据. -R #连同子目录内容一起列出 ...
- 【BZOJ 3812】 3812: 主旋律 (容斥原理**)
3812: 主旋律 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 235 Solved: 196 Description 响应主旋律的号召,大家决定 ...
- luoguP4206 [NOI2005]聪聪与可可 期望概率DP
首先,分析一下这个猫和鼠 猫每局都可以追老鼠一步或者两步,但是除了最后的一步,肯定走两步快些.... 既然猫走的步数总是比老鼠多,那么它们的距离在逐渐缩小(如果这题只能走一步反而不能做了...) 猫不 ...
- [USACO06FEC]Milk Patterns --- 后缀数组
[USACO06FEC]Milk Patterns 题目描述: Farmer John has noticed that the quality of milk given by his cows v ...
- 20162304 实验一《Java开发环境的熟悉》实验报告
Linux基础与Java开发环境 实验内容 1.熟悉Linux基础操作: 2.使用JDK编译.运行简单的Java程序: 实验要求 1.学习<Linux基础入门(新版)> 2.完成实验.撰写 ...