本文目錄一覽:
- 1、實例講解如何使用C++操作MySQL數據庫類
- 2、mysql workbench 怎麼新建數據庫實例
- 3、mysql8.0怎麼建一個數據庫
- 4、mysql如何變更數據庫實例名
- 5、mysql 數據庫timestamp值怎麼定義
- 6、MySQL(2)數據庫對象與應用
實例講解如何使用C++操作MySQL數據庫類
/* * project: * 通用模塊 ( 用 c++ 處理 mysql 數據庫類,像ADO ) * * description: * * 通過DataBase,RecordSet,Record,Field類,實現對mysql數據庫的操作 * 包括連接、修改、添加、刪除、查詢等等,像ADO一樣操作數據庫,使 * 用方便 * * ( the end of this file have one sample, * welcom to use… ) * * * file:zlb_mysql.h * * author: @ zlb * * time:2005-12-12 * * * –*/ #ifndef ZLB_MYSQL_H #define ZLB_MYSQL_H #include “mysql.h” #include iostream #include vector #include string using namespace std; namespace zlb_mysql{ /* * 字段操作 */ class Field { public : /* 字段名稱 */ vectorstring m_name; /* 字段類型 */ vectorenum_field_types m_type; public : Field(); ~Field(); /* 是否是數字 */ bool IsNum(int num); /* 是否是數字 */ bool IsNum(string num); /* 是否是日期 */ bool IsDate(int num); /* 是否是日期 */ bool IsDate(string num); /* 是否是字符 */ bool IsChar(int num); /* 是否是字符 */ bool IsChar(string num); /* 是否為二進制數據 */ bool IsBlob(int num); /* 是否為二進制數據 */ bool IsBlob(string num); /* 得到指定字段的序號 */ int GetField_NO(string field_name); }; /* * 1 單條記錄 * 2 [int ]操作 [“”]操作 */ class Record { public: /* 結果集 */ vectorstring m_rs; /* 字段信息 佔用4字節的內存 當記錄數很大是回產生性能問題 */ Field *m_field; public : Record(){}; Record(Field* m_f); ~Record(); void SetData(string value); /* [“”]操作 */ string operator[](string s); string operator[](int num); /* null值判斷 */ bool IsNull(int num); bool IsNull(string s); /* 用 value tab value 的形式 返回結果 */ string GetTabText(); }; /* * 1 記錄集合 * 2 [int ]操作 [“”]操作 * 3 表結構操作 * 4 數據的插入修改 */ class RecordSet { private : /* 記錄集 */ vectorRecord m_s; /* 游標位置*/ unsigned long pos; /* 記錄數 */ int m_recordcount; /* 字段數 */ int m_field_num; /* 字段信息 */ Field m_field; MYSQL_RES * res ; MYSQL_FIELD * fd ; MYSQL_ROW row; MYSQL* m_Data ; public : RecordSet(); RecordSet(MYSQL *hSQL); ~RecordSet(); /* 處理返回多行的查詢,返回影響的行數 */ int ExecuteSQL(const char *SQL); /* 得到記錄數目 */ int GetRecordCount(); /* 得到字段數目 */ int GetFieldNum(); /* 向下移動游標 */ long MoveNext(); /* 移動游標 */ long Move(long length); /* 移動游標到開始位置 */ bool MoveFirst(); /* 移動游標到結束位置 */ bool MoveLast(); /* 獲取當前游標位置 */ unsigned long GetCurrentPos()const; /* 獲取當前游標的對應字段數據 */ bool GetCurrentFieldValue(const char * sFieldName,char *sValue); bool GetCurrentFieldValue(const int iFieldNum,char *sValue); /* 獲取游標的對應字段數據 */ bool GetFieldValue(long index,const char * sFieldName,char *sValue); bool GetFieldValue(long index,int iFieldNum,char *sValue); /* 是否到達游標尾部 */ bool IsEof(); /* 返回字段 */ Field* GetField(); /* 返回字段名 */ const char * GetFieldName(int iNum); /* 返回字段類型 */ const int GetFieldType(char * sName); const int GetFieldType(int iNum); /* 返回指定序號的記錄 */ Record operator[](int num); }; /* * 1 負責數據庫的連接關閉 * 2 執行sql 語句(不返回結果) * 3 處理事務 */ class DataBase { public : DataBase(); ~DataBase(); private : /* msyql 連接句柄 */ MYSQL* m_Data; public : /* 返回句柄 */ MYSQL * GetMysql(); /* 連接數據庫 */ int Connect(string host, string user, string passwd, string db, unsigned int port, unsigned long client_flag); /* 關閉數據庫連接 */ void DisConnect(); /* 執行非返回結果查詢 */ int ExecQuery(string sql); /* 測試mysql服務器是否存活 */ int Ping(); /* 關閉mysql 服務器 */ int ShutDown(); /* 主要功能:重新啟動mysql 服務器 */ int ReBoot(); /* * 說明:事務支持InnoDB or BDB表類型 */ /* 主要功能:開始事務 */ int Start_Transaction(); /* 主要功能:提交事務 */ int Commit(); /* 主要功能:回滾事務 */ int Rollback(); /* 得到客戶信息 */ const char * Get_client_info(); /* 主要功能:得到客戶版本信息 */ const unsigned long Get_client_version(); /* 主要功能:得到主機信息 */ const char * Get_host_info(); /* 主要功能:得到服務器信息 */ const char * Get_server_info(); /*主要功能:得到服務器版本信息*/ const unsigned long Get_server_version(); /*主要功能:得到 當前連接的默認字符集*/ const char * Get_character_set_name(); /* 主要功能返回單值查詢 */ char * ExecQueryGetSingValue(string sql); /* 得到系統時間 */ const char * GetSysTime(); /* 建立新數據庫 */ int Create_db(string name); /* 刪除制定的數據庫*/ int Drop_db(string name); }; }; #endif //ZLB_MYSQL_H /* * project: * 通用模塊 ( 用 c++ 處理 mysql 數據庫類,像ADO ) * * description: * * 通過DataBase,RecordSet,Record,Field類,實現對mysql數據庫的操作 * 包括連接、修改、添加、刪除、查詢等等,像ADO一樣操作數據庫,使 * 用方便 * * ( the end of this file have one sample, * welcom to use… ) * * * file:zlb_mysql.cpp * * author: @ zlb * * time:2005-12-12 * * * –*/ #include “stdafx.h” #include “zlb_mysql.h” namespace zlb_mysql{ /* +++++++++++++++++++++++++++++++++++++++++++++++++++ */ /* * 字段操作 */ Field::Field(){} Field::~Field(){} /* * 是否是數字 */ bool Field::IsNum(int num) { if(IS_NUM(m_type[num])) return true; else return false; } /* * 是否是數字 */ bool Field::IsNum(string num) { if(IS_NUM(m_type[GetField_NO(num)])) return true; else return false; } /* * 是否是日期 */ bool Field::IsDate(int num) { if( FIELD_TYPE_DATE == m_type[num] || FIELD_TYPE_DATETIME == m_type[num] ) return true; else return false; } /* 是否是日期 */ bool Field::IsDate(string num) { int temp; temp=GetField_NO(num); if(FIELD_TYPE_DATE == m_type[temp] || FIELD_TYPE_DATETIME == m_type[temp] ) return true; else return false; } /* * 是否是字符 */ bool Field::IsChar(int num) { if(m_type[num]==FIELD_TYPE_STRING || m_type[num]==FIELD_TYPE_VAR_STRING || m_type[num]==FIELD_TYPE_CHAR ) return true; else return false; } /* * 是否是字符 */ bool Field::IsChar(string num) { int temp; temp=this-GetField_NO (num); if(m_type[temp]==FIELD_TYPE_STRING || m_type[temp]==FIELD_TYPE_VAR_STRING || m_type[temp]==FIELD_TYPE_CHAR ) return true; else return false; } /* * 是否為二進制數據 */ bool Field::IsBlob(int num) { if(IS_BLOB(m_type[num])) return true; else return false; } /* * 是否為二進制數據 */ bool Field::IsBlob(string num) { if(IS_BLOB(m_type[GetField_NO(num)])) return true; else return false; } /* * 得到指定字段的序號 */ int Field::GetField_NO(string field_name) { for(unsigned int i=0;im_name.size ();i++) { if(!m_name[i].compare (field_name)) return i; } return -1; } /*—————————————————–*/ /* +++++++++++++++++++++++++++++++++++++++++++++++++++ */ /* * 1 單條記錄 * 2 [int ]操作 [“”]操作 */ Record::Record(Field * m_f) { m_field =m_f; } Record::~Record(){}; void Record::SetData(string value) { m_rs.push_back (value); } /* [“”]操作 */ string Record::operator[](string s) { return m_rs[m_field-GetField_NO(s)]; } string Record::operator[](int num) { return m_rs[num]; } /* null值判斷 */ bool Record::IsNull(int num) { if(“” == m_rs[num].c_str ()) return true; else return false; } bool Record::IsNull(string s) { if(“” == m_rs[m_field-GetField_NO(s)].c_str()) return true; else return false; } /* 主要-功能:用 value tab value 的形式 返回結果 */ string Record::GetTabText() { string temp; for(unsigned int i=0 ;im_rs.size();i++) { temp+=m_rs[i]; if(im_rs.size ()-1) temp+=”\t”; } return temp; } /*—————————————————–*/ /* +++++++++++++++++++++++++++++++++++++++++++++++++++ */ /* * 1 記錄集合 * 2 [int ]操作 [“”]操作 * 3 表結構操作 * 4 數據的插入修改 */ RecordSet::RecordSet() { res = NULL; row = NULL; pos = 0; } RecordSet::RecordSet(MYSQL *hSQL) { res = NULL; row = NULL; m_Data = hSQL; pos = 0; } RecordSet::~RecordSet() { } /* * 處理返回多行的查詢,返回影響的行數 * 成功返回行數,失敗返回-1 */ int RecordSet::ExecuteSQL(const char *SQL) { if ( !mysql_real_query(m_Data,SQL,strlen(SQL))) { //保存查詢結果 res = mysql_store_result(m_Data ); //得到記錄數量 m_recordcount = (int)mysql_num_rows(res) ; //得到字段數量 m_field_num = mysql_num_fields(res) ; for (int x = 0 ; fd = mysql_fetch_field(res); x++) { m_field.m_name.push_back(fd-name); m_field.m_type.push_back(fd-type); } //保存所有數據 while (row = mysql_fetch_row(res)) { Record temp(m_field); for (int k = 0 ; k m_field_num ; k++ ) { if(row[k]==NULL||(!strlen(row[k]))) { temp.SetData (“”); } else { temp.SetData(row[k]); } } //添加新記錄 m_s.push_back (temp); } mysql_free_result(res ) ; return m_s.size(); } return -1; } /* * 向下移動游標 * 返回移動後的游標位置 */ long RecordSet::MoveNext() { return (++pos); } /* 移動游標 */ long RecordSet::Move(long length) { int l = pos + length; if(l0) { pos = 0; return 0; }else { if(l = m_s.size()) { pos = m_s.size()-1; return pos; }else { pos = l; return pos; } } } /* 移動游標到開始位置 */ bool RecordSet::MoveFirst() { pos = 0; return true; } /* 移動游標到結束位置 */ bool RecordSet::MoveLast() { pos = m_s.size()-1; return true; } /* 獲取當前游標位置 */ unsigned long RecordSet::GetCurrentPos()const { return pos; } /* 獲取當前游標的對應字段數據 */ bool RecordSet::GetCurrentFieldValue(const char * sFieldName, char *sValue) { strcpy(sValue,m_s[pos][sFieldName].c_str()); return true; } bool RecordSet::GetCurrentFieldValue(const int iFieldNum,char *sValue) { strcpy(sValue,m_s[pos][iFieldNum].c_str()); return true; } /* 獲取游標的對應字段數據 */ bool RecordSet::GetFieldValue(long index,const char * sFieldName, char *sValue) { strcpy(sValue,m_s[index][sFieldName].c_str()); return true; } bool RecordSet::GetFieldValue(long index,int iFieldNum,char *sValue) { strcpy(sValue,m_s[index][iFieldNum].c_str()); return true; } /* 是否到達游標尾部 */ bool RecordSet::IsEof() { return (pos == m_s.size())?true:false; } /* * 得到記錄數目 */ int RecordSet::GetRecordCount() { return m_recordcount; } /* * 得到字段數目 */ int RecordSet::GetFieldNum() { return m_field_num; } /* * 返回字段 */ Field * RecordSet::GetField() { return m_field; } /* 返回字段名 */ const char * RecordSet::GetFieldName(int iNum) { return m_field.m_name.at(iNum).c_str(); } /* 返回字段類型 */ const int RecordSet::GetFieldType(char * sName) { int i = m_field.GetField_NO(sName); return m_field.m_type.at(i); } const int RecordSet::GetFieldType(int iNum) { return m_field.m_type.at(iNum); } /* * 返回指定序號的記錄 */ Record RecordSet::operator[](int num) { return m_s[num]; } /* ————————————————– */ /* +++++++++++++++++++++++++++++++++++++++++++++++++++ */ /* * 1 負責數據庫的連接關閉 * 2 執行sql 語句(不返回結果) * 3 處理事務 */ DataBase::DataBase() { m_Data = NULL; } DataBase::~DataBase() { if(NULL != m_Data) { DisConnect(); } } /* 返回句柄 */ MYSQL * DataBase::GetMysql() { return m_Data; } /* * 主要功能:連接數據庫 * 參數說明: * 1 host 主機ip地址或者時主機名稱 * 2 user 用戶名 * 3 passwd 密碼 * 4 db 欲連接的數據庫名稱 * 5 port 端口號 * 6 uinx 嵌套字 * 7 client_flag 客戶連接參數 * 返回值: 0成功 -1 失敗 */ int DataBase::Connect(string host, string user, string passwd, string db, unsigned int port, unsigned long client_flag) { if((m_Data = mysql_init(NULL)) mysql_real_connect( m_Data, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(),port , NULL, client_flag)) { //選擇制定的數據庫失敗 if ( mysql_select_db( m_Data, db.c_str () ) 0 ) { mysql_close( m_Data) ; return -1 ; } } else { //初始化mysql結構失敗 mysql_close( m_Data ); return -1 ; } //成功 return 0; } /* * 關閉數據庫連接 */ void DataBase::DisConnect( ) { mysql_close(m_Data) ; } /* * 主要功能: 執行非返回結果查詢 * 參數:sql 待執行的查詢語句 * 返回值; n為成功 表示受到影響的行數 -1 為執行失敗 */ int DataBase::ExecQuery(string sql) { if(!mysql_real_query(m_Data,sql.c_str (),(unsigned long)sql.length()) ) { //得到受影響的行數 return (int)mysql_affected_rows(m_Data) ; } else { //執行查詢失敗 return -1; } } /* * 主要功能:測試mysql服務器是否存活 * 返回值:0 表示成功 -1 失敗 */ int DataBase::Ping() { if(!mysql_ping(m_Data)) return 0; else return -1; } /* * 主要功能:關閉mysql 服務器 * 返回值;0成功 -1 失敗 */ int DataBase::ShutDown() { if(!mysql_shutdown(m_Data,SHUTDOWN_DEFAULT)) return 0; else return -1; } /* * 主要功能:重新啟動mysql 服務器 * 返回值;0表示成功 -1 表示失敗 */ int DataBase::ReBoot() { if(!mysql_reload(m_Data)) return 0; else return -1; } /* * 說明:事務支持InnoDB or BDB表類型 */ /* * 主要功能:開始事務 */ int DataBase::Start_Transaction() { if(!mysql_real_query(m_Data, “START TRANSACTION” , (unsigned long)strlen(“START TRANSACTION”) )) { return 0; } else //執行查詢失敗 return -1; } /* * 主要功能:提交事務 * 返回值:0 表示成功 -1 表示失敗 */ int DataBase::Commit() { if(!mysql_real_query( m_Data, “COMMIT”, (unsigned long)strlen(“COMMIT”) ) ) { return 0; } else //執行查詢失敗 return -1; } /* * 主要功能:回滾事務 * 返回值:0 表示成功 -1 表示失敗 */ int DataBase::Rollback() { if(!mysql_real_query(m_Data, “ROLLBACK”, (unsigned long)strlen(“ROLLBACK”) ) ) return 0; else //執行查詢失敗 return -1; } /* 得到客戶信息 */ const char * DataBase::Get_client_info() { return mysql_get_client_info(); } /*主要功能:得到客戶版本信息*/ const unsigned long DataBase::Get_client_version() { return mysql_get_client_version(); } /* 主要功能:得到主機信息 */ const char * DataBase::Get_host_info() { return mysql_get_host_info(m_Data); } /* 主要功能:得到服務器信息 */ const char * DataBase::Get_server_info() { return mysql_get_server_info( m_Data ); } /* 主要功能:得到服務器版本信息 */ const unsigned long DataBase::Get_server_version() { return mysql_get_server_version(m_Data); } /*主要功能:得到 當前連接的默認字符集*/ const char * DataBase::Get_character_set_name() { return mysql_character_set_name(m_Data); } /* * 主要功能返回單值查詢 */ char * DataBase::ExecQueryGetSingValue(string sql) { MYSQL_RES * res; MYSQL_ROW row ; char *p = NULL; if(!mysql_real_query( m_Data, sql.c_str(),(unsigned long)sql.length())) { //保存查詢結果 res = mysql_store_result( m_Data ) ; row = mysql_fetch_row( res ) ; p = ((row[0]==NULL)||(!strlen(row[0])))?”-1″:row[0]; mysql_free_result( res ) ; } else //執行查詢失敗 p = “-1”; return p; } /* * 得到系統時間 */ const char * DataBase::GetSysTime() { return ExecQueryGetSingValue(“select now()”); } /* * 主要功能:建立新數據庫 * 參數:name 為新數據庫的名稱 * 返回:0成功 -1 失敗 */ int DataBase::Create_db(string name) { string temp ; temp=”CREATE DATABASE “; temp+=name; if(!mysql_real_query( m_Data,temp.c_str () , (unsigned long)temp.length ()) ) return 0; else //執行查詢失敗 return -1; } /* * 主要功能:刪除制定的數據庫 * 參數:name 為欲刪除數據庫的名稱 * 返回:0成功 -1 失敗 */ int DataBase::Drop_db(string name) { string temp ; temp=”DROP DATABASE “; temp+=name; if(!mysql_real_query( m_Data,temp.c_str () , (unsigned long)temp.length ()) ) return 0; else //執行查詢失敗 return -1; } /*—————————————————–*/ }; /* * 使用例子 */ #include “zlb_mysql.h” using namespace std; void main() { zlb_mysql::DataBase zlb; //連接數據庫 if(-1 == zlb.Connect(“localhost”/*本地數據庫,可以是遠程 ip*/, “root”/*用戶名*/,”apple”/*密碼*/, “test”/*數據庫名*/, 0,0/*兩個標誌,mysql文檔有說明,一般為0*/)) { std::cout”connect failed “std::endl; } else { std::cout”connect success”std::endl; } //通過返回的數據庫句柄,建立記錄急,你可以通過返回的這個句柄建立多個記錄急 zlb_mysql::RecordSet rs(zlb.GetMysql()); rs.ExecuteSQL(“select * from testtable”);//這個語句大家都知道是什麼意思了 coutrs.GetRecordCount()/*返回的總的記錄數*/endl; coutrs.GetFieldNum()/*返回的總的字段數*/endl; coutrs[0].GetTabText()/*返回第一條記錄,你也可以rs[1].GetTabText() 如果你有多條記錄, */ endl; /*實現遍列,也可以使用後面的遍列方式*/ for(int i=0;irs.GetRecordCount();++i) { for(int j =0;jrs.GetFieldNum();++j) coutrs[i][j]; coutendl; } zlb_mysql::Field *fd = rs.GetField();/*你可以通過這樣的方式,獲取字段的信息*/ coutfd-GetField_NO(“Password”)/*返回我表裡的 Password 字段的位置,不 是記錄的位置*/ endl; coutrs[0][“Password”]endl;/*輸出第0行第Password列的值*/ coutrs[0][fd-GetField_NO(“Password”)]endl;/*你也可以這樣*/ coutrs.GetFieldName(0)/*獲取字段的名字*/endl; coutrs.GetFieldType(“UserName”)/*獲取字段的類型,是mysql里定義的*/endl; coutrs.GetCurrentPos()/*獲取當前記錄的位置*/endl; char s[50]; rs.GetCurrentFieldValue(1,s);/*獲取當前記錄對應字段的值*/ coutsendl; coutrs.Move(1)endl;/*移動游標,正數往前 負數往後*/ coutrs.GetCurrentPos()endl; rs.GetCurrentFieldValue(1,s); coutsendl; rs.MoveFirst();/*移動游標到最前*/ while(!rs.IsEof()/*判斷是否到達游標尾,實現遍列*/) { rs.GetCurrentFieldValue(“UserName”,s); couts”\t”; rs.GetCurrentFieldValue(“Password”,s); couts”\t
“; rs.MoveNext(); } rs.GetFieldValue(0,”UserName”,s);/*獲取指定行 的記錄值*/ couts”\t”; rs.GetFieldValue(0,”Password”,s); couts”\t
“; }
mysql workbench 怎麼新建數據庫實例
1、首先使用MySQL提供的命令行界面來導入數據庫,確保自己的電腦中安裝了MySQL數據庫,可以通過命令行來確認是否安裝了MySQL數據庫,當然,第一步是打開Mysql的數據庫服務,使用命令行來打開
2、啟動MySQL後,找到需要用到的腳本文件,也就是數據庫文件,當然,首先得建立一個數據庫,這樣才可以導入腳本。
3、在將腳本拷到本地磁盤的根目錄,這樣方便進入找到腳本,這裡以D盤來說明,使用test.sql:接着來到命令行,使用SOURCE d:/test.sql;來導入數據庫,先進入mysql
4、首先要在數據庫中建立好數據庫,然後導入腳本,所以先建立一個數據庫哦,不要腳本是不知道你要往哪個數據庫中導入腳本的
5、然後就可以輸入導入.sql文件命令:
mysql USE 數據庫名;
mysql SOURCE d:/test.sql;
mysql8.0怎麼建一個數據庫
打開MySQL Workbench 8.0 CE軟件,單擊要啟動的MySQL數據庫連接
請點擊輸入圖片描述
輸入密碼後,點擊OK,進入數據庫實例管理界面,可以看到很多數據庫實例
請點擊輸入圖片描述
請點擊輸入圖片描述
接下來,可以新建一個數據庫實例,也可以在已存在的數據庫中創建數據庫實體表,展開要增加表的數據庫實例,右擊 tables,再點擊 create table..
請點擊輸入圖片描述
輸入表名等信息,然後點擊 apply
請點擊輸入圖片描述
確認後,再次點擊 apply,最後點擊 finish,就成功創建數據庫實體表了。
請點擊輸入圖片描述
mysql如何變更數據庫實例名
常見的主要有三種方法:
如果所有表都是MyISAM類型的話,可以直接修改文件夾的名字。關閉mysql→把data目錄中的db_name目錄重命名為new_db_name→開啟mysql
新建數據庫,在新的數據庫里重命名所有舊數據庫中的表,再刪除舊的數據庫。具體操作命令如下:創建新的數據庫→重命名數據表名稱→刪除舊的數據庫。
CREATE DATABASE new_db_name;RENAME TABLE db_name.table1 TO new_db_name.table1,db_name.table2 TO new_db_name.table2;DROP DATABASE db_name;
利用mysqldump命令從舊的數據導出數據,再導入新數據庫。具體操作命令如下:導出數據→創建新的數據庫→導入數據→刪除舊的數據庫。
mysqldump -u root -p -h ip db_name db_name_dump.SQLmysql -u root -p -h ip -e “CREATE DATABASE new_db_name”mysql -u root -p -h ip new_db_name db_name_dump.SQLmysql -u root -p -h ip -e “DROP DATABASE db_name”
mysql 數據庫timestamp值怎麼定義
MySQL數據庫TIMESTAMP設置默認值的靈活運用,本次我們接着上次的內容介紹幾個MySQL數據庫TIMESTAMP設置默認值的幾個應用實例,希望能夠對您有所幫助。
#1查看錶定義,可以看到b列有個屬性ON UPDATE CURRENT_TIMESTAMP,導致更新數據時,即便未涉及到該列,該列數據也被自動更新。另一方面,c列默認值是’0000-00-00 00:00:00’,實際插入已經被自動賦值為current_timestamp。
MySQL(2)數據庫對象與應用
庫建立好之後基本不動,和我們接觸最頻繁的是表. 建表就是聲明字段的過程!
選擇合適的類型[速度快 減少硬盤佔用]
存儲空間,還是存儲範圍有區別?
答案: 兩者本質完全一樣 ,只是在一些特殊情況下兩者顯示有區別(只是在顯示的時候補全0的位數不一樣)
實驗
*zerofill 零填充(本字段同時即自動帶有unsigned屬性,因為負數不能零填充)
如 數字2在固定寬度4時 零填充 即為0002
M值是一個整數(固定寬度值),只有在字段有零填充zerofill屬性時 規定M值才有意義!
M值只是 顯示效果 ,不會影響實際數據值!
如M值為1,實際值255,一樣會顯示255
列可以聲明默認值(推薦聲明)
因為null無法和別的值比較
null = 0 返回null
null 0 返回null
null只能用is或is not比較 null is null當然對的。
例子:
【浮點型】有誤差,不穩定!定點數更精確。
實際測試數據
Float(M,D)
M精度(總位數,不包含點) 精度值M 影響 存儲的 值的範圍.
D標度(小數位) 小數點後有幾位(mysql比較特殊,mssql/oracle都不能指定)
testcolumn float(5,2) unsigned; 範圍0到999.99
float(5,2)的範圍-999.99到999.99
給float(5,2)這樣的字段插入值在進位時有一些規矩:暫時沒搞清楚,不是簡單的四捨五入
插入值688.826實際是688.83 末尾6 進位
插入值688.825實際是688.83 末尾5 進位
插入值688.824實際是688.82 末尾4 捨去
插入值688.005實際是688.00
插入值688.015實際是688.01 末尾5 5前面是1 捨去
插入值688.025實際是688.02 末尾5 5前面是2 捨去
插入值688.035實際是688.03 末尾5 5前面是3 捨去
插入值688.045實際是688.04 末尾5 5前面是4 捨去
一般使用tinyint、char(1)、enum類型。
varchar(M)
M代表寬度 即可容納的【字符數】 (並不是字節數) varchar佔用的字節數與編碼有關:
utf-8 一個漢字3字節 英文字母1字節
對於utf8mb4號稱佔用4字節但是並不絕對(在utf8可以覆蓋到的範圍則仍然佔用3字節)
utf8mb4最有優勢的應用場景:存儲emoji表情
例子:
性能太差,不推薦
MySQL在5.6.4版本之後,TimeStamp和DateTime支持到微妙
一個例子:
以如下這張表為例
show privileges 命令可以查看全部權限
查詢時從user-db-table_pirv-columns_pirv依次驗證,如果通過則執行查詢。
本課程涉及建表SQL
場景1:歌單按時間排序
場景2:統計雲音樂創建歌單的用戶
場景3-1:統計雲音樂創建歌單的用戶列表和每人創建歌單的數量。
場景3-2:統計雲音樂創建歌單的用戶列表和每人創建歌單的數量,並且只顯示歌單數量排序大於等於2的用戶
SQL進階語法-like
場景4:查詢一個月內創建歌單(從第6行開始顯示10條記錄)
場景5:對於未錄入歌曲的歌單(trackcount = null),輸出結果時歌曲數返回0.
連接的作用是用一個SQL語句把多個表中相互關聯的數據查出來
場景6:查詢收藏“老男孩”歌單的用戶列表
子查詢:內層查詢的結果作為外層的比較條件。一般子查詢都可以轉換成連接,推薦使用連接。
場景7:查詢出沒有用戶收藏的歌單
場景8:老闆想看創建和收藏歌單的所有用戶,查詢play_list和play_fav兩表中所有的userid
實例還是上節中的那些表
場景1:查詢每張專輯總的點播次數和每首歌的平均點播次數。
場景2:查詢全部歌曲中的最大的播放次數和最小的播放次數。
場景2續:查詢播放次數最多的歌曲
count(*) 和 count(1) 基本一樣,沒有明顯的性能差異。
count(*) 和 count(song_name) 差別在於 count(song_name) 會除去song_name is null的情況
場景3:顯示每張專輯的歌曲列表
實例:查詢一個月內userid為1,3,5的用戶創建的歌單
學生表:
用於更正成績的觸發器:
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/248168.html