English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MySQL学习笔记之数据定义表约束,分页方法总结

Questo esempio descrive le note di studio di MySQL: definizione delle restrizioni delle tabelle e metodi di paginazione. Condivido con tutti per riferimento, come segue:

1. primary key chiave primaria

Caratteristiche: la chiave primaria è una restrizione utilizzata per identificare un record in modo univoco, una tabella può avere al massimo una chiave primaria, non può essere vuota né ripetuta

create table user1(id int primary key,name varchar(32));
mysql> insert into user1 values(1,'hb');
Query OK, 1 riga influenzata (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): Il campo 'id' non ha un valore predefinito

2. auto_increament crescita automatica

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 secondi)
mysql> select * from user2;
+----+-------+
| id | name |
+----+-------+
| 1 | name1 |
| 2 | name2 |
| 3 | name3 |
+----+-------+

3. vincolo unique (unicità)

Caratteristiche: il valore di una colonna della tabella non può essere ripetuto, può aggiungere NULL duplicati

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): Entrata duplicata 'name3' per la chiave 'name'

Permette l'inserimento di null e può essere utilizzato più di uno

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

Le colonne delle tabelle MySQL possono essere impostate come null per default, se non si desidera che una colonna sia vuota, si può utilizzare not null per indicarlo

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 外键

从理论上说先建立主表,再建立从表

雇员表:

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

部门表:

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 secondi)
mysql> insert into emp values(1,'aaa',1);
Query OK, 1 row affected (0.22 secondi)
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 secondi)
mysql> select * from emp;
+----+------+--------+
| id | name | deptid |
+----+------+--------+
| 1 | aaa |   1 |
| 2 | aaa |  NULL |
+----+------+--------+
2 righe nel set (0.00 sec)

总结:

① 外键只能指向主表的主键列或者unique
② The data type of the foreign key should be consistent with the type of the column it points to
③ The value of the foreign key: NULL or a value existing in the column
④ The foreign key can point to the primary key column or unique of the same table

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 pagination

Basic syntax:

select * from table_name where condition limit start from, number of rows to fetch
mysql starts from the 0th data

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 | Li Laicai |  82 |  84 |  67 |
|  6 | 张进宝   |   55 |   85 |  45 |
|  7 | Zhang Xiaoming |  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 righe nel set (0.00 sec)

OrderBy the Chinese score, investigate the 3rd to the 5th

mysql> select * from student order by chinese desc limit 3,2;
+------+--------+---------+---------+------+
| id  | name  | chinese | english | math |
+------+--------+---------+---------+------+
|  5 | Li Laicai |  82 |  84 |  67 |
|  7 | Zhang Xiaoming |  75 |  65 |  30 |
+------+--------+---------+---------+------+
2 righe nel set (0.00 sec)

Estensione, paginazione: pageNow, pageSize

select * from nome_tabella where condizione [group by … having … order by …]limit partenza da quale riga, numero di righe da prendere
select * from nome_tabella where condizione [group by … having … order by …]limit (pageNow-1)*pageSize, pageSize

Chi è interessato a ulteriori contenuti su MySQL può consultare la sezione speciale di questo sito: 'Riassunto delle tecniche di gestione degli indici MySQL', 'Manuale completo delle tecniche di gestione dei log MySQL', 'Riassunto delle tecniche di gestione delle transazioni MySQL', 'Manuale completo delle tecniche di gestione dei procedimenti memorizzati MySQL', 'Riassunto delle tecniche di gestione dei blocchi del database MySQL' e 'Riassunto delle funzioni più comuni del database MySQL'.

Spero che questo articolo possa essere utile per la gestione del database MySQL.

Dichiarazione: il contenuto di questo articolo è stato raccolto da Internet, il diritto d'autore appartiene ai rispettivi proprietari, il contenuto è stato contribuito e caricato volontariamente dagli utenti di Internet, questo sito non detiene i diritti di proprietà, non è stato elaborato manualmente e non assume responsabilità per le relative responsabilità legali. Se trovi contenuti sospetti di violazione del copyright, ti preghiamo di inviare una email a: notice#oldtoolbag.com (al momento dell'invio dell'email, sostituisci # con @) per segnalare, fornendo prove pertinenti. Una volta verificata, questo sito rimuoverà immediatamente i contenuti sospetti di violazione del copyright.