Home > Database > Mysql Tutorial > MySQL study notes data definition table constraints, summary of paging methods_MySQL

MySQL study notes data definition table constraints, summary of paging methods_MySQL

WBOY
Release: 2016-10-09 08:33:39
Original
1119 people have browsed it

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

Copy after login

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 |
+----+-------+

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 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 |
+----+-------+

Copy after login

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

Copy after login

5. foreign key foreign key

Theoretically speaking, first create the master 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 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 |
+------+

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 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)

Copy after login

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)

Copy after login

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.

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