


MySQL study notes data definition table constraints, summary of paging methods_MySQL
The example in this article describes the data definition table constraints and paging method of MySQL study notes. Share it with everyone for your reference, the details are as follows:
1. primary key primary key
Features: The primary key is a constraint used to uniquely identify a record. A table can only have one primary key at most, and it cannot be empty or repeated
create table user1(id int primary key,name varchar(32)); mysql> insert into user1 values(1,'hb'); Query OK, 1 row affected (0.10 sec) mysql> insert into user1 values(1,'hb'); ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY' mysql> insert into user1 (name) values('hb'); ERROR 1364 (HY000): Field 'id' doesn't have a default value
2. auto_increament self-increasing
mysql> create table user2(id int primary key auto_increment,name varchar(34)); mysql> insert into user2 (name ) values ("name1"); Query OK, 1 row affected (0.09 sec) mysql> insert into user2 (name ) values ("name2"); Query OK, 1 row affected (0.05 sec) mysql> insert into user2 (name ) values ("name3"); Query OK, 1 row affected (0.13 sec) mysql> select * from user2; +----+-------+ | id | name | +----+-------+ | 1 | name1 | | 2 | name2 | | 3 | name3 | +----+-------+
3. unique unique constraint
Features: A certain column value in the table cannot be repeated, and repeated NULLs can be added
create table user3(id int primary key auto_increment,name varchar(34) unique); mysql> create table user3(id int primary key auto_increment,name varchar(34) unique); Query OK, 0 rows affected (0.39 sec) mysql> insert into user3 (name ) values ("name3"); Query OK, 1 row affected (0.11 sec) mysql> insert into user3 (name ) values ("name3"); ERROR 1062 (23000): Duplicate entry 'name3' for key 'name'
Allows to insert null, and there can be multiple
mysql> insert into user3 (name ) values (null); Query OK, 1 row affected (0.12 sec) mysql> insert into user3 (name ) values (null); Query OK, 1 row affected (0.12 sec) mysql> select * from user3; +----+-------+ | id | name | +----+-------+ | 3 | NULL | | 4 | NULL | | 1 | name3 | +----+-------+
4. not null
MySQL table columns can be null by default. If a column is not allowed to be empty, you can use the not null statement
create table user4 (id int primary key auto_increment,name varchar(32) not null); mysql> insert into user4 (name) values(null); ERROR 1048 (23000): Column 'name' cannot be null
5. foreign key foreign key
Theoretically speaking, create the master table first and then create the slave table
Employee table:
create table dept(id int primary key , name varchar(32));
Department table:
create table emp( id int primary key , name varchar(32), deptid int, constraint myforeignkey foreign key(deptid) references dept(id) ); mysql> select * from dept; +----+-------+ | id | name | +----+-------+ | 1 | name1 | +----+-------+ 1 row in set (0.00 sec) mysql> insert into emp values(1,'aaa',1); Query OK, 1 row affected (0.22 sec) mysql> insert into emp values(1,'aaa',2); ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY' mysql> insert into emp values(1,'aaa',null); ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY' mysql> insert into emp values(2,'aaa',null); Query OK, 1 row affected (0.13 sec) mysql> select * from emp; +----+------+--------+ | id | name | deptid | +----+------+--------+ | 1 | aaa | 1 | | 2 | aaa | NULL | +----+------+--------+ 2 rows in set (0.00 sec)
Summary:
① Foreign keys can only point to primary columns or unique columns of the main table
② The data type of the foreign key should be consistent with the column type it points to
③ The value of the foreign key: NULL or points to the value existing in the column
④ The foreign key can point to the primary key column of this table or unique
mysql does not support check
create table user99(age int check(age>13)); mysql> create table user99(age int check(age>13)); Query OK, 0 rows affected (0.19 sec) mysql> insert into user99 values(99); Query OK, 1 row affected (0.04 sec) mysql> select * from user99; +------+ | age | +------+ | 99 | +------+
mysql paging
Basic syntax:
select * from indicates where condition limit from which item to take, how many items to take out
mysql starts fetching data from item 0
mysql> select * from student; +------+--------+---------+---------+------+ | id | name | chinese | english | math | +------+--------+---------+---------+------+ | 1 | 张小明 | 89 | 78 | 90 | | 2 | 李进 | 67 | 98 | 56 | | 3 | 王五 | 87 | 78 | 77 | | 4 | 李一 | 88 | 98 | 90 | | 5 | 李来财 | 82 | 84 | 67 | | 6 | 张进宝 | 55 | 85 | 45 | | 7 | 张小明 | 75 | 65 | 30 | +------+--------+---------+---------+------+ 7 rows in set (0.05 sec) mysql> select * from student limit 2,2; +------+------+---------+---------+------+ | id | name | chinese | english | math | +------+------+---------+---------+------+ | 3 | 王五 | 87 | 78 | 77 | | 4 | 李一 | 88 | 98 | 90 | +------+------+---------+---------+------+ 2 rows in set (0.00 sec)
In order of Chinese language scores, investigate items 3 to 5
mysql> select * from student order by chinese desc limit 3,2; +------+--------+---------+---------+------+ | id | name | chinese | english | math | +------+--------+---------+---------+------+ | 5 | 李来财 | 82 | 84 | 67 | | 7 | 张小明 | 75 | 65 | 30 | +------+--------+---------+---------+------+ 2 rows in set (0.00 sec)
Extensions, paging: pageNow, pageSize
select * from indicates where condition [group by … having … order by …]limit from which item to take, how many items to take out
select * from indicates where condition [group by … having … order by …]limit (pageNow-1)*pageSize, pageSize
Readers who are interested in more MySQL-related content can check out the special topics on this site: "Summary of MySQL Index Operation Skills", "Comprehensive Collection of MySQL Log Operation Skills", "Summary of MySQL Transaction Operation Skills", "Comprehensive Collection of MySQL Stored Procedure Skills", " Summary of MySQL database lock related skills" and "Summary of commonly used MySQL functions"
I hope this article will be helpful to everyone’s MySQL database planning.

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

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.
