笔试面试那件小事(数据库-范式)
1相关概念和知识 数据依赖:反映一个关系内部属性与属性之间的约束关系,是现实世界属性相互联系的抽象,属于数据内在的性质和语义的体现 规范化理论:是用来设计良好的关系模式的基
1>相关概念和知识
数据依赖:反映一个关系内部属性与属性之间的约束关系,是现实世界属性相互联系的抽象,属于数据内在的性质和语义的体现
规范化理论:是用来设计良好的关系模式的基础。它通过分解关系模式来消除其中不合适的数据依赖,以解决插入异常、删除异常、更新异常和数据冗余问题
函数依赖:简单的说,对于关系模式的两个属性子集X和Y,若X的任一取值都能唯一确定Y的值,那么则称Y函数依赖于X,记为X->Y
非平凡函数依赖:对于关系模式的两个属性子集X和Y,如果X->Y,但是Y不是X的子集,那么就称X->Y为非平凡函数依赖
完全函数依赖:对于关系模式的两个属性子集X和Y,如果X->Y,并对于X的任何一个真子集X',都有X‘->Y,称为完全函数依赖
范式:指符合某一种级别的关系的集合。
1NF:若关系模式的所有属性都是不可分的基本数据项,则该关系属于1NF
2NF:1NF关系模式如果同时满足每一个非主属性完全依赖于码,则该关系模式为2NF
3NF:若关系模式的每一个非主属性,既不部分依赖于码,也不传递依赖于码,则该关系模式为3NF
BCNF:若一个关系模式的每个决定因素都包含码,则该关系模式为BCNF
数据库设计:是指对于一个给定的应该环境,构造优化的数据逻辑模式和物理结构,并据此建立数据库及其应用系统,使之能够有效的存储和管理数据,满足各种用户的应用需求,包括信息管理需求和数据库操作需求。
数据库设计的6个基本步骤:需求分析、概念结构设计、逻辑结构设计、物理结构设计、数据库实现、数据库运行和维护
概念结构设计:是指需求分析得到的用户需求抽象为信息结构。代表E-R图构建
逻辑结构设计:将概念结构模型(E-R图),转化为某个DBMS系统支持的数据模式。代码E-R转为关系数据模式
物理结构设计:为一个给定的逻辑数据模型选取一个合适应用环境的物理结构过程。包括设计数据的存储结构与存钱方法
抽象:指对实际的人、物、事和概念进行人为处理,抽取所关心的共同特征,忽略非本质的细节。
数据库设计必须遵循相结合的原则
数据字典主要包括数据项、数据结构、数据流、数据存储和处理过程5个部分
三种常见抽象方法:分类、聚集和概括
局部E-R图之间的冲突主要表现在属性冲突、命名冲突和结构冲突三个方面
数据库常用的存取方法:索引方法、聚簇方法和HASH方法
确定数据存放位置和存储结构需要考虑的因素主要有:存取时间、空间利用率和维护代价
&&&&&&&&&&&&数据库的范式&&&&&&&&&&&&&
1>第一范式(1NF)无重复的列
第一范式(1NF)中数据库表的每一列都是不可分割的基本数据项
同一列中不能有多值
即实体中的某一属性不能有多个值或者不能有重复的属性。不满足1NF的数据库不是关系数据库
2>第二范式(2NF)属性完全依赖于主键(消除部分函数依赖)
满足第二范式必须先满足第1范式
第二范式要求数据库表中的每一个实例或者元组必须可以被唯一的区分
3>第三范式(3NF)消除传递依赖
例子:在设计数据库表结构之前,我们先确定一下要设计的内容包含哪些。学号、学生姓名、年龄、课程、课程学分、系别、学科成绩、系办公地址、系办电话等信息。为了更好的理解信息,将上述信息分为以下几个方面:
学生信息包括哪些
学生选了哪些课,成绩是什么
每个课程的学分是多少
学生属于哪个系,系的基本信息是什么
首先第一范式(1NF):数据库表中的字段都是单一属性,不可再分。这个单一属性由基本类型构成,包括整数、字符、逻辑型、日期型等。目前在任何的关系数据库中不允许将一列分成多列,因此做出的数据库都符合第一范式的数据库
考虑第2范式,先把所有的信息放到一个表中(学号,学生姓名,年龄,性别,课程,课程学分,系别,学科成绩,系办地址,系办电话)并且存在下列的依赖关系。
1)(学号)->(姓名,年龄,性别,系别,系办地址,系办电话)
2)(课程名称)->(学分)
3)(学号,课程)->(学科成绩)
根据依赖关系我们生成三个表:
学生:student(学号,姓名,年龄,性别,系别,系办地址,系办电话)
课程:Course(课程名称,学分)
选课关系:selectCourse(学号,课程名称,成绩)
对照第二范式的要求,上述表已经满足第二范式。如果一个数据库不满足第2范式,则会产生:
数据冗余,更新异常,删除异常等;例如同一课程由n个学生选修,那么学分就重复了n-1次,同一个学生选修了m个课程,那么他的名字就重复多次
若调整了某一门课程的学分,则所有的学分都得更新,否则会出现一门课程多种学分的情况。假设一批学生已经选修完了课程,要将这些记录从数据库中删除,那么会连同课程的信息被一道删除。
接下来再考虑将数据库改成满足第3范式的数据库表:
看上面的学生表Student(学号,姓名,年龄,性别,系别,系办公地址,系办电话),单一码为学号。并且系办地址和系办电话依赖于系别。存在传递函数依赖。
将学生表拆成两个表:
学生(学号,姓名,年龄,性别,系别)
系别(系别,系办地址,系办电话)
上述关系满足三范式。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Last week, amid the internal wave of resignations and external criticism, OpenAI was plagued by internal and external troubles: - The infringement of the widow sister sparked global heated discussions - Employees signing "overlord clauses" were exposed one after another - Netizens listed Ultraman's "seven deadly sins" Rumors refuting: According to leaked information and documents obtained by Vox, OpenAI’s senior leadership, including Altman, was well aware of these equity recovery provisions and signed off on them. In addition, there is a serious and urgent issue facing OpenAI - AI safety. The recent departures of five security-related employees, including two of its most prominent employees, and the dissolution of the "Super Alignment" team have once again put OpenAI's security issues in the spotlight. Fortune magazine reported that OpenA

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

