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 ...
随机推荐
- logstash收集nginx日志
(1)安装nginx 1.安装nginx yum install epel-release -y yum install nginx -y 2.修改日志文件格式为json #vim /etc/ngin ...
- Window 下一台机器配置三个Tomcat实例
下面我们把配置的详细过程写在下面,以供参考:(此例以配置三个Tomcat为例) 1. 下载apache-tomcat-8.0.63,下载下来的文件为apache-tomcat-8.0.63.zip. ...
- Xcode10升级项目报错library not found for -lstdc++.6.0.9
在升级Xcode10后运行项目会发出报了一个错“library not found for -libstdc++.6.0.9”,很简单,就是因为xocde10后这个libstd++.6.0.9库已经不 ...
- JavaWeb 之 AJAX
Ajax ajax:AJAX 是与服务器交换数据的艺术,它在不重载全部页面的情况下,实现了对部分网页的更新 AJAX:Asynchronous JavaScript and XML,异步 javasc ...
- Java 请求webServce接口 不带参数
最近对接了个webService的接口取数据,从网上良莠不齐的代码中找到了个方法, 具体作者已经记不住是谁了,现在把代码贴出来,希望可以帮到大家,代码如下,简单粗暴 public String get ...
- [转]Android ListView最佳处理方式,ListView拖动防重复数据显示,单击响应子控件
Android ListView最佳处理方式,ListView拖动防重复数据显示,单击响应子控件. 1.为了防止拖动ListView时,在列表末尾重复数据显示.需要加入 HashMap<In ...
- IntelliJ IDEA导出设置
导出: [File]->[Export Settings] 导入: [File]->[Import Settings]
- OpenVPN配置网桥模式的一些理解
说明: 1.网桥的作用是将所有的网卡都能直连主机所在的路由器,可以把它当做一个独立的PC. 2.OpenVPN设置成网桥之后,IP设置成主机所在的IP网段,这样客户端连接进来之后就是所在主机所在的网络 ...
- Ubuntu 16.09下iptables通过raw表实现日志输出和调试
1.先配置好raw表日志打点功能 参考:http://www.cnblogs.com/EasonJim/p/8413563.html 2.配置好messages文件 参考:http://www.cnb ...
- Ext.js 中 25种类型的Ext.panel.Tool
通过Ext.panel.Panel的tools配置项来设置Ext.panel.Tool实例. 要注意的一点是,Ext框架提供的Ext.panel.Tool仅包含按钮图标而具体的点击事件处理函数需要我们 ...