Home > Database > Mysql Tutorial > MySQL database-constraints and paging

MySQL database-constraints and paging

黄舟
Release: 2017-02-11 11:12:50
Original
1406 people have browsed it

This article mainly introduces the data definition table constraints and paging methods of MySQL study notes. It summarizes and analyzes the concepts and usage of data definition, primary key, foreign key, auto-increment, constraints, etc. in the form of examples, and gives some information about paging. For examples and related operation skills, friends in need can refer to

This article describes the data definition table constraints and paging methods of MySQL study notes through examples. 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, and one table can have up to There can only be one primary key, 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
Copy after login

2. auto_increament self-increase

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 |
+----+-------+
Copy after login

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'
Copy after login

Allows insertion of null, and 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 |
+----+-------+
Copy after login

4. not null

The columns of mysql table can be null by default. If a column is not allowed to be empty, you can use the not null description

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
Copy after login

5. foreign key foreign key

Theoretically speaking, first create the main table, and then create the slave table

Employee table:

create table dept(id int primary key , name varchar(32));
Copy after login

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)
Copy after login

Summary:

① Foreign keys can only point to the primary The main column of the table or unique
② 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 pointing to the value existing in the column
④ The foreign key can point to this table The primary key column 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 |
+------+
Copy after login

mysql paging

Basic syntax:

select * from indicates where condition limit from which item to take, how many items to take out
mysql starts to fetch 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)
Copy after login

Sort by Chinese 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)
Copy after login

##Extension, paging: pageNow , pageSize

select * from indicates where condition [group by … having … order by …]limit From which article to take, how many items to take out

select * from indicates where condition [group by … having … order by …]limit (pageNow-1)*pageSize, pageSize

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template