Lazy loaded image
数据分析
Lazy loaded imageSQL Tutorial - Full Database Course for Beginners
Words 16633Read Time 42 min
Sep 28, 2025
Nov 16, 2025
type
status
date
slug
summary
tags
category
icon
password
comment
视频发布日期:2018年7月3日
视频时长:4小时20分钟
导言
In this course, we'll be looking at database management basics and SQL using the MySQL RDBMS. Want more from Mike? He's starting a coding RPG/Bootcamp - https://simulator.dev/ The course is designed for beginners to SQL and database management systems, and will introduce common database management topics. Throughout the course we'll be looking at various topics including schema design, basic C.R.U.D operations, aggregation, nested queries, joins, keys and much more. You can get PopSQL to follow along here: http://popsql.com/freecodecamp 🔗Company Database Code: https://www.giraffeacademy.com/databa... ❤️ Try interactive Databases courses we love, right in your browser: https://scrimba.com/freeCodeCamp-Data... (Made possible by a grant from our friends at Scrimba) ⭐️ Contents ⭐ ⌨️ (0:00) Introduction ⌨️ (2:36) What is a Database? ⌨️ (23:10) Tables & Keys ⌨️ (43:31) SQL Basics ⌨️ (52:26) MySQL Windows Installation ⌨️ (1:01:59) MySQL Mac Installation ⌨️ (1:15:49) Creating Tables ⌨️ (1:31:05) Inserting Data ⌨️ (1:38:17) Constraints ⌨️ (1:48:11) Update & Delete ⌨️ (1:56:11) Basic Queries ⌨️ (2:08:37) Company Database Intro ⌨️ (2:14:05) Creating Company Database ⌨️ (2:30:27 ) More Basic Queries ⌨️ (2:26:24) Functions ⌨️ (2:45:13) Wildcards ⌨️ (2:53:53) Union ⌨️ (3:01:36) Joins ⌨️ (3:11:49) Nested Queries ⌨️ (3:21:52) On Delete ⌨️ (3:30:05) Triggers ⌨️ (3:42:12) ER Diagrams Intro ⌨️ (3:55:53) Designing an ER Diagram ⌨️ (4:08:34) Converting ER Diagrams to Schemas Course developed by Mike Dane. Check out his YouTube channel for more great programming courses:    / @giraffeacademy   🔗The Giraffe Academy website: http://www.giraffeacademy.com/ -- Learn to code for free and get a developer job: https://www.freecodecamp.org Read hundreds of articles on programming: https://medium.freecodecamp.org

What is a Database (DB)?

  • Any collection of related information
    • Phone Book
    • Shopping list
    • Todo list
    • Your 5 best friends
    • Facebook’s User Base
  • Databases can be stored in different ways
    • On paper
    • In your mind
    • On a computer
    • This powerpoint
    • Comments Section
notion image
  • Computers are preferred tools to store data.
    • Computers are great at keeping track of large amounts of information

Database Management Systems (DBMS)

  • A special software program that helps users create and maintain a database
    • Makes it easy to manage large amounts of information
    • Handles Security
    • Backups
    • Importing/exporting data
    • Concurrency
    • Interacts with software applications
举例:Amazon.com Database Diagram
notion image
Amazon.com will interact with the DBMS in order to create, read, update and delete information

C.R.U.D

notion image
  • four main operations
C: Create database from information
R: Read database from information
U: Update
D: Delete
CRUD stands for Create, Read, Update, and Delete—the four basic ways / main operations you interact with any database.
  • Create: Insert new data into tables.
  • Read: Retrieve data with SELECT statements.
  • Update: Change existing records.
  • Delete: Remove records you no longer need.

Two Types of Databases

Relationship Databases (SQL)
  • Organize data into one or more tables
    • Each table has columns and rows
    • A unique key identifies each row
Non-Relational (noSQL / not just SQL)
  • Organize data is anything but a traditional table
    • Key-value stores
    • Documents (JSON, XML, etc)
    • Graphs
    • Flexible Tables

Relational Databases (SQL)

notion image
  • Relational Database Management Systems (RDBMS)
    • Help users create and maintain a relational database
      • mySQL, Oracle, postgreSQL, maria DB, etc.
  • Structured Query Language (SQL)
    • Standardized language for interacting with RDBMS
    • Used to perform C.R.U.D operations, as well as other administrative tasks (user management, security, backup, etc).
    • Used to define tables and structures
    • SQL code used on one RDBMS is not always portable to another without modification.

Non-Relational Databases (noSQL / not just SQL)

notion image
  • JSON:读作 J-son
  • Non-Relational Database Management Systems (NRDBMS)
    • Help users create and maintain a non-relational database
      • mongoDB, dynamoDB, apache cassandra, firebase, etc
  • Implementation Specific
    • Any non-relational database falls under this category, so there’s no set language standard.
    • Most NRDBMS will implement their own language performing C.R.U.D and administrative operations on the database.

Database Queries

Queries are requests made to the database management system for specific information
As the database’s structure become more and more complex, it becomes more difficult to get the specific pieces of information we want.
A google search is a query

[Wrap Up]

  • Database is any collection of related information
  • Computer are great for storing databases
  • Database Management Systems (DBMS) make it easy to create, maintain and secure a database.
  • DBMS allow you to perform the C.R.U.D operations and other administrative tasks
  • Two types of Databases, Relational & Non-Relational
  • Relational databases use SQL and store data in tables with rows and columns
  • Non-Relational data store data using other data structures

Tables and Keys

An example of a table:
A student table
A student table
  • Column: vertical entry
    • E.g. major column
  • Row: horizontal entry
  • Primary key: special column
    • Uniquely identify the row and database
    • here: student id
    • (uniquely) identify a specific row
Another example:
notion image
  • Primary key: email
Example:
notion image
  • Primary key: emp id
    • a surrogate key is basically a key that has no mapping to anything in the real world
      代理键 (Surrogate Key)是数据仓库中用于唯一标识表中每一行记录的人工键 (Artificial Key),通常是一个自增的整数或全局唯一标识符 (GUID)。 与业务系统中的自然键 (如身份证号、订单号等)不同,代理键不包含任何业务含义,仅用于技术实现。 代理键不依赖业务规则,仅用于数据关联。 即使业务键发生变化,代理键保持不变,避免数据关系断裂。 通常使用整数类型,占用空间小,索引效率高。
  • a natural key: a key that has a mapping or has a purpose in the real world, not just in the database.
  • a foreign key: an attribute that we can store on a database table that will link us to another database table.
    • a foreign key stores the primary key of a row in another database table.
ChatGPT 讲解 surrogate key(代理键)natural key(自然键)foreign key(外键)
以下是对 surrogate key(代理键)natural key(自然键)foreign key(外键) 的通俗讲解和对比,帮助你更好理解它们的区别和应用场景。

1. 🆔 Surrogate Key(代理键)

定义:
代理键是数据库中为某一行数据人为生成的唯一标识,一般是自增的整数或 GUID(全球唯一标识符)。
不携带任何实际业务含义,仅仅用于技术层面上的唯一标识和关联
例子:
一个“用户”表中,你可以添加一个 UserID 字段作为代理键:
UserID 是数据库自动生成的,跟现实世界无关,只是用来唯一识别用户。
优点:
  • 不会因业务规则变动而改变(例如,邮箱换了,UserID 还是不变)。
  • 性能高,整数索引效率好。
  • 避免自然键重复或不稳定造成的问题。

2. 🗝️ Natural Key(自然键)

定义:
自然键是数据中的真实字段,在现实世界中也能代表一个对象的身份,有实际业务含义,可以天然地用于唯一标识一条记录。
例子:
  • 身份证号(一个人唯一的标识)
  • ISBN(图书编号)
  • 学号、订单号等
