Mysql 建库建表技巧分享
本文中说到的“建”,并非单纯的建一个库,或是建一张表,而是你建好的库和表在项目的运营中,是否能应付各种事件,下面我说说几个我在项目中遇到的问题以及处理的方法,算是一个小小的心得,给大家分享下。
一、两表之间若有关联,你是否还在用主键进行关联?比如现在有2张表,一张新闻栏目表,一张新闻表,现在两张表需要进行关联,我想大多数人的做法肯定是在新闻表里建一个新闻栏目id,然后把新闻栏目表里的主键ID(自增)写到这个字段里,通过这样进行两表关联。
如果你是这样做的,赶紧改掉这个习惯吧。也许你会问为什么,栏目id是主键啊,又是自增的,为什么这样操作不行?原因其实很简单,栏目我们会增加,也会删除,删除就会造成主键id之间会有断号的情况,由于主键设置为自增,也就是说你之前删掉的栏目,再进行添加,id是不会去补上哪个空缺的,而是一直递增。这样就会造成一种情况,如果那天对数据库进行优化,把主键进行了重新排序(暂时没有找到mysql优化软件会优化主键,但是可以通过代码删除主键,然后从新建立自增主键来实现主键重新排序),那就彻底杯具了,栏目和文章完全对不上号了。所以我建议两表之间关联不用主键,而是单独建一个编号的字段,我们这里可以用mysql的uuid()函数做为编号,相关文献可以参考《UUID做主键好还是不好》,只所以一张表要2个主键,一个物理主键(自增id),一个逻辑主键(UUID),原因是:对于InnoDB这种聚集主键类型的引擎来说,数据会按照主键进行排序,由于UUID的无序性,InnoDB会产生巨大的IO压力,此时不适合使用UUID做物理主键,可以把它作为逻辑主键,物理主键依然使用自增ID。至于性能,我本地测了下基本上没差异,网上也有人做了10W条数据的测试——《实测MYSQL UUID性能》。
二、统一把主键类型设为bigint吧
bigint是从-2^63 (-9223372036854775808)到2^63-1 (9223372036854775807)的所有整型数据,存储大小为8个字节。而int是从-2^31 (-2,147,483,648)到2^31-1 (2,147,483,647)的整型数据,存储大小为4个字节。存储空间扩大一倍,而存储数据却扩大N倍,再加上主键是一个自增的字段,我们根本无法控制它会自增到多少数值,所以我通常在建表的时候,主键类型都是设为bigint的,同样,上面提到的编号字段类型也是bigint。
三、不要把varchar长度设太“死”
这也是我之前经常犯得一个毛病,比如手机,我就设置为varchar(11),邮编设置成varchar(6),姓名设置成varchar(10)等等等等,看似每个字段都设置得很严谨,但是在项目实际进行中,这完全就是自找苦吃,比如手机,用户偏偏就要在手机号前输个0,又比如邮编,如果用户输入的是全角的数字呢?姓名就更不用说了,万一是个少数民族的人,名字七八个字。所以我建议,既然定义为varchar,就代表不会涉及到计算,何不干脆定义一个通用的长度,比如varchar(50),如果真要限制长度,用程序去判断,不要让数据库来限制,不然用户输了一长串,结果mysql就存了前几个字符,让人觉得这程序有问题。
还有就是,如果你是做cms这种通用后台,更别把字段限制得太“死”,因为你无法预料之后的每个项目的需求,所以还是把varchar设大一点,我现在是统一都设为255,如果很有可能会超过255的字段,比如URL,我就干脆设置成text,一劳永逸。
四、为常用的搜索字段建立索引吧
不解释,但不要盲目建立索引。
五、欢迎您的回复补充

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

AI Hentai Generator
Generate AI Hentai for free.

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



MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

Recovering deleted rows directly from the database is usually impossible unless there is a backup or transaction rollback mechanism. Key point: Transaction rollback: Execute ROLLBACK before the transaction is committed to recover data. Backup: Regular backup of the database can be used to quickly restore data. Database snapshot: You can create a read-only copy of the database and restore the data after the data is deleted accidentally. Use DELETE statement with caution: Check the conditions carefully to avoid accidentally deleting data. Use the WHERE clause: explicitly specify the data to be deleted. Use the test environment: Test before performing a DELETE operation.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.
