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 ...
随机推荐
- 一个配置文件收集多个日志-if根据type类型判断
1.同时收集/var/log/messages日志和secure日志 #vim /etc/logstash/conf.d/system.conf input { file { path => & ...
- TestDirector其他
一.查看连接TD的用户 1.用单击界面上的“Connections”按钮,进入如下图: 二.Licenses管理 1.单击“Licenses”按钮,显示如下图: 2.单击“Modify License ...
- GNU Wget 1.19.4 for Windows
资源地址:https://eternallybored.org/misc/wget/ 然后将工具目录加入环境变量
- vue表格导入
<input id="upload" type="file" @change="importfxx(this)" accept=&q ...
- Linux (x86) Exploit 开发系列教程之六(绕过ASLR - 第一部分)
转:https://bbs.pediy.com/thread-217390.htm 前提条件: 经典的基于堆栈的缓冲区溢出 虚拟机安装:Ubuntu 12.04(x86) 在以前的帖子中,我们看到了攻 ...
- Python并发编程-进程池回调函数
回调函数不能传参数 回调函数是在主进程中执行的 from multiprocessing import Pool import os def func1(n): print('in func1', o ...
- FastReport.Net使用:[38]关系的使用
打印所有成绩 1. 数据源准备 接下来我们需要打印学生成绩,而成绩表中无姓名,我们通过建立Realtion关系来打印数据. 2. 创建Relation关系 在数据视图上的动作下拉菜单中选择“新建关系” ...
- android 进程优先级
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 前台进程 可见进程 服务进程 后台进程 空进程
- manacher算法求最长回文子序列
一:背景 给定一个字符串,求出其最长回文子串.例如: s="abcd",最长回文长度为 1: s="ababa",最长回文长度为 5: s="abcc ...
- [HNOI2006]最短母串问题 --- AC自动机 + 隐式图搜索
[HNOI2006]最短母串问题 题目描述: 给定n个字符串(S1,S2.....,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,......,Sn)都是T的子串. 输入格式: 第 ...