欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL學(xué)習(xí)筆記之?dāng)?shù)據(jù)定義表約束,分頁方法總結(jié)

 更新時(shí)間:2016年09月14日 09:00:40   作者:hbiao68  
這篇文章主要介紹了MySQL學(xué)習(xí)筆記之?dāng)?shù)據(jù)定義表約束,分頁方法,結(jié)合實(shí)例形式總結(jié)分析了數(shù)據(jù)定義、主鍵、外鍵、自增長、約束等概念與用法,并給出了關(guān)于分頁的實(shí)例與相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了MySQL學(xué)習(xí)筆記之?dāng)?shù)據(jù)定義表約束,分頁方法。分享給大家供大家參考,具體如下:

1. primary key 主鍵

特點(diǎn):主鍵是用于唯一標(biāo)識(shí)一條記錄的約束,一張表最多只能有一個(gè)主鍵,不能為空也不能重復(fù)

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 自增長

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 唯一約束

特點(diǎn):表的某列值不能重復(fù),可以添加重復(fù)的NULL

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'

允許插入null,并且可以多個(gè)

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表的列默認(rèn)情況下可以為null,如果不允許某列為空則可以使用not null說明

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

總結(jié):

① 外鍵只能指向主表的主見列或者unique
② 外鍵的數(shù)據(jù)類型應(yīng)該與它指向的列類型一致
③ 外鍵的值:NULL 或者 指向列中存在的值
④ 外鍵可以指向本表的主鍵列或者unique

mysql 不支持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 分頁

基本語法:

select * from 表明 where 條件 limit 從第幾條取,取出幾條
mysql 是從第0條開始取數(shù)據(jù)

mysql> select * from student;
+------+--------+---------+---------+------+
| id  | name  | chinese | english | math |
+------+--------+---------+---------+------+
|  1 | 張小明   |   89 |   78 |  90 |
|  2 | 李進(jìn)    |   67 |   98 |  56 |
|  3 | 王五    |   87 |   78 |  77 |
|  4 | 李一   |   88 |   98 |  90 |
|  5 | 李來財(cái)    |   82 |   84 |  67 |
|  6 | 張進(jìn)寶   |   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)

按照語文成績排序,查處第3條到第5條

mysql> select * from student order by chinese desc limit 3,2;
+------+--------+---------+---------+------+
| id  | name  | chinese | english | math |
+------+--------+---------+---------+------+
|  5 | 李來財(cái)    |   82 |   84 |  67 |
|  7 | 張小明   |   75 |   65 |  30 |
+------+--------+---------+---------+------+
2 rows in set (0.00 sec)

擴(kuò)展,分頁:pageNow , pageSize

select * from 表明 where 條件 [group by … having … order by …]limit 從第幾條取,取出幾條
select * from 表明 where 條件 [group by … having … order by …]limit (pageNow-1)*pageSize, pageSize

更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL索引操作技巧匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲(chǔ)過程技巧大全》、《MySQL數(shù)據(jù)庫鎖相關(guān)技巧匯總》及《MySQL常用函數(shù)大匯總

希望本文所述對大家MySQL數(shù)據(jù)庫計(jì)有所幫助。

相關(guān)文章

  • MySQL 創(chuàng)建三張關(guān)系表實(shí)操

    MySQL 創(chuàng)建三張關(guān)系表實(shí)操

    這篇文章主要介紹了MySQL 創(chuàng)建三張關(guān)系表實(shí)操,文章說先創(chuàng)建學(xué)生表然后科目表和分?jǐn)?shù)表三張有著密切關(guān)系的表,下文實(shí)操分享需要的小伙伴可以參考一下
    2022-03-03
  • MySQL之存儲(chǔ)函數(shù)詳細(xì)介紹

    MySQL之存儲(chǔ)函數(shù)詳細(xì)介紹

    大家好,本篇文章主要講的是MySQL之存儲(chǔ)函數(shù)詳細(xì)介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • MySQL數(shù)據(jù)庫觸發(fā)器從小白到精通

    MySQL數(shù)據(jù)庫觸發(fā)器從小白到精通

    觸發(fā)器是SQLserver提供給程序員和數(shù)據(jù)分析員來保證數(shù)據(jù)完整性的一種方法,它是與表事件相關(guān)的特殊的存儲(chǔ)過程,它的執(zhí)行不是由程序調(diào)用,也不是手工啟動(dòng),而是由事件來觸發(fā),比如當(dāng)對一個(gè)表進(jìn)行操作時(shí)就會(huì)激活它執(zhí)行。觸發(fā)器經(jīng)常用于加強(qiáng)數(shù)據(jù)的完整性約束和業(yè)務(wù)規(guī)則等
    2022-03-03
  • B-樹的插入過程介紹

    B-樹的插入過程介紹

    今天小編就為大家分享一篇關(guān)于B-樹的插入過程介紹,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • MySQL觸發(fā)器運(yùn)用于遷移和同步數(shù)據(jù)的實(shí)例教程

    MySQL觸發(fā)器運(yùn)用于遷移和同步數(shù)據(jù)的實(shí)例教程

    這篇文章主要介紹了MySQL觸發(fā)器運(yùn)用于遷移和同步數(shù)據(jù)的實(shí)例教程,分別是SQL Server數(shù)據(jù)遷移至MySQL以及同步備份數(shù)據(jù)表記錄的兩個(gè)例子,需要的朋友可以參考下
    2015-12-12
  • mysql-8.0.35-winx64?zip版安裝教程(附圖文)

    mysql-8.0.35-winx64?zip版安裝教程(附圖文)

    許多人在學(xué)習(xí)過程中經(jīng)常因使用不當(dāng)將MySQL數(shù)據(jù)庫搞崩潰,這篇文章主要給大家介紹了關(guān)于mysql-8.0.35-winx64?zip版安裝教程的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • MySQL利用UNION連接2個(gè)查詢排序失效詳解

    MySQL利用UNION連接2個(gè)查詢排序失效詳解

    這篇文章主要給大家介紹了關(guān)于MySQL利用UNION連接2個(gè)查詢排序失效的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • MySQL存儲(chǔ)引擎中的MyISAM和InnoDB區(qū)別詳解

    MySQL存儲(chǔ)引擎中的MyISAM和InnoDB區(qū)別詳解

    這篇文章主要介紹了MySQL存儲(chǔ)引擎中的MyISAM和InnoDB區(qū)別詳解,本文總結(jié)了MyISAM與InnoDB的11點(diǎn)區(qū)別,需要的朋友可以參考下
    2015-03-03
  • MySQL導(dǎo)出sql腳本文件操作指南

    MySQL導(dǎo)出sql腳本文件操作指南

    mysql數(shù)據(jù)庫是非常常用的一種數(shù)據(jù)庫,屬于中小型數(shù)據(jù)庫,常用于網(wǎng)站業(yè)務(wù)和一些WEB系統(tǒng)業(yè)務(wù),下面這篇文章主要給大家介紹了關(guān)于MySQL導(dǎo)出sql腳本文件操作的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • MySQL索引最左匹配原則實(shí)例詳解

    MySQL索引最左匹配原則實(shí)例詳解

    最左匹配原則就是指在聯(lián)合索引中,如果你的SQL語句中用到了聯(lián)合索引中的最左邊的索引,那么這條SQL語句就可以利用這個(gè)聯(lián)合索引去進(jìn)行匹配,下面這篇文章主要給大家介紹了關(guān)于MySQL索引最左匹配原則的相關(guān)資料,需要的朋友可以參考下
    2022-09-09

最新評(píng)論