1、函數(shù)
字符串函數(shù)
length \ char length \ trim \ substring \ ascii \ concat \ upper \ repiace
數(shù)學函數(shù)
ceil \ floor \ round \ mod \ sin \ cos \ sqrt
日期函數(shù)
year \ month \ week \ curdate \ curtime \ date_format —— atr_to_date \ now
select date_format(curdate(),"%¥年%m月%d %H:%i:%s");
2、view視圖的使用
視圖也是一個數(shù)據(jù)庫對象,視圖是一張?zhí)摂M表
如何創(chuàng)建視圖:
create view vName as 查詢語句;
如:create view v_all_emp as select * from emp;
視圖的意義:簡化查詢、安全問題、
查詢視圖,和表一樣,通過select語句來查詢
select * from v_all_emp_dept;
select * from v_give_you;
不建議對視圖進行增刪改操作,因為約束條件未知
刪除視圖:
drop view vName;
索引 index
索引是用來加快查詢速度
創(chuàng)建索引:
1、在創(chuàng)建表的時候創(chuàng)建索引:
create table tName(
id int primary key auto_increment,
name varchar(255) not null,
age int not null,
index my_index(name(255))
)
2、alter 修改表,添加索引
alter table tName add index indexName (name(255));
3、create 關鍵字創(chuàng)建索引
create index IndexName on tName(fieldn(length));
查詢當前表的索引
show columns from emp;
顯示當前表中的索引
show index from emp;
為emp表添加索引
create index i_name on emp(name(50));
刪除索引
drop index i_name on emp;
(面試題)索引的分類:
1、普通索引
2、主鍵索引
3、唯一索引
4、組合索引
5、全文檢索索引
索引的原理:
BTREE:B+TREE B-TREE
hash:
選學BTREE的原理
3、index的使用
7、python調用MySQL
python3.2之前 MySQLdb模塊來完成調用python調用MySQL
python3.3之前 pymysql模塊來完成調用python調用MySQL
如何安裝第三方模塊
pip install moduleName
pip install pymysql
ubuntu下:
pip==pip2 apt install python-pip
pip3 apt install python3-pip
1、下載安裝pymysql
2、導入pymysql模塊
3、創(chuàng)建鏈接
4、創(chuàng)建游標
5、執(zhí)行execute
6、如果是查詢,需要使用fetchOne或者fetchMany獲取數(shù)據(jù)、
導入pymysql模塊
import pymysql
獲取連接
conn = pymysql.connect(
host="localhost",
db="數(shù)據(jù)庫名稱",
user="root",
password="root",
port=3306,
charset="uttf8")
獲取游標
cursor = conn.cursor()
sql = "select * from emp"
執(zhí)行sql
返回值是影響的行數(shù)
count = cuesor.excute(sql)
print(count)
users = cursor.fetchall()
print(users)