IDCardNumber 就是自然键。
优点:
  • 业务上直观、有含义。
  • 可以避免冗余的“额外字段”。
缺点:
  • 可能变动(邮箱、手机号会变)。
  • 有时过长或复杂(不利于索引或连接)。
  • 不适合用作主键时,通常会引入代理键。

3. 🔗 Foreign Key(外键)

定义:
外键是指一个表中用来引用另一个表主键的字段,用于建立表与表之间的关联关系。
例子:
你有两个表:
用户表(Users)
订单表(Orders)
这里 Orders 表中的 UserID 就是外键,它引用了 Users 表中的主键 UserID,用于说明“这笔订单是哪个用户下的”。
作用:
  • 保证数据一致性和完整性(防止孤儿记录)。
  • 支持联表查询和数据建模。

总结对比表:

项目
Surrogate Key(代理键)
Natural Key(自然键)
Foreign Key(外键)
是否有业务含义
❌ 没有
✅ 有
❌/✅ 通常没有,但引用业务表
是否唯一
✅ 是主键
✅ 是主键
❌ 自身不一定唯一(引用唯一键)
典型形式
自增 ID、GUID
身份证号、邮箱、ISBN 等
引用另一个表的主键字段
使用目的
技术实现、性能优化
业务建模、数据直观
表与表之间的关联

如果你想了解它们在实际数据库设计中的使用策略,我也可以进一步说明什么时候推荐用代理键、什么时候自然键更好。
A branch table
A branch table
讲解
Over here we can define which branch a specific employee belongs to, by referring to the ID, the primary key of the branch.
[32:00] So, here, Jan Levinson, her branch ID is 1, which means she is in the corporate branch, right? Because branch ID number 1 is corporate.
Michael Scott, his branch ID is 2, which means he’s in the Scranton branch. So this number is mapping this row over here into this other table.
And that’s what a foreign key does.

[32:56] And over here you'll see on the branch table, I also defined another foreign key which is manager ID. mgr_id, that would stand for manager ID.
And now this is actually going to be a foreign key which connects branch to the employee table. So, manager ID is going to be the ID of a particular employee who is the manager of the branch.
So, let’s take a look at the Scranton branch. So, the branch ID is 2.
The name of the branch is Scranton, and the manager ID is 101. So, let’s go check it out.
So, over here in the employee table, employee 101 is Michael Scott.
What that means is that Michael Scott is the manager of the Scranton branch, right?
We were able to define that relational by using these foreign keys.
So, down here, the Stamford branch, the manager is 102.
If we come up here, we can see employee 102 is Josh Porter.
So, Josh Porter is the manager of the Stamford branch.
Corporate over here, the manager ID is 108.
So, over here we didn’t actually include 108 over there.
So, obviously, this isn’t like, you know, a huge table of employees.
But the manager for the corporate branch would be the employee with the ID of 108, whatever that would be inside of this table.
So, that’s what a foreign key does.
A foreign key is essentially just a way that we can define relationships between the two tables.

 
notion image
讲解
[34:50] But I also added another attribute or other column over here which is super_id. And that stands for supervisor ID.
And so, what this is going to do is it’s going to define who is the supervisor of a particular employee.
And what’s cool about this foreign key is it’s actually relating back to the same table. So, an employee can be a supervisor of another employee.
So, over here we have Angela Martin, right? Her employee ID is 103. And her supervisor ID is 101.
And that means her supervisor is the employee with the ID 101.
So, if we come over here we can look.
Angela Martin’s supervisor is Michael Scott because Michael Scott has an employee ID of 101.
So, Michael Scott.
Michael Scott’s super ID is 100.
That means Michael Scott’s supervisor is the employee with ID 100, which is Jan Levison. So, we can these this foreign key over here on the employee table to define relationships between employees.
notion image
讲解
A branch supplier would basically define who the suppliers are for specific branches. 36:39 So, we have these branches. 36:41 Maybe they're selling a certain product. 36:43 Maybe they're selling like paper products. 36:44 And the branch suppliers would define who are the suppliers for that branch. 36:48 And you'll notice up here, the primary key actually consists of two columns. 36:54 And this is what we would call a composite key or a composite key. 36:58 And a composite key is basically a key that needs two attributes. 37:03 So, this key is made up of two columns, right? 37:07 It’s made up of branch_id and supplier_name. 37:10 So, branch_id is actually going to refer to the specific branch. 37:15 And supplier_name is going to refer to the specific supplier, right? 37:20 So, we can say over here that Hammer Mill supplies paper to branch number 2. 37:24 So Hammer Mill supplies paper to Scranton. 37:28 Uni-ball supplies writing utensils to branch ID number 2, which is Scranton. 37:33 Patriot Paper supplies paper to branch ID number 3 which is Stamford. 37:38 So, inside of this branch supplier table, I’m able to define which different suppliers are supplying what to which different branches. 37:46 Now, the reason that I need this composite key or this composite key, is because the supplier name doesn’t uniquely identify each row. 37:56 And the branch_id doesn’t uniquely identify each row. 38:00 Only together can they uniquely identify each row, right? 38:04 So, for example, supplier name, Hammer Mill. 38:07 You’ll notice that Hammer Mill shows up here and it also shows up here. 38:10 So, Hammer Mill supplies to branch number 2. 38:12 And they also supply to branch number 3. 38:14 Uni-ball supplies to branch number 3 and Uni-ball supplies to branch number 2. 38:19 So, this column has repeated values, right? 38:22 This column has Hammer Mill repeated. 38:25 It has Uni-ball repeated. 38:26 branch_id also has those things repeated, right? 38:30 So, branch_id 2 shows up here a bunch of times. 38:32 branch_id 3 shows up here a bunch of times, right? 38:35 And so, the branch_id column can’t uniquely identify the table. 38:40 And the supplier name column can’t uniquely identify the table. 38:44 Only together can they uniquely identify it, right? 38:47 So, we would say Hammer Mill supplies branch 2. 38:50 That combination only shows up once. 38:53 We wouldn't put that combination again because we’re already defining it. 38:57 So, Hammer Mill supplies branch 2. 38:59 Hammer Mill supplies branch 3. 39:01 These two rows are uniquely identified by the identified by the supplier name and the branch ID. 39:07 And so, over here in these employee and the branch table, we only find one column as the primary key. 39:13 But over here, we define two columns as the primary key, which would be a composite key. 39:18 And that’s actually pretty common, where we're going to have two columns uniquely identifying each row.
  • branch_id 和 supplier_name都有重复的值,所以它们单独都不能identify,但两者结合可以identify。
[One more example]
notion image
  • Works_With Table
    • 给客户卖出多少
    • composite key: emp_id 和 client_id
    • emp_id和client_id都是 foreign key
page icon
ChatGPT 总结 primary key/surrogate key/natural key/foreign key/composite key
我帮你用简单的例子解释这几个数据库中的常用键(keys),顺序按关联性排列,更容易理解:

1. Primary Key(主键)

  • 定义:唯一标识表中每一条记录的字段(或字段组合),不能重复,也不能为空。
  • 例子:在“学生”表中,用学号 student_id 作为主键,因为每个学生有唯一的学号。

2. Surrogate Key(代理键)

  • 定义:人为生成的、没有实际业务含义的唯一标识符,通常是自增数字或UUID。
  • 例子:虽然学生有学号,但为了方便数据库设计,给每个学生一个自动生成的ID,比如 student_auto_id = 1, 2, 3...,这个ID就是代理键。

3. Natural Key(自然键)

  • 定义:在业务上天然唯一的字段,可以直接作为唯一标识的键。
  • 例子:学生的身份证号 ID_number,它本身就是唯一的且有实际意义,所以可以用作自然键。

4. Foreign Key(外键)

  • 定义:一个表中的字段,用来关联另一个表的主键,保证数据的参照完整性。
  • 例子:在“选课”表中,有 student_id 字段,它指向“学生”表的主键 student_id,表示这条选课记录属于哪个学生。

