How to set mysql primary key non-null constraint?
In mysql, you can set primary key constraints by using the "
PRIMARY KEY [default value]" statement in the "CREATE TABLE" statement, using " NOT NULL" statement to set a non-null constraint.
#mysql primary key constraint
The full name of PRIMARY KEY is "primary key constraint", which is the most frequently used constraint in MySQL. Under normal circumstances, in order to facilitate the DBMS to find records in the table faster, a primary key will be set in the table.The following points should be noted when using primary keys:
- Each table can only define one primary key.
- The primary key value must uniquely identify each row in the table and cannot be NULL, that is, there cannot be two rows of data with the same primary key value in the table. This is the principle of uniqueness.
- A field name can only appear once in the joint primary key field table.
- The joint primary key cannot contain unnecessary redundant fields. When a field in the joint primary key is deleted, if the primary key composed of the remaining fields still satisfies the uniqueness principle, then the joint primary key is incorrect. This is the principle of minimization.
Set the primary key constraint when creating the table
Set the primary key constraint when creating the data table. You can also set the primary key for a field in the table. , you can also set a joint primary key for multiple fields in the table. But no matter which method is used, there can only be one primary key in a table. The following explains how to set a single-field primary key and a multi-field joint primary key. 1) Set a single-field primary keyIn the CREATE TABLE statement, specify the primary key through the PRIMARY KEY keyword. Specify the primary key while defining the field. The syntax format is as follows:<字段名> <数据类型> PRIMARY KEY [默认值]
mysql> CREATE TABLE tb_emp3 -> ( -> id INT(11) PRIMARY KEY, -> name VARCHAR(25), -> deptId INT(11), -> salary FLOAT -> ); Query OK, 0 rows affected (0.37 sec) mysql> DESC tb_emp3; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(25) | YES | | NULL | | | deptId | int(11) | YES | | NULL | | | salary | float | YES | | NULL | | +--------+-------------+------+-----+---------+-------+ 4 rows in set (0.14 sec)
[CONSTRAINT <约束名>] PRIMARY KEY [字段名]
mysql> CREATE TABLE tb_emp4 -> ( -> id INT(11), -> name VARCHAR(25), -> deptId INT(11), -> salary FLOAT, -> PRIMARY KEY(id) -> ); Query OK, 0 rows affected (0.37 sec) mysql> DESC tb_emp4; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(25) | YES | | NULL | | | deptId | int(11) | YES | | NULL | | | salary | float | YES | | NULL | | +--------+-------------+------+-----+---------+-------+ 4 rows in set (0.14 sec)
PRIMARY KEY [字段1,字段2,…,字段n]
mysql> CREATE TABLE tb_emp5 -> ( -> name VARCHAR(25), -> deptId INT(11), -> salary FLOAT, -> PRIMARY KEY(id,deptId) -> ); Query OK, 0 rows affected (0.37 sec) mysql> DESC tb_emp5; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | name | varchar(25) | NO | PRI | NULL | | | deptId | int(11) | NO | PRI | NULL | | | salary | float | YES | | NULL | | +--------+-------------+------+-----+---------+-------+ 3 rows in set (0.14 sec)
Add primary key constraints when modifying the table
Primary key constraints can not only be created when the table is created, but can also be added when the table is modified. However, it should be noted that null values are not allowed in fields set as primary key constraints. The syntax format for adding primary key constraints when modifying the data table is as follows:ALTER TABLE <数据表名> ADD PRIMARY KEY(<字段名>);
mysql> DESC tb_emp2; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | id | int(11) | NO | | NULL | | | name | varchar(30) | YES | | NULL | | | deptId | int(11) | YES | | NULL | | | salary | float | YES | | NULL | | +--------+-------------+------+-----+---------+-------+ 4 rows in set (0.14 sec)
mysql> ALTER TABLE tb_emp2 -> ADD PRIMARY KEY(id); Query OK, 0 rows affected (0.94 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC tb_emp2; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(30) | YES | | NULL | | | deptId | int(11) | YES | | NULL | | | salary | float | YES | | NULL | | +--------+-------------+------+-----+---------+-------+ 4 rows in set (0.12 sec)
mysql non-null constraint
MySQL non-null constraint (NOT NULL) means that the value of the field cannot be null. For fields that use non-null constraints, if the user does not specify a value when adding data, the database system will report an error. This can be achieved with the CREATE TABLE or ALTER TABLE statement. Add the keyword NOT NULL as a qualifier after the definition of a column in the table to constrain the value of the column to not be empty.
For example, in the user information table, if the user name is not added, then this user information will be invalid. At this time, you can set a non-null constraint for the user name field.
Set non-null constraints when creating a table
You can use the NOT NULL keyword to set non-null constraints when creating a table. The specific syntax format is as follows:<字段名> <数据类型> NOT NULL
mysql> CREATE TABLE tb_dept4 -> ( -> id INT(11) PRIMARY KEY, -> name VARCHAR(22) NOT NULL, -> location VARCHAR(50) -> ); Query OK, 0 rows affected (0.37 sec) mysql> DESC tb_dept3; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(22) | NO | | NULL | | | location | varchar(50) | YES | | NULL | | +----------+-------------+------+-----+---------+-------+ 3 rows in set (0.06 sec)
在修改表时添加非空约束
如果在创建表时忘记了为字段设置非空约束,也可以通过修改表进行非空约束的添加。
修改表时设置非空约束的语法格式如下:
ALTER TABLE <数据表名> CHANGE COLUMN <字段名> <字段名> <数据类型> NOT NULL;
例 2
修改数据表 tb_dept4,指定部门位置不能为空,SQL 语句和运行结果如下所示。
mysql> ALTER TABLE tb_dept4 -> CHANGE COLUMN location -> location VARCHAR(50) NOT NULL; Query OK, 0 rows affected (0.15 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> DESC tb_dept4; +----------+-------------+------+-----+----------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+----------+-------+ | id | int(11) | NO | PRI | NULL | | | name | varchar(22) | NO | | NULL | | | location | varchar(50) | NO | | NULL | | +----------+-------------+------+-----+----------+-------+ 3 rows in set (0.00 sec)
推荐教程:mysql视频教程
The above is the detailed content of How to set mysql primary key non-null constraint?. For more information, please follow other related articles on the PHP Chinese website!

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

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.
