Home Database Mysql Tutorial 设立MySQL数据表主键

设立MySQL数据表主键

Jun 07, 2016 pm 04:14 PM
mysql host primary key data sheet set up

设置MySQL数据表主键 ? 设置MySQL数据表主键: ? ? 使用“primary key”关键字创建主键数据列。被设置为主键列不允许出现重复的值,很多情况下与“auto_increment”递增数字相结合。如下SQL语句所示: pre Mysql create ? table ?books(bookid? int (11)? NOT

设置MySQL数据表主键

?

设置MySQL数据表主键:

? ? 使用“primary key”关键字创建主键数据列。被设置为主键列不允许出现重复的值,很多情况下与“auto_increment”递增数字相结合。如下SQL语句所示:

Copy after login
Copy after login
Copy after login
Copy after login

  1. Mysql>create?table?books(bookid?int(11)?NOT?NULL?AUTO_INCREMENT?PRIMARY?KEY,bookname?varchar(50));?
  2. Mysql>insert?into?books(bookname)?values(“book1”),(“book2”),(“book3”);?
  3. Mysql>select?*?from?books;?


??? 若要修改列主键或类型,请参考 ALTER TABLE 语句。
设置MySQL数据表外键

??? 外键是设置当前表中的某一列与别一数据表中的主键列关联。主要目的是控制与外键表中的数据,保持数据一致性,完整性,也就是说:当前表中这一列的数据必须是关联外键列中的某一数据,而且相关联的两个数据列的类型必须相同;当关联外键列某一数据修改或删除时,将触当前表的某一项相应操作。可解发以下事件以及参数:
触发事件:on delete和on update
可设参数:cascade(跟随外键改动);
restrict(限制外表中的外键改动);
set Null(设空值);
set Default(设默认值);
no action [默认]

??? 设置关联的语句由[指定主键关键字:foreign key(列名)]和[引用外键关键字: references (外键列名)]组成。例如创建一个关于books的购物车数据表“gbooks”,其中“gbookid”与books表中的“bookid”创建外键关联。

Copy after login
Copy after login
Copy after login
Copy after login

  1. Mysql>create?table?gbooks(gid?int(11)?not?null?auto_increment?primary?key,gbookid?int(11),goodsum?int(4),foreign?key(gbookid)?references?books(bookid)?on?delete?cascade?on?update?cascade);?


??? 删除外键:

??? 首先,使用SHOW CREATE TABLE语句查看创建表描述。其中“CONSTRAINT”关键字后面有一个引号括起来的名称,它就是这个表外键的代表,是在创建外键时自动生成的名称,当然在创建的过程中可以直接用“CONSTRAINT”关键字自定义名称。其查看的完整语句如下:

Copy after login
Copy after login
Copy after login
Copy after login

  1. Mysql>show?create?table?gbooks;?

??? 这里代表外键的名称是“gbooks_ibfk_1”,目标找到了,再使用ALTER语句进行删除。

Copy after login
Copy after login
Copy after login
Copy after login

  1. Mysql>ALTER?TABLE?gbooks?DROP?FOREIGN?KEY?gbooks_ibfk_1;?


主键和外键的设置意义:

??? 主键和外键是把多个表组织为一个有效的关系数据库的粘合剂。主键和外键的设计对物理数据库的性能和可用性都有着决定性的影响。

??? 必须将数据库模式从理论上的逻辑设计转换为实际的物理设计。而主键和外键的结构是这个设计过程的症结所在。一旦将所设计的数据库用于了生产环境,就很难对这些键进行修改,所以在开发阶段就设计好主键和外键就是非常必要和值得的。

主键:

关系数据库依赖于主键—它是数据库物理模式的基石。主键在物理层面上只有两个用途:
1. 惟一地标识一行。
2. 作为一个可以被外键有效引用的对象。

基于以上这两个用途,下面给出了我在设计物理层面的主键时所遵循的一些原则:

1. 主键应当是对用户没有意义的。如果用户看到了一个表示多对多关系的连接表中的数据,并抱怨它没有什么用处,那就证明它的主键设计地很好。

2. 主键应该是单列的,以便提高连接和筛选操作的效率。

3. 永远也不要更新主键。实际上,因为主键除了惟一地标识一行之外,再没有其他的用途了,所以也就没有理由去对它更新。
注:这项原则对于那些经常需要在数据转换或多数据库合并时进行数据整理的数据并不适用。

4. 主键不应包含动态变化的数据,如时间戳、创建时间列、修改时间列等。

5. 主键应当有计算机自动生成。如果由人来对主键的创建进行干预,就会使它带有除了惟一标识一行以外的意义。一旦越过这个界限,就可能产生认为修改主键的动机,这样,这种系统用来链接记录行、管理记录行的关键手段就会落入不了解数据库设计的人的手中。

外键是用来和其他表建立联系用的:这个表中的一列和另外一个表中的一列相同,为了让这两个表联系起来,就把其中一个表中的列设成外键,把另外一个表列设成主键,就实现了这两个表的关联。一个表可以有多个外键。但主键只能有一个。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: The Ease of Data Management for Beginners MySQL: The Ease of Data Management for Beginners Apr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

Can I retrieve the database password in Navicat? Can I retrieve the database password in Navicat? Apr 08, 2025 pm 09:51 PM

Navicat itself does not store the database password, and can only retrieve the encrypted password. Solution: 1. Check the password manager; 2. Check Navicat's "Remember Password" function; 3. Reset the database password; 4. Contact the database administrator.

How to create navicat premium How to create navicat premium Apr 09, 2025 am 07:09 AM

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.

How to copy tables in mysql How to copy tables in mysql Apr 08, 2025 pm 07:24 PM

Copying a table in MySQL requires creating new tables, inserting data, setting foreign keys, copying indexes, triggers, stored procedures, and functions. The specific steps include: creating a new table with the same structure. Insert data from the original table into a new table. Set the same foreign key constraint (if the original table has one). Create the same index. Create the same trigger (if the original table has one). Create the same stored procedure or function (if the original table is used).

How to view database password in Navicat for MariaDB? How to view database password in Navicat for MariaDB? Apr 08, 2025 pm 09:18 PM

Navicat for MariaDB cannot view the database password directly because the password is stored in encrypted form. To ensure the database security, there are three ways to reset your password: reset your password through Navicat and set a complex password. View the configuration file (not recommended, high risk). Use system command line tools (not recommended, you need to be proficient in command line tools).

How to view mysql How to view mysql Apr 08, 2025 pm 07:21 PM

View the MySQL database with the following command: Connect to the server: mysql -u Username -p Password Run SHOW DATABASES; Command to get all existing databases Select database: USE database name; View table: SHOW TABLES; View table structure: DESCRIBE table name; View data: SELECT * FROM table name;

How to copy and paste mysql How to copy and paste mysql Apr 08, 2025 pm 07:18 PM

Copy and paste in MySQL includes the following steps: select the data, copy with Ctrl C (Windows) or Cmd C (Mac); right-click at the target location, select Paste or use Ctrl V (Windows) or Cmd V (Mac); the copied data is inserted into the target location, or replace existing data (depending on whether the data already exists at the target location).

How to execute sql in navicat How to execute sql in navicat Apr 08, 2025 pm 11:42 PM

Steps to perform SQL in Navicat: Connect to the database. Create a SQL Editor window. Write SQL queries or scripts. Click the Run button to execute a query or script. View the results (if the query is executed).

See all articles