5. Composite Key(复合键/组合键)

  • 定义:由多个字段组合起来作为主键,联合唯一标识一条记录。
  • 例子:在“选课”表中,单独 student_idcourse_id 不能唯一确定一条记录,但两个字段组合 (student_id, course_id) 可以唯一确定学生选了哪门课,这就是复合主键。

简单总结表格

术语
含义
例子
Primary Key
唯一标识一条记录
学生表里的 student_id
Surrogate Key
系统生成的无业务含义的唯一ID
自动生成的学生ID(1,2,3,...)
Natural Key
业务上天然唯一的字段
学生身份证号 ID_number
Foreign Key
指向另一张表主键的字段
选课表中的 student_id
Composite Key
多字段组合起来作为主键
选课表中的 (student_id, course_id)

SQL Basics

Structured Query Language (SQL)

  • SQL is a language used for interacting with Relational Database Management Systems (RDBMS)
    • You can use SQL to get the RDBMS to do things for you
      • Create, retrieve, update & delete data
      • Create & manage databases
      • Design & create database tables
      • Perform administration tasks (security, user management, import/export, etc)
  • SQL implementations vary between systems
    • Not all RDBMS’ follow the SQL standard to a ‘T’
    • The concepts are the same but the implementation may vary
  • SQL is actually a hybrid language, it’s basically 4 types of languages in one
    • Data Query Language (DQL)
      • Used to query the database for information.
      • Get information that is already stored there
    • Data Definition Language (DDL)
      • Used for defining database schemas.
    • Data Control Language (DCL)
      • Used for controlling access to the data in the database.
      • User & permissions management
    • Data Manipulation Language (DML)
      • Used for inserting, updating and deleting data from the database.

Queries

notion image
  • A query is a set of instructions given to the RDBMS (written in SQL) that tell the RDBMS what information you want it to retrieve for you
    • TONS of data in a DB
    • Often hidden in a complex schema
    • Goal is to only get the data you need

MySQL Windows Installation

What is MySQL?
MySQL is a popular open-source relational database management system (RDBMS). It is used to store, manage, and retrieve data for websites, applications, and other software.

Key points about MySQL:

  • Relational database: Organizes data into tables with rows and columns.
  • Open source: Free to use and modify, with commercial versions available.
  • Widely used: Powers many websites, including big platforms like Facebook, WordPress, and many others.
  • Supports SQL: Uses Structured Query Language (SQL) to query and manage data.
  • Cross-platform: Works on Windows, Linux, macOS, and more.
  • Client-server architecture: Runs as a server that clients connect to for data access.

In short, MySQL helps you efficiently store and access structured data in your applications.
  1. search in Google for “mysql community server”
    1. notion image
  1. 下载,点击 “no thanks, just start my download”
    1. notion image
  1. 不用Developer Default,因为会下载很多东西。点击Custom。
    1. notion image
  1. 点击MySQL Server - 下面的选项点击,右侧出现
    1. notion image
  1. MySQL Shell 下面的内容同样放到右侧
    1. 以上两样是讲师用到的。如有需要可以全部下载(Developer Default)
      我安装的时候选的是Typical
      notion image
  1. 一路Next,设置 MySQL Root Password(自己要记住,后续用)
    1. MySQL Root Password
      Password
      notion image
  1. Create a database that I can use
    1. 在搜索栏点击MySQL… Command Line Client
      notion image
  1. 单击打开,Enter password
    1. notion image
  1. 输入 create database girrafe; hit Enter
    1. notion image
现在使用PopSQL
  • 更好Visualize
  1. 搜索popsql
  1. Download and sign in
    1. notion image
  1. Connect to your database
    1. notion image
    2. Type: MySQL
    3. Hostname: localhost
    4. Port: 3306
    5. Database: giraffe
      1. 刚才创建的名字
    6. Username: root
    7. Password
      1. 刚才的密码
      我登录后没有这些设置。

MySQL Mac Installation

  1. 搜索mysql community server
  1. 下载mac OS第一个
    1. notion image
  1. temperory password:localhost后面的,记得copy
    1. notion image
  1. set up MySQL server
      • search for system preferences,弹出:
      notion image
  1. click on MySQL
    1. 输入密码
      notion image
      a database server is running on our computer
  1. Update our password:
    1. open up terminal
      1. type in mysql
        • not found
        notion image
        notion image
    2. type in echo ‘export PATH=/usr/local/mysql/bin:$PATH’>>~/.bash_profile
      1. notion image
        the command is now working
    3. type in mysql -u root -p
      1. 输入刚才的temporary password
        notion image
    4. change password
      1. type in ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘password’;
        notion image
    5. 重新type in mysql -u root -p,这次可以输入新密码
      1. notion image
  1. type out create database giraffe;
    1. notion image
  1. type in exit
    1. clear up
      notion image
  1. log in again
    1. mysql -u root -p
      notion image
       
可以在terminal (终端)中完成SQL各种工作,下面将使用PopSQL
Crete a database
  1. type out create database giraffe
    1. create a database called giraffe
      semicolon
      click enter
      notion image
  1. 下面将使用 PopSQL [optional]
    1. 下载
    2. 设置
      1. Type: MySQL
      2. Hostname: localhost
      3. Post: 3306
        1. default
      4. Database
        1. database的名字
      5. Username: roots
      6. click connect

Creating Tables

[以下演示在PopSQL里面]
  • SQL demand: create table

Datatypes:

Most common datatypes
Most common datatypes
  1. INT: integer
  1. DECIMAL(10,4)
  1. VARCHAR: variable char/ variable character
    1. /ˈværiəbl/
    2. VARCHAR(100) 储存100 characters
 

Create a table

notion image
  • student_id: primary key - uniquely identify each row
  • 或者 create table,大写更好identify
  • student: SQL reserved words
  • name VARCHAR(1000): out of bounds of reality
  • define each column
      1. student_id
      1. name
      1. major
  • click run
    • notion image
 
  • 鼠标点击到 DESCRIBE student;这一行后,点击Run,可以运行到这一行。
    • notion image

Delete & Modify a Table

  • click run
    • notion image
 
  • Add an extra column to the table
  • DECIMAL(3,2): three total digits with two of the digits occurring after the decimal point
  • 鼠标点到这一行,click run
    • notion image
  • gpa一行就删去了

Insert Values

  • INSERT INTO student VALUES(); full statement
    • () 里面可以放想要store的information
  • string需要在 single quotes ‘’里面
  • click run
  • click run
    • notion image
  • click run
    • notion image
 
  • not including major
  • specify what information you wanna include in the table
 
  • Click 2次run 会出现:Duplicate entry ‘3’ for key ‘PRIMARY’
  • 换成4,则Work
    • notion image

Constraints

  • DROP TABLE student; — there’s no such table
  • Constrainst:
    • NULL: doesn’t have a value
    • NOT NULL: can’t be NULL
    • UNIQUE
    • DEFAULT value
       
      AUTO_INCREMENT
      好的,我们来仔细解释 AUTO_INCREMENT

      1. 定义

      AUTO_INCREMENT 是数据库(如 MySQL)中一个字段属性,用于 自动给新插入的记录生成一个递增的整数值
      通常用于 主键(primary key),这样你每次插入数据时不用手动指定唯一 ID。

      2. 特点

      1. 自动递增
          • 第一个插入的记录默认从 1 开始(可以修改起始值),第二条自动 +1,第三条再 +1,以此类推。
      1. 唯一性
          • 通常和主键搭配使用,保证每条记录有唯一标识。
      1. 只作用于整数类型
          • 常用 INTBIGINT

      3. 例子

      查询结果:
      student_id
      name
      major
      1
      Jack
      Biology
      2
      Kate
      Siology
      如果你再插入一条记录,student_id 会自动变成 3。

      💡 小提示
      • 如果你手动给 student_id 指定了值,MySQL 会用你指定的值,并从这个值继续递增下一条自动生成的值。
      • AUTO_INCREMENT 字段必须是索引(通常是主键)。

      老师的演示:即使
      • 结果依然会出现 student_id
  • 如果运行,不可以。因为上面我们规定了name不可以是NULL
