mysql 自动化运维开发_Python自动化运维开发----基础(十三)Mysql数据库基础
1.MYSQL 語言的分類
(1) DDL 數(shù)據(jù)庫定義
(2) DQL 數(shù)據(jù)庫查詢
(3) DML 數(shù)據(jù)庫操作
(4) DCL? 數(shù)據(jù)庫權(quán)限
2.MYSQL? 操作
(1) 創(chuàng)建數(shù)據(jù)庫mysql>?create?database?cmdb?default?charset?utf8;
(2)查看所有的數(shù)據(jù)庫mysql>?show?databases;
+--------------------+
|?Database???????????|
+--------------------+
|?information_schema?|
|?cmdb???????????????|
|?mysql??????????????|
|?performance_schema?|
|?sys????????????????|
+--------------------+
5?rows?in?set?(0.00?sec)
(3) 使用cmdb數(shù)據(jù)庫mysql>?use?cmdb;
(4) 查看數(shù)據(jù)庫的創(chuàng)建語法mysql>?show?create?database?cmdb;
(5) 刪除數(shù)據(jù)庫mysql>?drop?database?cmdb;
(6) 查看所有的表mysql>?show?tables;
(7)? 創(chuàng)建用戶表mysql>?create?table?user(id?int,name?varchar(64),age?int,?sex?boolean,telphone?varchar(32),?addr?varchar(512))engine=innodb?default?charset?utf8;
(8)? 查看創(chuàng)建表的過程mysql>?show?create?table?user;
(9)? 刪除表mysql>?drop?table?user;
(10)? 查看表結(jié)構(gòu)mysql>?desc?user;
(11)? 插入數(shù)據(jù)mysql>?insert?into?user(id,name,age,sex,telphone,addr)values(1,'李寬',25,1,'18829787559','陜西省西安市');
(12)? 查看數(shù)據(jù)mysql>?select?*?from?user;
(13)? 只查詢指定的列mysql>?select?name,addr?from?user;
(14)? 條件查詢
where
邏輯關(guān)聯(lián)詞? and? or
關(guān)系表達(dá)式? >? =? <=? !=
like表達(dá)式
(1) % 占多位? ? 'abc%'? ?'%abc'
(2) _ 占一位? ? ? ‘a(chǎn)bc_’ '_abc'
in 的使用? ? ? ?colname? in (a,b)
not in 的使用? ?colname not in (a,b)select?name,age,addr?from?user?where?addr?=?'陜西省西安市'?and?age=25;
mysql>?select?name,age,addr?from?user?where?addr?=?'陜西省西安市'?or?age?=?25;
select?name,age,addr?from?user?where?addr?=?'陜西省西安市'?or?age?>?25;
mysql>?select?name,age,addr?from?user?where?age?>=?25;
mysql>?select?name,age,addr?from?user?where?age?!=?25;
select?name,age,addr?from?user?where?age?
mysql>?select?name,age,addr?from?user?where?addr?like?'陜西省%';
mysql>?select?name,age,addr?from?user?where?addr?like?'%市';
mysql>?select?name,age,addr?from?user?where?not?(addr?like?'臨汾市');
mysql>?select?name,age,addr?from?user?where?age?in?(23,25);
mysql>?select?name,sex,age,addr?from?user?where?age?not?in?(15,25);
(15)? 查詢總數(shù)mysql>?select?count(*)?from?user;
3.創(chuàng)建CMDB的用戶表
建表的sql,性別在數(shù)據(jù)庫中存儲(chǔ)的時(shí)候,男存1,女存0CREATE?TABLE?user(
id?int?primary?key?auto_increment,
name?varchar(32)?unique?not?null?default?'',
password?varchar(512)?not?null?default?'',
age?int?not?null?default?18,
sex?boolean?not?null?default?1,
tel?varchar(16)?not?null?default?'',
addr?text,
add_time?datetime
)ENGINE=INNODB?DEFAULT?CHARSET?utf8mb4;
批量插入測試數(shù)據(jù)insert?into?user(name,?password,?age,?sex,?tel,?addr,?add_time)?values?('kk',?md5('kk'),?30,?1,?'15200000000',?'西安市',?now()),\
('woniu',?md5('woniu'),?30,?1,?'15200000001',?'北京市',?now()),('zhangzhengguang',?md5('zhangzhengguang'),?30,?1,?'15200000003',?'杭州市',?now()),\
('likuan',?md5('likuan'),?30,?1,?'15200000002',?'西安市',?now())
查看用戶登錄的用戶名和密碼mysql>?select?name,password?from?user?where?name='likuan'?and?password=md5('likuan');
查找所有的數(shù)據(jù)mysql>?select?id,name,password,age,sex,tel,addr?from?user?;
限制查詢的數(shù)據(jù) (limit可以用來做分頁)mysql>?select?id,name,password,age,sex,tel,addr?from?user?limit?1;
Limit?和?offset結(jié)合使用mysql>?select?id,name,password,age,sex,tel,addr?from?user?limit?2?offset?2;
排序 (降序和升序)
降序(desc)Mysql>?select?id,name,password,age,sex,tel,addr?from?user?order?by?age?desc;
升序(asc)mysql>?select?id,name,password,age,sex,tel,addr?from?user?order?by?age?asc;
更新操作mysql>?update?user?set?age=15?where?id?=?3;
mysql>?update?user?set?name='kk',tel='152',sex=1,addr='西安市'?where?id?=?1;
刪除操作mysql>?delete?from?user?where?id?=?1;
mysql>?delete?from?user;
聚合函數(shù)mysql>?select?max(age),min(age),avg(age),count(age),sum(age)?from?user;
分類統(tǒng)計(jì)mysql>?select?addr,?count(*)?from?user?group?by?addr;
mysql>?select?addr,age,?count(*)?from?user?group?by?addr,age;
4.Python代碼里操作mysql
首先需要安裝mysql的開發(fā)包? ?mysql-devel
其次pip安裝 mysqlclient
使用是導(dǎo)入包? MysqlSQLdb
Python操作mysql的七步
(1)導(dǎo)入模塊import?MySQLdb
(2)創(chuàng)建連接conn=MySQLdb.connect(host='127.0.0.1',port=3306,user='root',passwd='passwd',db='cmdb')
(3)獲取游標(biāo)cursor?=?conn.cursor()
(4)執(zhí)行sql(DQL 和 DML)
DQL
返回符合條件的個(gè)數(shù)cursor.execute("select?id,name?from?user?where?name='likuan'?and?password=md5('likuan');")
DMLcursor.execute("update?user?set?age?=?35?where?id?=?1")
(5)DQL獲取結(jié)果 、DML提交執(zhí)行
DQL(元組)cursor.fetchall()
cursor.fetchone()
>>>?cursor.fetchall()
(('kk',),?('likuan',),?('woniu',),?('zhangzhengguang',))
DML 提交conn.commit()
(6)關(guān)閉游標(biāo)cursor.close()
(7)關(guān)閉連接conn.close()
5.提交sql采用預(yù)處理的方式(預(yù)防sql注入)
(1)將操作和數(shù)據(jù)分開
(2)兩個(gè)變量,一個(gè)是sql操作,一個(gè)是對應(yīng)的數(shù)據(jù)
(3)只有數(shù)據(jù)才可以占位,操作不能占位
標(biāo)簽:addr,運(yùn)維,Python,age,Mysql,user,mysql,select,name
來源: https://blog.51cto.com/12217124/2359822
總結(jié)
以上是生活随笔為你收集整理的mysql 自动化运维开发_Python自动化运维开发----基础(十三)Mysql数据库基础的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用powershell代码安装Windo
- 下一篇: [论文]深度强化学习在超视距空战机动规划