標籤:scribe 查看 span 文法 ble row 建表 port sed
一 儲存引擎介紹
儲存引擎即表類型,mysql根據不同的表類型會有不同的處理機制
http://www.cnblogs.com/guoyunlong666/p/8491702.html
二 表介紹
表相當於檔案,表中的一條記錄就相當於檔案的一行內容,不同的是,表中的一條記錄有對應的標題,稱為表的欄位
id,name,qq,age稱為欄位,其餘的,一行內容稱為一條記錄
三 建立表
#文法:create table 表名(欄位名1 類型[(寬度) 約束條件],欄位名2 類型[(寬度) 約束條件],欄位名3 類型[(寬度) 約束條件]);#注意:1. 在同一張表中,欄位名是不能相同2. 寬度和約束條件可選3. 欄位名和類型是必須的
MariaDB [(none)]> create database db1 charset utf8;MariaDB [(none)]> use db1;MariaDB [db1]> create table t1( -> id int, -> name varchar(50), -> sex enum(‘male‘,‘female‘), -> age int(3) -> );MariaDB [db1]> show tables; #查看db1庫下所有表名MariaDB [db1]> desc t1;+-------+-----------------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-----------------------+------+-----+---------+-------+| id | int(11) | YES | | NULL | || name | varchar(50) | YES | | NULL | || sex | enum(‘male‘,‘female‘) | YES | | NULL | || age | int(3) | YES | | NULL | |+-------+-----------------------+------+-----+---------+-------+MariaDB [db1]> select id,name,sex,age from t1;Empty set (0.00 sec)MariaDB [db1]> select * from t1;Empty set (0.00 sec)MariaDB [db1]> select id,name from t1;Empty set (0.00 sec)
View Code
MariaDB [db1]> insert into t1 values -> (1,‘egon‘,18,‘male‘), -> (2,‘alex‘,81,‘female‘) -> ;MariaDB [db1]> select * from t1;+------+------+------+--------+| id | name | age | sex |+------+------+------+--------+| 1 | egon | 18 | male || 2 | alex | 81 | female |+------+------+------+--------+MariaDB [db1]> insert into t1(id) values -> (3), -> (4);MariaDB [db1]> select * from t1;+------+------+------+--------+| id | name | age | sex |+------+------+------+--------+| 1 | egon | 18 | male || 2 | alex | 81 | female || 3 | NULL | NULL | NULL || 4 | NULL | NULL | NULL |+------+------+------+--------+
往表中插入資料
注意注意注意:表中的最後一個欄位不要加逗號
四 查看錶結構
MariaDB [db1]> describe t1; #查看錶結構,可簡寫為desc 表名+-------+-----------------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-----------------------+------+-----+---------+-------+| id | int(11) | YES | | NULL | || name | varchar(50) | YES | | NULL | || sex | enum(‘male‘,‘female‘) | YES | | NULL | || age | int(3) | YES | | NULL | |+-------+-----------------------+------+-----+---------+-------+MariaDB [db1]> show create table t1\G; #查看錶詳細結構,可加\G
五 資料類型
http://www.cnblogs.com/guoyunlong666/p/8491773.html
六 表完整性條件約束
http://www.cnblogs.com/guoyunlong666/p/8491875.html
七 修改表ALTER TABLE
文法:1. 修改表名 ALTER TABLE 表名 RENAME 新表名;2. 增加欄位 ALTER TABLE 表名 ADD 欄位名 資料類型 [完整性條件約束條件…], ADD 欄位名 資料類型 [完整性條件約束條件…]; ALTER TABLE 表名 ADD 欄位名 資料類型 [完整性條件約束條件…] FIRST; ALTER TABLE 表名 ADD 欄位名 資料類型 [完整性條件約束條件…] AFTER 欄位名; 3. 刪除欄位 ALTER TABLE 表名 DROP 欄位名;4. 修改欄位 ALTER TABLE 表名 MODIFY 欄位名 資料類型 [完整性條件約束條件…]; ALTER TABLE 表名 CHANGE 舊欄位名 新欄位名 舊資料類型 [完整性條件約束條件…]; ALTER TABLE 表名 CHANGE 舊欄位名 新欄位名 新資料類型 [完整性條件約束條件…];
樣本:1. 修改儲存引擎mysql> alter table service -> engine=innodb;2. 添加欄位mysql> alter table student10 -> add name varchar(20) not null, -> add age int(3) not null default 22; mysql> alter table student10 -> add stu_num varchar(10) not null after name; //添加name欄位之後mysql> alter table student10 -> add sex enum(‘male‘,‘female‘) default ‘male‘ first; //添加到最前面3. 刪除欄位mysql> alter table student10 -> drop sex;mysql> alter table service -> drop mac;4. 修改欄位類型modifymysql> alter table student10 -> modify age int(3);mysql> alter table student10 -> modify id int(11) not null primary key auto_increment; //修改為主鍵5. 增加約束(針對已有的主鍵增加auto_increment)mysql> alter table student10 modify id int(11) not null primary key auto_increment;ERROR 1068 (42000): Multiple primary key definedmysql> alter table student10 modify id int(11) not null auto_increment;Query OK, 0 rows affected (0.01 sec)Records: 0 Duplicates: 0 Warnings: 06. 對已經存在的表增加複合主鍵mysql> alter table service2 -> add primary key(host_ip,port); 7. 增加主鍵mysql> alter table student1 -> modify name varchar(10) not null primary key;8. 增加主鍵和自動成長mysql> alter table student1 -> modify id int not null primary key auto_increment;9. 刪除主鍵a. 刪除自增約束mysql> alter table student10 modify id int(11) not null; b. 刪除主鍵mysql> alter table student10 -> drop primary key;
樣本八 複製表
複製表結構+記錄 (key不會複製: 主鍵、外鍵和索引)mysql> create table new_service select * from service;只複製表結構mysql> select * from service where 1=2; //條件為假,查不到任何記錄Empty set (0.00 sec)mysql> create table new1_service select * from service where 1=2; Query OK, 0 rows affected (0.00 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> create table t4 like employees;
九 刪除表
DROP TABLE 表名;
DAY10-MYSQL表操作