PRIMARY KEY
好的,我们仔细来看你写的 SQL 和问题。你问的是 primary key 的意思,我给你详细解释一下。

1. Primary Key 的定义

在数据库中,Primary Key(主键) 是用来唯一标识表中每一行(记录)的字段或字段组合。它有几个特点:
  1. 唯一性(Unique)
    1. 每个主键值在表中都必须唯一,不能重复。例如,你的 student_id 设置为主键,所以每个学生的 student_id 都不能相同。
  1. 非空(NOT NULL)
    1. 主键字段不能为 NULL,必须有值。
      这保证了每条记录都能被唯一识别。
  1. 单表唯一标识
    1. 主键通常用来作为表的标识符,也可以用作其他表的外键(foreign key)建立关联。

2. 你的例子分析

  • student_id 是主键 → 不能重复,也不能为 NULL
  • name 是非空 → 不能为 NULL
  • major 是唯一 → 不允许重复,但可以为 NULL(除非你加上 NOT NULL)

3. 插入数据问题

  • 第 3 行失败,因为 name 列不允许为 NULL
  • 第 4 行失败,因为 major 列是 UNIQUE,不能重复 'Biology'

✅ 总结:primary key 就是用来唯一标识每条记录的列(或列组合),它不能重复也不能为 NULL。

如果你愿意,我可以画一个小图帮你直观理解 primary key 与 UNIQUE 的区别
你想让我画吗?

Update & Delete

  • update and delete rows/entries inside a table
Run
notion image
  • WHERE: conditions
  • Update specific thing
Run
  • student id是4的Jack:Bio变为Comp Sci
notion image
  • OR logic
Run
  • Jack和Claire的学科变为了Biochemistry
notion image
把鼠标点到第一行 SELCT * FROM student; ,然后run
第一行的学生名变为了Tom,major变为了undecided
notion image
把鼠标点到第一行 SELCT * FROM student; ,然后run
第五行Mike被删掉了
notion image
把鼠标点到第一行 SELCT * FROM student; ,然后run
Tom也被删去了
notion image
  • Deleted everything

Basic Queries

  • getting information from the database
    • SELECT keyword
In SQL, a query is basically a request you make to the database to retrieve, manipulate, or modify data. Think of it as asking the database a question and getting an answer in the form of data.

1. Types of SQL Queries

  1. SELECT query – to retrieve data
    1. → This asks the database: "Give me the name and major of all students."
  1. INSERT query – to add new data
    1. → Adds a new student record to the table.
  1. UPDATE query – to modify existing data
    1. → Changes Alice’s major to Physics.
  1. DELETE query – to remove data
    1. → Deletes Alice’s record from the table.

2. Key Points

  • A query is any SQL statement that interacts with the database.
  • SELECT queries are the most common because you often want to read data.
  • Queries can include conditions, sorting, joining multiple tables, etc.

💡 Example: A more advanced SELECT query
This asks: "Give me the names and majors of all students who study Biology, and sort the results by name."

  • 也可以写在一行
  • *表示所有
RUN
notion image
RUN
只出现 name和major
notion image
RUN
notion image
RUN
notion image
  • not returning student_id, but can still order by it
 
order them by major first, and then by student_id
notion image
RUN
notion image
  • Filtering : WHERE condition
RUN
notion image
RUN
notion image
  • less than
  • greater than
  • less than and equal to
  • greater than and equal to
  • equal to
  • not equal to <>
  • major not equal to Chemistry
RUN
notion image
  • IN keyword
  • if the name is Claire, Kate, Mike, gonna select that
RUN
notion image
RUN
notion image

Company Database Info

notion image
  • Primary key: student id
Complex database schema:
notion image
第一个表格 Employee:
  • primary key: emp_id 【标红】
  • foreign key: super_id, branch_id【标绿】
Branch Table:
  • foreign key: mrg_id
Client Table:
  • foreign key: branch_id

Creating Company Database

  • 依旧是在PopSQL里面演示
🔗Company Database Code: https://www.giraffeacademy.com/databa...
ALTER TABLE 是什么?
在 SQL(结构化查询语言)里,ALTER TABLE修改表结构的命令
它的作用是:在不删除表的情况下,对已有表的结构进行修改

1. 基本语法

table_name:你要修改的表名
操作类型:可以是增加列、修改列、删除列、增加约束等

2. 常见操作

(1)添加列

  • employees 表增加一个 age 列,类型为整数。

(2)删除列

  • 删除 employees 表的 age 列。

(3)修改列类型

  • age 列的数据类型改为 BIGINT

(4)重命名列(不同数据库语法不同)

(5)添加约束

  • 给表增加主键约束。

3. 总结

  • ALTER TABLE = 改变表结构
  • 可以 增加/删除列、修改列类型、增加约束
  • 优点:不需要删除表或重新建表,就可以修改表结构

如果你需要,我可以给你画一张 表结构变化示意图,更直观地展示 ALTER TABLE 的作用。
你希望我画吗?
ON DELETE SET NULL 有什么作用?
在 SQL 里,ON DELETE SET NULL外键约束(FOREIGN KEY constraint) 的一个选项,用来定义 当父表(被引用表)中的记录被删除时,子表(引用表)对应的外键列的行为

1. 作用

  • 当父表的某条记录被删除时,子表中引用这条记录的外键字段会被自动设置为 NULL
  • 这样可以避免外键约束报错,同时保留子表的记录。

2. 示例

假设有两张表:
  • employees.dept_id 是外键,引用 departments.dept_id
  • 并设置了 ON DELETE SET NULL

情况:

  • 如果有员工 emp_id = 100dept_id = 1,那么执行删除后:
  • 员工记录仍然保留,但不再属于任何部门。

3. 使用场景

  • 适用于 希望保留子表记录,但删除父表时外键可为空 的情况。
  • 例如:
    • 用户删除了某个项目,但保留参与记录;
    • 删除部门,但保留员工信息,只是部门字段设为空。

💡 总结
ON DELETE SET NULL = 父表删除记录 → 子表外键自动设为 NULL,避免删除子表记录,同时保持数据完整性。

如果你想,我可以帮你画一张 父表删除导致子表字段变 NULL 的示意图,更直观地理解。
你希望我画吗?
SQL注释速查
好,我帮你整理一份 SQL 注释用法速查表,方便快速理解和使用。

SQL 注释速查表