70B model, 1000 tokens can be generated in seconds, which translates into nearly 4000 characters! The researchers fine-tuned Llama3 and introduced an acceleration algorithm. Compared with the native version, the speed is 13 times faster! Not only is it fast, its performance on code rewriting tasks even surpasses GPT-4o. This achievement comes from anysphere, the team behind the popular AI programming artifact Cursor, and OpenAI also participated in the investment. You must know that on Groq, a well-known fast inference acceleration framework, the inference speed of 70BLlama3 is only more than 300 tokens per second. With the speed of Cursor, it can be said that it achieves near-instant complete code file editing. Some people call it a good guy, if you put Curs

The Go framework is a set of components that extend Go's built-in libraries, providing pre-built functionality (such as web development and database operations). Popular Go frameworks include Gin (web development), GORM (database operations), and RESTful (API management). Middleware is an interceptor pattern in the HTTP request processing chain and is used to add functionality such as authentication or request logging without modifying the handler. Session management maintains session status by storing user data. You can use gorilla/sessions to manage sessions.

According to news on June 26, at the opening ceremony of the 2024 World Mobile Communications Conference Shanghai (MWC Shanghai), China Mobile Chairman Yang Jie delivered a speech. He said that currently, human society is entering the fourth industrial revolution, which is dominated by information and deeply integrated with information and energy, that is, the "digital intelligence revolution", and the formation of new productive forces is accelerating. Yang Jie believes that from the "mechanization revolution" driven by steam engines, to the "electrification revolution" driven by electricity, internal combustion engines, etc., to the "information revolution" driven by computers and the Internet, each round of industrial revolution is based on "information and "Energy" is the main line, bringing productivity development

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.
