MySQL初識
記不住過去,看不透未來——失敗。
MySQL基礎知識點羅列,幫助理清思路,主要包含安裝、基本操作、MySQL約束、C/C++庫使用等知識點。
Ubuntu下安裝MySQL
按照順序執行下面三條指令:
$ sudo apt-get install mysql-server
$ sudo apt-get install mysql-client
$ sudo apt-get install libmysqlclient-dev
安裝完成,檢查MySQL是否成功安裝:
$ sudo netstat -tap | grep mysql
記住安裝過程中需要輸入的密碼,通過下列命令可以登錄mysql數據庫:
$ mysql -u root -p # -u for user, -p for password
安裝后,MySQL是默認啟動的,如果需要自己控制,則可以執行下列命令:
$ sudo /etc/init.d/mysql start
$ sudo /etc/init.d/mysql stop
$ sudo /etc/init.d/mysql restart
$ sudo service mysql start
$ sudo service mysql stop
$ sudo service mysql restart
MySQL基本操作
登錄操作
$ mysql -u root -p -P 3306 -h 127.0.0.1
# -u --user
# -p --passwd
# -P --port, 3306 is the default port for mysql
# -h --host
設置提示符
mysql> prompt \u@\h \d>
PROMPT set to '\u@\h \d> '
root@localhost (none) > use test
root@localhost test >
# \u 設置顯示用戶名
# \h 設置顯示主機
# \d 設置顯示當前數據庫
# \D 設置顯示完整日期
查詢警告
mysql > show warnings;
查詢命令幫助
使用 help 命令能夠找到MySQL語句的具體使用幫助指南,我們以 create database 舉例:
mysql> help create;
mysql> help create database;
mysql> help alter;
mysql> help alter database;
MySQL數據庫操作
數據庫創建
使用 create database 創建數據庫t1,并顯示詳細信息:
root@localhost test > create database t1;
root@localhost test > show databases;
root@localhost test > show create database t1;
root@localhost test > create database if not exists t2 character set gbk;
數據庫修改
root@localhost test > alter database t2 character set = utf8;
數據庫刪除
root@localhost test > drop database t1;
使用數據庫
root@localhost test > use t1;
root@localhost t1 > select database(); # 顯示當前數據庫
創建數據庫表
root@localhost test > create table tb1(
-> username varchar(20),
-> age tinyint unsigned,
-> salary float(8,2) unsigned
-> );
查看數據庫表
root@localhost test > show tables; # 查看數據庫表列表
root@localhost test > show tables from test; # 查看數據庫test表列表
root@localhost test > show columns from tb1; # 查看數據庫表結構
插入記錄到數據庫表
root@localhost test > insert tb1 values('Tom', 25, 7863.25);
root@localhost test > insert tb1 values('Tom', 25); # error
root@localhost test > insert tb1(username,salary) values('John', 4500.69); # ok
記錄查找
root@localhost test > select * from tb1;
MySQL數據類型
整形
數據類型 | 存儲需求 |
---|---|
TINYINT | 8位 |
SMALLINT | 16位 |
MEDIUMINT | 24位 |
INT | 32位 |
BIGINT | 64位 |
浮點型
數據類型 | 存儲需求 |
---|---|
FLOAT(M,D) | M是數字總位數,D是小數點后面的位數 |
DOUBLE(M,D) |
日期時間型
列類型 | 存儲需求 | 范圍 |
---|---|---|
YEAR | 8位 | 1901~2155 |
TIME | 24位 | -838:59:59~838:59:59 |
DATE | 24位 | 1000.01.01~9999.12.31 |
DATETIME | 64位 | 1000.01.01 00:00:00~9999.12.31 23:59:59 |
TIMESTAMP | 32位 | 1970.01.01 00:00:01~2037 年某時 |
字符類型
列類型 | 存儲需求 |
---|---|
CHAR(M) | 固定長度,M個字節,0<=<M<=255 |
VARCHAR(M) | 可變長度,L+1個字節,L<=M且0<=M<=2^16 -1 |
TINYTEXT | L+1個字節,L<2^8 |
TEXT | L+1個字節,L<2^16 |
MEDIUMTEXT | L+1個字節,L<2^24 |
LONGTEXT | L+1個字節,L<2^32 |
ENUM(‘value1’,’value2’,…) | 1或2個字節,最多2^16 -1個值 |
SET(‘value1’,’value2’,…) | 1、2、3、4或8個字節,最多64個成員 |
MySQL約束
主鍵約束
NOT NULL 和 UNIQUE 的結合。確保某列(或兩個列多個列的結合)有唯一標識,有助于更容易更快速地找到表中的一個特定的記錄。
AUTO_INCREMENT 必須與 主鍵 組合使用,其會在新記錄插入表中時生成一個唯一的數字。默認情況下,起始值為1,每次的增量為1。
root@localhost test > create table tb3(
-> id smallint unsigned AUTO_INCREMENT PRIMARY KEY,
-> username varchar(30) NOT NULL
-> );
root@localhost test > show columns from tb3;
root@localhost test > insert tb3(username) values('Tom');
root@localhost test > insert tb3(username) values('John');
root@localhost test > select * from tb3;
非空約束
NOT NULL 指示某列不能存儲 NULL 值。在默認的情況下,表的列接受 NULL 值。
- NULL,字段值可以為空
- NOT NULL,字段值禁止為空
root@localhost test > create table tb2(
-> username varchar(20) NOT NULL,
-> age tinyint unsigned NULL
-> );
root@localhost test > insert tb2 values('tom',NULL); # ok
root@localhost test > insert tb2 values(NULL,25); # error
唯一約束
UNIQUE 保證某列的每行必須有唯一的值。唯一約束的字段可以為NULL值,但是必須只有一個NULL值。
請注意,每個表可以有多個 UNIQUE 約束,但是每個表只能有一個 PRIMARY KEY 約束。
root@localhost test > create table tb5(
-> id smallint unsigned AUTO_INCREMENT PRIMARY KEY,
-> username varchar(20) NOT NULL UNIQUE KEY,
-> age tinyint unsigned
-> );
root@localhost test > show columns from tb5;
root@localhost test > insert tb5(username, age) values('Tom',22);
root@localhost test > insert tb5(username, age) values('Tom',22); # error
默認約束
為沒用明確指出值的字段賦予默認值。
root@localhost test > create table tb6(
-> id smallint unsigned AUTO_INCREMENT PRIMARY KEY,
-> username varchar(20) NOT NULL UNIQUE KEY,
-> sex ENUM('1','2','3') DEFAULT '3'
-> );
root@localhost test > insert tb6(username) values('Tom');
root@localhost test > select * from tb6;
使用C/C++語言來操作MySQL
我們可以使用命令行來操作MySQL數據庫,譬如:
root@localhost test > show databases;
root@localhost test > use mysql;
root@localhost test > show tables;
也可以通過MySQL對外公布的C接口來操作數據庫,譬如:
// show_tables.cxx
#include<mysql/mysql.h>
#include<stdio.h>
#include<stdlib.h>
intmain(){
MYSQL * conn;
MYSQL_RES * res;
MYSQL_ROW row;
char server[] = "localhost";
char user[] = "root";
char passwd[] = "lovemime";
char database[] = "mysql";
conn = mysql_init(NULL);
if(!mysql_real_connect(conn, server, user, passwd, database, 0, NULL, 0)) {
fprintf(stderr, "%s/n", mysql_error(conn));
exit(1);
}
if(mysql_query(conn, "show tables")){
fprintf(stderr, "%s/n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
printf("MySQL Tables in mysql database:\n");
while((row =mysql_fetch_row(res)) != NULL){
printf("%s \n", row[0]);
}
mysql_free_result(res);
mysql_close(conn);
printf("finish! \n");
return 0;
}
連接MySQL庫,來編譯該段代碼:
$ g++ -Wall show_tables.cxx -o showtables -lmysqlclient
參考鏈接
來自:http://freehacker.cn/reading/db/mysql-basic/
本文由用戶 LinStclair 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!