注释类型
语法示例
用途
注意事项
单行注释
-- 这是单行注释
注释一行代码或文字说明
-- 后面内容到行尾都被忽略
块注释 / 多行注释
/* 这是多行注释 */
注释多行代码或说明
可以跨多行,适合长说明或临时屏蔽大段代码
分隔线
-- ----------------------
用于视觉分隔 SQL 脚本的模块或表
纯视觉效果,不影响执行
模块/标题说明
-- Corporate
标明接下来的 SQL 代码所属模块或功能
通常和分隔线配合使用,提高可读性
组合示例
```sql

  • - Corporate


如果你需要,我可以帮你画一个 SQL 文件结构示意图,用颜色和注释标出模块和分隔线,看起来一眼就懂整个文件结构。
你希望我画吗?

在 SQL 里,# 并不是标准注释符号,所以能否用取决于 数据库系统

1. 标准 SQL 注释

  • 单行注释:-
  • 多行注释:/* ... */
例如:

2. # 注释的兼容性

  • MySQL 支持:
  • 其他数据库(如 PostgreSQL、SQL Server、Oracle)不支持 #,会报错。

3. 建议

  • 为了 跨数据库兼容,尽量用:
    • -(单行)
    • /* ... */(多行)
  • 只有在 确认只用 MySQL 时,# 才安全。

💡 总结
注释方式
支持数据库
跨库兼容性
--
所有主流 SQL 数据库
✅ 高
/* ... */
所有主流 SQL 数据库
✅ 高
#
MySQL / MariaDB
⚠️ 低(其他数据库报错)

明白了,你想在 SQL 语句的同一行后面加注释,这种叫 行尾注释(inline comment)。在 SQL 里是完全可以的,方法如下:

1. 使用 - 行尾注释(最常用)

  • - 后面的内容都是注释,直到行尾。
  • 标准 SQL,几乎所有数据库都支持。

2. 使用 /* ... */ 块注释

  • 可以写在语句末尾,也可以跨多行:

3. 注意事项

  1. 不要在 ; 后换行再写 -
      • - 必须在行内,或者单独占一行。
  1. 块注释 /* ... */ 可以嵌套在语句中
      • 适合长说明或临时屏蔽部分代码。

💡 示例对比
写法
结果
SELECT * FROM Employees; -- 查询所有员工
查询语句执行,-- 后是注释
SELECT * FROM Employees; /* 查询所有员工 */
查询语句执行,/* */ 内是注释
SELECT * FROM Employees; # 查询所有员工
只在 MySQL 支持,其他数据库报错

如果你需要,我可以帮你画一张 行尾注释示意图,展示不同注释方式在 SQL 里的位置和作用,这样一眼就懂。
你希望我画吗?
Check out:

More Basic Queries

Functions

  • blocks of codes to do things
Find out how many males and females there are 的结果
aggregation
notion image
Find the total sales of each salesman 的结果
notion image
什么是aggregation
你提到的 SQL 语句被称为 aggregation(聚合),原因主要在于 它对数据进行汇总或统计。我给你详细分析一下:

1. 什么是聚合(Aggregation)?

在数据库里,聚合操作是指:
  • 将多行数据 合并为单个值或更少的行
  • 常用于统计、求和、平均、计数等。
常见的 聚合函数(Aggregate Functions):
函数
作用
COUNT()
计数
SUM()
求和
AVG()
平均值
MAX()
最大值
MIN()
最小值

2. 分析你的 SQL 语句

  • COUNT(sex)聚合函数,统计每个性别的数量
  • GROUP BY sex → 按 sex 分组,把相同性别的记录聚在一起
  • 最终结果是:每个性别对应一行,总数被汇总出来
🔹 例子:
emp_id
name
sex
1
Alice
F
2
Bob
M
3
Carol
F
执行 SQL 后:
COUNT(sex)
sex
2
F
1
M
这里 多行数据被“聚合”为每个性别的一行统计结果,所以叫 aggregation

3. 总结

  • 聚合 = 用函数把多条数据汇总成统计值
  • GROUP BY + 聚合函数 = 按某个字段分组统计
  • 你的例子就是统计男女员工数量 → 典型的 聚合查询

如果你愿意,我可以帮你画一张 原始表 → GROUP BY 聚合后的表 的示意图,看起来更直观。
你希望我画吗?

Wildcards

Wildcards: a way of defining different patterns that we want to match specific pieces of data to.
你这句话的意思可以这样理解:

1. 直译

Wildcards: 一种定义不同模式的方法,我们用它来匹配特定的数据。

2. 更自然的解释

  • Wildcard(通配符) = 用来代表 任意字符或一类字符的特殊符号
  • 它让你不用写完整的值,也能匹配满足某种模式的数据
例如:
通配符
用法
说明
*
a*
匹配以 a 开头的任意字符串,如 apple, abc
?
a?
匹配 a 加任意一个字符,如 ab, ax
%
a% (SQL)
匹配以 a 开头的任意长度字符串
_
a_ (SQL)
匹配 a 加一个任意字符

3. 例子

假设有一列名字:
name

Alice

Bob

Alex

Albert

  • 用通配符 Al* 匹配 → 会得到 Alice, Alex, Albert
  • 用通配符 A?ex 匹配 → 会得到 Alex

💡 总结
  • 通配符就是 一种模式匹配工具
  • 可以匹配 未知或多变的数据,不用每次都写完整内容。

 
是的,几乎所有编程语言和一些工具都有 通配符(wildcards)或类似的模式匹配机制,但形式和用法可能不同。
我帮你整理一下常见语言和工具的情况:

1. SQL

  • % → 匹配任意长度的字符
  • _ → 匹配单个字符

2. Shell / Bash / Linux

  • → 任意字符任意长度
  • ? → 任意单个字符
  • [abc] → 匹配 a、b 或 c

3. Python

  • Python 的标准库里没有“通配符”操作符,但有 glob 模块正则表达式(regex)

4. Java / C# / JavaScript

  • 没有直接通配符符号,但用 正则表达式(regex) 实现类似功能

5. Windows 文件搜索 / DOS

  • → 任意长度字符
  • ? → 任意单个字符

✅ 总结

  • 通配符概念广泛存在,本质都是“用简单符号代表任意字符或模式”。
  • 不同语言写法不同:SQL %/_,Shell ? [],编程语言多用正则表达式。

如果你想,我可以帮你画一张 各语言通配符对照表,一眼就能看到每种语言怎么写和匹配。
你希望我画吗?
占位符 vs 通配符 vs 正则符号
概念
用途
示例
占位符
在字符串或 SQL 里“占位置”,运行时替换成实际值
%s, {}, ?(SQL)
通配符
模糊匹配,匹配任意字符或模式
*, %, ?
正则符号
精确匹配规则或位置
., *, ^, $
🔹 简单说:
  • 占位符 = 填东西
  • 通配符/正则符号 = 匹配东西
LIKE keyword
在 SQL 里,LIKE用于进行模式匹配(pattern matching)的关键字。它可以让你查找满足一定规则的字符串,而不必完全匹配整个值。

1. 基本语法

  • column_name → 要匹配的列
  • 'pattern' → 模式(pattern),可以包含 通配符

2. 常用通配符(在 SQL 中)

通配符
含义
示例
%
任意长度的任意字符
'A%' → 以 A 开头的字符串
_
单个任意字符
'A_' → 两个字符,第一位是 A,第二位任意
[] (部分 DBMS 支持)
匹配方括号里的任意字符
'[AB]%' → 以 A 或 B 开头的字符串

3. 示例

假设有一个 employees 表:
name

Alice

Bob

Alex

Albert

查询名字以 A 开头的人:

结果:
name

Alice

Alex

Albert

查询名字第二个字母是 l 的人:

结果:
name

Alice

Alex

Albert


4. 总结

  • LIKE = SQL 的 模式匹配关键字
  • 搭配 %_ 可以灵活匹配字符串
  • 常用于 模糊查询,如查找某个前缀、后缀或包含某字符的记录

如果你愿意,我可以帮你画一张 LIKE + % / _ 匹配示意图,直观展示 SQL 是如何匹配不同字符串的。
你希望我画吗?
Find any employee born on the 10th day of the month 的结果
notion image
SQL Wildcards 速查表
好的,我帮你整理一份 SQL 通配符(Wildcards)用法速查表,方便快速理解和使用。

SQL Wildcards 速查表

通配符
含义
示例
说明
%
任意长度的任意字符(包括 0 个字符)
name LIKE 'A%'
匹配以 A 开头的所有字符串,如 Alice、Alex、Albert
_
单个任意字符 any single character
name LIKE 'A_e'
匹配三字符字符串,第一位 A,第三位 e,如 Ace、Ale
[ ]
匹配括号内的任意单个字符(部分数据库支持,如 SQL Server)
name LIKE '[AB]%'
匹配以 A 或 B 开头的字符串
[^ ]
匹配不在括号内的任意单个字符(部分数据库支持,如 SQL Server)
name LIKE '[^AB]%'
匹配不以 A 或 B 开头的字符串

使用示例

1. 匹配前缀

2. 匹配单个字符

3. 匹配指定字符集合(SQL Server)

4. 排除指定字符集合(SQL Server)


小贴士

  1. SQL 的通配符 只能在 LIKE 或 NOT LIKE 中使用
  1. % 可匹配任意长度(包括空字符串),_ 只匹配一个字符。
  1. 方括号 [ ][^ ] 在 MySQL 不支持,但 SQL Server 支持。
  1. 使用通配符进行模糊查询时,查询效率可能比精确匹配低

我可以帮你画一张 通配符匹配示意图,把 %_[ ] 的匹配效果直观展示出来,这样一眼就能看懂。
你希望我画吗?

Union

Union: a special operator which we can use to combine the result of multiple SELECT statements into one.
是的,这个解释 基本是正确的,我帮你详细补充一下,让理解更准确:

1. 基本解释

  • UNION 是 SQL 的一个 集合操作符(set operator)
  • 用来 把多个 SELECT 查询的结果合并成一个结果集
  • 合并后的结果 默认去重(相同的行只出现一次)

2. 语法

  • 条件:
      1. SELECT 的列数必须相同
      1. 列的顺序和数据类型最好一致

3. 示例

假设有两张表:
Table_A
name

Alice

Bob

Table_B
name

Bob

Carol

结果:
name

Alice

Bob

Carol

  • 注意:Bob 只出现一次,因为 UNION 默认去重

4. UNION ALL

  • 如果想保留重复行,可以用 UNION ALL
结果:
name

Alice

Bob

Bob

Carol


5. 总结

  • UNION = 合并多个 SELECT 的结果
  • 默认 去重,如果想保留重复用 UNION ALL
  • 列数和类型必须匹配

  • SELECT employee.first_name AS Employee_Branch_Names
    • AS是change最终显示的column名称为Employee_Branch_Names
以下这种写法会出错,因为一个有2个columns,一个只有一个column。所以Rule: column一致
notion image

Joins

  • 在 SQL 里,JOIN 用来把两个或多个表的数据根据某些条件组合在一起,常见于查询有关系的表。
    • In SQL, JOIN is used to combine rows from two or more tables based on a related column between them.
分类

1. INNER JOIN 内连接

  • 中文:只返回两个表中 匹配到的记录。如果某一行在任一表里没有匹配,就不会出现在结果里。
  • English: Returns only the rows where there is a match in both tables. Rows without a match are excluded.
示例 Example:
👉 输出:只有有部门的员工会显示。

2. LEFT JOIN (LEFT OUTER JOIN) 左连接

  • 中文:返回 左表所有的记录,即使右表没有匹配,也会返回,右表对应字段显示 NULL。
  • English: Returns all rows from the left table, and matched rows from the right table. If no match, NULLs appear for right table columns.
示例 Example:
👉 输出:所有员工都会显示,即使没有部门,也会显示,但部门列为 NULL。

3. RIGHT JOIN (RIGHT OUTER JOIN) 右连接

  • 中文:与 LEFT JOIN 相反,返回 右表所有的记录,即使左表没有匹配,也会显示,左表对应字段为 NULL。
  • English: Opposite of LEFT JOIN. Returns all rows from the right table, and matched rows from the left table. If no match, NULLs appear for left table columns.
示例 Example:
👉 输出:所有部门都会显示,即使某部门没有员工,也会出现,但员工列为 NULL。

4. FULL JOIN (FULL OUTER JOIN) 全连接

  • 中文:返回左右两表的 所有记录。匹配的会合并显示,不匹配的补 NULL。
  • English: Returns all rows from both tables. Matches are combined, non-matches are filled with NULLs.
示例 Example:
👉 输出:所有员工和所有部门都会显示,缺失匹配的一边显示 NULL。

5. CROSS JOIN 交叉连接

  • 中文:返回两个表的 笛卡尔积,即左表每一行和右表每一行都组合一次。
  • English: Returns the Cartesian product of two tables, meaning every row from the first table is paired with every row from the second table.
示例 Example:
👉 输出:如果有 10 个员工、5 个部门,就会得到 50 行结果。

✨ 小结 Summary
  • INNER JOIN: 只取交集 (only matching rows)
  • LEFT JOIN: 左表全保留 (all left + matches from right)
  • RIGHT JOIN: 右表全保留 (all right + matches from left)
  • FULL JOIN: 并集 (all rows from both tables)
  • CROSS JOIN: 笛卡尔积 (Cartesian product)

要不要我帮你画一张 Venn 图对照图(中英文标注),直观展示每种 JOIN 的区别?

INNER JOIN

  • INSERT INTO branch VALUES(4, "Buffalo", NULL, NULL);
    • 后面两个NULL的解释:not gonna have a manager ID, and a manager start date
      • notion image
INSERT INTO branch VALUES(4, "Buffalo", NULL, NULL); Result
notion image
employee table和branch table都有 employee’s IDs【JOINS可以把两个table通过 related column(这里就是employee’s IDs)关联】
notion image
解释代码
FROM employee
JOIN branch
join the Employee table and the Branch table together (into one table) on a specific column.

整体代码解释
I wanna combine all of the rows from the Employe table and all of the rows from the Branch table, as long as the manager ID of the branch row is equal to the employee row.

LEFT JOIN

举例:
Result
notion image
💡
Why?
With the LEFT JOIN, we include all of the rows from the left table (employee table), but only the rows in the Branch table that matched are going to get included because the Branch table is the right table.
  • left table: the one in the FROM statement
    • FROM employee

RIGHT JOIN

举例:
  • Include all of the rows from the Branch table no matter what.
Result
notion image

FULL OUTER JOIN

  • a LEFT JOIN and a RIGHT JOIN combined.
  • 返回左右两表的 所有记录。匹配的会合并显示,不匹配的补 NULL。
    • Returns all rows from both tables. Matches are combined, non-matches are filled with NULLs.

Nested Queries

  • Nested query is basically a query where we’re going to be using multiple SELECT statements in order to get a specific piece of information.
notion image
Step 1:
这一步还没用到nested query
  • prefix with the table name just in case we have repeated column names
    • works_with.
Result
notion image
Step 2:
  • WHERE employee.emp_id IN ( )
    • 这里 use a nested query
把下面的部分放到 IN values ()里面【nest queries inside there】【注意indented】
SELECT works_with.emp_id FROM words_with WHERE works_with.total_sales > 30000;
变为:
  • If the employee ID got returned from this query
    • SELECT works_with.emp_id FROM words_with WHERE works_with.total_sales > 30000;
      which gave us the ID’s of all of the employees who have sold over 30,000,
      then we’re going to return their first name and last name.
Result
notion image
举例:
notion image
  • 首先execute ()内部的
    • SELECT branch.branch_id FROM branch WHERE branch.mgr_id = 102;
  • 再执行外部的
    • SELECT client.client_name FROM client WHERE client.branch_id = ();
[ChatGPT] 请解释 LIMIT 1
你给的 SQL 语句里有一个 LIMIT 1,我来详细解释一下。

原语句分析
  1. 外层查询:
  • 这是找出所有 client 表里,属于某个分支机构 (branch_id) 的客户。
  • 哪个分支机构呢?由子查询决定。
  1. 子查询:
  • 这里是从 branch 表中找出经理 ID 为 102 的分支机构。
  • 问题:理论上,可能有多个分支机构由 Michael Scott 管理。
  • LIMIT 1 的作用就是 只取第一条记录,即使有多个分支机构也只选一个 branch_id 返回。

总结 LIMIT 1
  • LIMIT 用于 限制返回的记录条数
  • LIMIT 1 → 只返回 一条记录
  • 在这里,它保证子查询只返回一个 branch_id,避免出现 “子查询返回多行” 的错误。
⚠️ 注意:SQL 标准中,子查询用于 = 时 必须返回单值(单行单列)。如果子查询可能返回多行,就必须用 LIMIT 1 或其他方式(例如 IN)。

如果你愿意,我可以帮你改写这个查询,使它 可以处理 Michael Scott 管理的多个分支,而不会受 LIMIT 1 限制。
你想让我改吗?
Result
notion image

ON DELETE

  • Deleting entries in the database when they have foreign keys associated to them.
  • Useful when we’re defining foreign key relationships between tables.
notion image
notion image
ChatGPT解释 ON DELETE SET NULLON DELETE CASCADE
好的,我来仔细解释一下这两个 外键约束的删除行为。它们都是在 SQL 中定义 外键关系 时,用来控制当父表(被引用表)的数据被删除时,子表(引用表)应如何处理。

1️⃣ ON DELETE SET NULL
作用:当父表中的记录被删除时,子表中对应的外键字段会被 设置为 NULL
例子
  • 如果某个部门(如 dept_id = 10)被删除,所有员工表中 dept_id = 10 的字段会被自动设为 NULL
  • 适用场景:子表的记录 仍然有效,只是它不再隶属于被删除的父表记录。

2️⃣ ON DELETE CASCADE
作用:当父表中的记录被删除时,子表中对应的记录也会被 自动删除
例子
  • 如果某个部门(如 dept_id = 10)被删除,所有员工表中 dept_id = 10 的记录也会 自动删除
  • 适用场景:子表记录 完全依赖于父表,没有父表就没有意义。

对比总结
行为
删除父表记录后子表效果
适用场景
ON DELETE SET NULL
外键字段设为 NULL
子表记录仍有价值,只是脱离父表
ON DELETE CASCADE
子表记录被删除
子表记录完全依赖父表,不存在独立意义

简单记忆:
  • SET NULL → 删除父表,子表断开联系(字段置空)
  • CASCADE → 删除父表,子表跟着一起消失(连锁删除)

ON DELETE SET NULL

  • If we delete one of these employees, that means that the manager ID that was associated to that employee is going to get set to NULL.
  • If the employee ID in the Employee table gets deleted, I want to set the manager ID equal to NULL.

ON DELETE CASCADE

  • If we delete the employee whose ID is stored in the manager ID column, then we’re just going to delete this entire row in the database.
ChatGPT 解释代码
好的,我们来逐行分析你提供的这段 SQL 代码:


1️⃣ CREATE TABLE branch_supplier(...)
  • 创建一个新表 branch_supplier,用来存储分支机构与供应商的关系。

2️⃣ 字段定义
  • branch_id:分支机构的 ID,对应 branch 表的主键。
  • 类型是 INT(整数)。
  • 供应商名称,最多 40 个字符。
  • 供应类型,例如“食品”、“文具”等,最多 40 个字符。

3️⃣ PRIMARY KEY(branch_id, supplier_name)
  • 联合主键branch_idsupplier_name 一起组成主键。
  • 意思:同一个分支机构不能重复关联同一个供应商,但不同分支机构可以关联相同供应商。

4️⃣ FOREIGN KEY(branch_id) REFERENCES branch(branch_id) ON DELETE CASCADE
  • branch_id外键,引用 branch 表的 branch_id
  • 约束作用
      1. branch_id 必须存在于 branch 表中(保持数据一致性)。
      1. ON DELETE CASCADE:如果 branch 表中某个 branch_id 被删除,branch_supplier 表中所有对应的记录也会自动被删除。
          • 举例:删除分支机构 101 → 这个分支对应的所有供应商记录也会自动删除。

✅ 总结
这个表用来表示 分支机构与供应商的对应关系,特点是:
  1. 一个分支可以有多个供应商;一个供应商可以对应多个分支(多对多关系)。
  1. 同一个分支不能重复添加同一个供应商(联合主键保证)。
  1. 删除父表 branch 中的分支时,对应的供应商关系会 自动级联删除,避免出现“孤立的记录”。

如果你想,我可以画一张小图,把 branch 表branch_supplier 表 的关系以及 ON DELETE CASCADE 的效果直观展示出来,这样更容易理解。
你希望我画吗?
  • If the branch ID that’s stored as the foreign key in the Branch Supplier table gets deleted, then we’re just going to delete the entire row in the database.
 
notion image
  • All of those branch supplier rows are going to get deleted that had 2 as their foreign key.
Result
notion image

Triggering

  • Trigger: a block of SQL code which we can write which will define a certain action that should happen when a certain operation gets performed on the database.
    • E.g. an entry was added into a particular table in the database.
  • A great way to control what happens when ofther stuff happens, you can automate a lot of the stuff on the backend of your database.
  • DROP TRIGGER my_trigger;
    • my_trigger is no longer going to be active.
  • Change the SQL delimiter that we’re going to use.
  • Do in terminal.
    • Windows: open up the MySQL Command Line Client
      • notion image
    • OSX
      • 什么是OSX
        OSX 指的是 Apple 的操作系统 macOS(以前叫 Mac OS X)。它是苹果电脑(Mac)上使用的操作系统。

        1️⃣ 名称来源

        • 原名:Mac OS X
          • “X” 是罗马数字 10,表示它是 Mac OS 的第十代。
        • 现在叫法:macOS
          • Apple 从 2016 年开始统一命名风格,改成 macOS(比如 macOS Ventura、macOS Sonoma)。

        2️⃣ 特点

        • 基于 UNIX:安全性高,稳定性好。
        • 图形界面友好:操作直观、美观。
        • 与 Apple 生态兼容:和 iPhone、iPad 的应用、服务协同良好。
        • 自带软件丰富:如 Safari、Mail、Finder、Time Machine 等。

        3️⃣ 常见用途

        • 日常办公(文档、邮件、网页浏览)
        • 软件开发(支持 Xcode、Terminal)
        • 图形设计、视频剪辑、音频制作
        • 科学计算和数据分析(通过 Terminal 或 IDE)

        简单记忆:
        OSX = Mac 电脑操作系统 = macOS = Mac OS X(第十代 Mac 系统)

        如果你想,我可以帮你列一个 Windows vs macOS vs Linux 的区别表,方便对比。
        你希望我列吗?
        type in mysql -u root - p ,hit Enter.
        prompt you for your password and you should be logged in.
举例:
  • We can create a trigger by saying CREATE and then I can say TRIGGER.
  • TRIGGER my_trigger BEFORE INSERT
    • define trigger, give it a name “my_trigger”
    • before something gets inserted on the Employee table, for each of the new items that are getting inserted I want to insert into the trigger test table the values ‘added new employee’.
      • 或者说: Before anything gets inserted on the Employee table now, I’m going to perform INSERT INTO trigger_test VALUES('added new employee');
  • DELIMITER : a special keyword in MySQL
    • It’ll change the MySQL delimiter. 分隔符
      • Normally, the MySQL delimiter is a semicolon(分号)
        • notion image
      • inside of these FOR EACH ROW BEGIN , I have to use this semicolon over here.
        • to end off the SQL command INSERT INTO trigger_test VALUES('added new employee');
          So, I can’t actually use the same delimiter in order to end off the trigger creation. 不能使用相同的分隔符来结束触发器的创建。
          But if I don’t change the delimiter, then this semicolon is basically going to tell SQL that we’re done creating the trigger, even though we’re clearly not. 但如果我不改变分隔符,我们已经完成了触发器的创建,尽管显然还没有。
      • So I’m changing the delimiter to two dollar signs to delineate that the trigger is done being created. 所以,我这里将分隔符改为了两个美元符号,表明触发器已创建完成。
        • And then I can ust delinear back to a semicolon. 然后我就可以直接将其还原为分号。
  • The reason that I have to do this over here in the terminal is because in PopSQL you can’t actually configure the delimiter(无法配置分隔符). So the delimiter is actually something that’s defined not on the text editor level.
Hopefully now this trigger is all set up inside of MySQL, so, one thing we can do to test it is just to add in another employee.
  • Add in Oscar, Martinez
    • VALUES(109, 'Oscar', 'Martinez', '1968-02-19', 'M', 69000, 106, 3);
  • Select all from trigger_test table.
Result
notion image
举例:
  • INSERT INTO trigger_test VALUES(NEW.first_name);
    • allow me to access a particular attribute about the thing that we just inserted. 允许我访问我们刚刚插入的东西的特定属性
    • 因此,NEW.first_name 将为我提供插入的员工的名字。
  • Insert Kevin Malone
Result
notion image
举例:
  • 第三行的INSERT 可以换成 DELETE, UPDATE
  • 第三行的BEFORE可以换成AFTER

ER Diagrams Intro

When you design a database, one of the most important things is designing a database schema.
  • Database schema: all the different tables and the different attributes /ˈætrɪbjuːt/ that are going to be on those tables.
    • Maybe you have some requirements for the different data that needs to get stored inside of your database and the different relationships that data is going to have.
      You can use an ER diagram to act as a middleman between database or storage requirements and the actual database schema.
  • ER = Entity Relationship
    • Entity: an object we want to model & store information about
  • ER diagram: a little diagram that consists of different shapes and symbols and text. It all kind of gets combined together to end up defining a relationship model.
ChatGPT解释 Database schema和 ER diagram
好的,我们来详细解释 SQL 中的 database schemaER diagram,并说明它们的关系和作用。

1️⃣ Database Schema(数据库模式)

定义

  • 数据库模式指的是数据库的结构和组织方式,也就是数据库里包含的表(tables)、字段(columns)、数据类型、约束条件(primary key、foreign key 等)以及表之间的关系。
  • 简单理解:数据库的蓝图

作用

  • 描述数据如何存储、组织和关联。
  • 保证数据一致性和完整性。
  • 方便开发者理解数据库结构。

举例

  • 上面三个表和它们的字段、主键、外键就是一个简单的 database schema

2️⃣ ER Diagram(实体-关系图)

定义

  • ER 图(Entity-Relationship Diagram)是一种可视化工具,用图形表示数据库模式。
  • 它显示:
    • 实体(Entity):通常对应表,如 studentcourse
    • 属性(Attribute):实体的字段,如 nameage
    • 关系(Relationship):实体之间的关联,如 enrollment 表表示学生选课的关系

作用

  • 可视化数据库结构,便于理解和设计。
  • 有助于发现潜在问题,如重复数据、缺失关系。
  • 在数据库设计阶段非常重要,先画 ER 图,再根据图创建数据库 schema。

常见符号

符号
含义
矩形
实体(Entity)
椭圆
属性(Attribute)
菱形
关系(Relationship)
线
实体与关系之间的联系
1:N, N:M
表示关系的基数(Cardinality)

3️⃣ 区别与联系

特性
Database Schema
ER Diagram
表现形式
文本/代码(SQL)
图形/可视化
内容
表、字段、数据类型、约束
实体、属性、关系
作用
实际建库、操作数据库
设计阶段可视化,便于理解和交流
创建顺序
通常根据 ER 图创建
设计数据库前先画

💡 总结
  • ER 图是蓝图的可视化展示,帮助设计和理解。
  • Database schema是蓝图的实际 SQL 实现,用于创建和维护数据库。

如果你需要,我可以帮你画一个 简单的学生选课 ER 图,并标注出对应的 SQL schema,让两者一眼对应,理解起来更直观。
你希望我画吗?
Attributes - Specific pieces of information about an entity
Student entity, we might want to store the student’s name, grade number, like what grade are they in, and their GPA.
Student entity, we might want to store the student’s name, grade number, like what grade are they in, and their GPA.
Primary Key - An attribute(s) that uniquely identify an entry in the database table
  • We can also define a primary key (underline).
Primary key: student_id
Primary key: student_id
Composite Attribute - An attribute that can be broken up into sub-attributes
name split into fname (first name) and lname (last name)
name split into fname (first name) and lname (last name)
Multi-valued Attribute - An attribute that can have more than one value
notion image
Derived Attribute - An attribute that can be derived from the other attributes
  • A way that we can sort of notate attributes(标记属性的一种方式)that could be derived from the attributes that we’re storing.
Derived Attribute: has_honors (derive from gpa). We’re not actually going to be keeping track of this attribute, but it’s an attribute that we can derive from the GPA that we are keeping track of.
Derived Attribute: has_honors (derive from gpa). We’re not actually going to be keeping track of this attribute, but it’s an attribute that we can derive from the GPA that we are keeping track of.
Multiple Entities - You can define more than one entity in the diagram
notion image
Relationships - defines a relationship between two entities
Total Participation - All members must participate in the relationship
notion image
  • Single line: this indicates partial participation
    • Not all students need to take a class.
  • Double line: indicates total participation
Relationship Cardinality - the number of instances of an entity from a relation that can be associated with the relation
notion image
Students can take any number of classes.
  • M: refers to any number
    • A student could take basically multiple classes.
上面就是NM cardinality relationship
1 to 1 cardinality relationship: a student can take one class and a class can be taken by one student.
1 to N cardinality relationship: a student could take one class and a class could be taken by many students. Or reverse it. A class can be taben by many students but a student can take any number of classes.
notion image
Weak Entity - An entity that cannot be uniquely identified by its attributes alone
Identifying relationship - A relationship that serves to uniquely identify the weak entity
脑袋挡住的是Class;exam can’t identify a class, so an exam is a weak entity; a class has an exam
脑袋挡住的是Class;exam can’t identify a class, so an exam is a weak entity; a class has an exam

Designing an ER Diagram

notion image
notion image
notion image
  • Take this document and convert it into an ER diagram
  1. The company is organized into branches. Each branch has a unique number, and a name
    1. primary key: branch name
    2. notion image
  1. The company makes it’s money by selling to clients. Each client has a name and a unique number to identify it.
    1. notion image
  1. The foundation of the company is it’s employees. Each employee has a name, birthday, sex, salary and a unique number to identify it.
    1. notion image
  1. An employee can work for one branch at a time
    1. notion image
  1. Each branch will be managed by one of the employees that work there. We’ll also want to keep track of when the current manager started as manager.
      • single participation
      notion image
  1. An employee can act as a supervisor for other employees at the branch, an employee may also act as the supervisor for employees at other branches. An employee can have at most one supervisor.
    1. notion image
  1. A branch may handle a number of clients, however a single client may only be handled by one branch at a time.
      • new relationship: handle
      notion image
  1. Employees can work with clients controlled by their branch to sell them stuff. If necessary multiple employees can work with the same client.
    1. notion image
  1. Many branches will need to work with suppliers to buy inventory. For each supplier we’ll keep track of their name and the type of product they’re selling the branch. A single supplier may supply products to multiple branches.
    1. notion image
Entire ER Diagram:
notion image

Converting ER Diagrams to Schemas

Step 1: Mapping of Regular Entity Types

notion image
 
notion image
  • Employee
    • primary key: emp_id

Step 2: Mapping of Weak Entity Types

notion image
 
notion image

Step 3: Mapping of Binary 1:1 Relationship Types

 
notion image
 
notion image
  • Branch
    • Foreign key: mgr_id

Step 4: Mapping of Binary 1:N Relationship Types

notion image
 
notion image

Step 5: Mapping of Binary M:N Relationship Types

notion image
notion image
 
notion image
 
Actual database:
notion image
 
 
 
上一篇
Packaging 包装
下一篇
图解词汇 Meat

Comments
Loading...
Catalog