一、安裝mysql-server的命令:sudo apt-get install mysql-serverpython
安裝mysql-client客戶端:sudo apt-get install mysql-clientmysql
查是否安裝成功而且啓用:sudo netstat -tap | grep mysqlsql
關閉mysql服務器命令:service mysql stop數據庫
開頭mysql服務器命令:service mysql start服務器
重啓mysql服務器命令:service mysql restartide
二、建立一個用戶而且受權:grant all privileges on *.* to test@"%" identified by "123456" with grant option;fetch
登陸數據庫:mysql -utest -p123456ui
顯示全部數據庫:show databases;spa
選擇某一個數據庫:use students:設計
查看當前數據庫的全部表單:show tables;
查看某個表的結構:desc students;
查看當前表(user)的全部數據:select * from user;
查看當前表(user)的user,host兩列的數據:select user,host from user;
刪除表中某個id=1的數據:delete from students where id=1;
查看當前使用的數據庫:select database();
建立數據庫:create database test charset=utf8;
刪除數據庫:drop database test;
修改用戶密碼:mysqladmin -test -p123456 password 110110
三、E-R模型:E-R模型是當前物理的數據庫都是按照E-R模型進行設計的,其中
E表示entity,實體;至關於python中的對象包含的信息。
R表示relationship,關係;如當百度王寶強時會有馬蓉的相關消息。它是一個實體轉換爲數據庫的一個表。
對字段約束方式:
主鍵primary key;不能重複,惟一標識,物理存儲方式,速度快
非空not null;當保存爲null時就會報錯;
惟一unique;這個值是惟一的,有重複就會報錯。
默認default ;若是不寫就會有個默認值,這種叫默認,只有類型。
外鍵 foreign key
四、用sql語句建立表: auto_increment 表示自增加; primary key:主鍵 not null:不能爲空
create table students(
id int(10)auto_increment primary key not null,
name varchar(40),
gender bite(1) default 0
)charset=utf8;
五、刪除表單:drop table 表單名;
六、查看錶單中的全部數據:select * from students;
取別名:select name as 姓名,gender as 性別 fromstudents
七、查找表中id爲1的全部字段:select * from students where id=1;
八、查找表中id爲1的id和name:select id,name from students where id=1;
九、從表中查找全部數據中的id和name的字段數據: select id,name from students
十、insert into students(id,name,gender) values(3,'zhangsan','男'),(4,'lishi','男');
十一、刪除表單中id爲1的或者name爲mysql的數據:delete from students where id=1 or name=mysql;
十二、修改表單中id爲2的,把name改成mysql的數據:update students set name='mysql' where id=2;
1三、將表單中的記錄清空:delete from students;
1四、備份:mysqldump -uroot -p test2 > ~/0000mysql.sql
還原:mysql -uroot -p test1 < 0000mysql.sql
1五、消除重複行,如查詢性別: select distinct gender from students;
1六、查詢id大於3的女同窗:select id,name from students where id>3 and gender =0;
1七、查詢姓郭的學生:select * from students where name like '郭%'; %表示一個或者多個
1八、查詢姓黃而且名字是一個字的學生:selcet * from students where name like '黃_'; _表示一個
1九、查詢姓黃或者叫靖的學生:select name from students where name like '黃%' or name like '%靖%';
20、查詢id是1或3或8的學生:select * from students where id in(1,3,8);
2一、查詢學生id是3至8的學生:select * from students where id between 3 and 8;
2二、查詢沒有寫性別的學生:select * from students where gender is null;
2三、查詢寫了性別的學生:select * from students where gender is not null;
2四、查詢女生id的最大值:select max(id) from students where gender=0; 最小值用min ,和用sum,平均值用avg ,總個數用count
2五、一個語句統計男生和女生總人數:select gender,count(id) from students group by gender;
2六、查詢男生總人數:
使用where語句 :select gender,count(id) from students where gender=1;
使用分組後篩選:select gender,count(id) from students group by gender having gender =1;
2七、查詢未刪除男生學生信息,按id降序:select id,name from students where gender=1 order by desc; (order by asc 升序)
2八、分面查找:(n-1)*m 從第一頁開始查找 n表示頁數,m表示每頁顯示數據的條數
查詢學生表,每頁5條數據,頁數從1開始 :
第一頁:select * from students limit 0,5;
第二頁:select * from students limit 5,5; ......
2九、建立分數表(scores)直接建立約束: foreign key(stuid) 表示外鍵 references表示與...相連
create table scores(
id int primary key auto_increment ,
score decimal(4,1),
stuid int,
subid int,
foreign key(stuid) references students(id),
foreign key(subid) references students(id)
)charset=utf8;
30、插入數據:insert into scores values(1,92,3,3),當插入外鍵在主表單中不存在時,會報錯,這就是不合法數據.
3一、查詢每一個學生的每一個科目的分數:
第一種寫法:
select students.name,subjects.title,scores.score from scores
inner join students on scores.stuid=students.id
inner join subjects on scores.subid=subjects.id;
第二種寫法:
select students.name,subjects.title,scores.score from students
inner join scores on scores.stuid=students.id
inner join subjects on scores.subid=subjects.id;
第三種寫法:
select students.name,subjects.title,scores.score from subjects
inner join scores on scores.stuid=subjects.id
inner join students on scores.stuid=students.id;
3二、左鏈接(left join):表示表A和表B進行匹配,會完整顯示出表A的數據,表B只顯示與表A對應的數據
右鏈接(right join )與此相反
select students.name,subjects.title,scores.score from subjects
left join scores on scores.stuid=subjects.id
left join students on scores.stuid=students.id;
3三、查詢男生的姓名、總分:
select students.name ,sum(scores.score)from scores
inner join students on scores.stuid=students.id where gender=1 group by students.name;
3四、查詢未刪除科目的名稱、平均分:
select subjects.title,avg(scores.score)from scores
inner join subjects on scores.subid=subjects.id where isdelete=0 group by subjects.title;
3五、使用python代碼實現把mysql數據庫封裝成MysqlTool模塊,該模塊中MySqlHelper類:
import pymysql # 建立MysqlHelper類 class MysqlHelper(object): # 建立初始化參數 def __init__(self,host,user,password,db): self.host=host self.prot=3306 self.user=user self.password=password self.db=db self.charset='utf8' # 打開數據庫連接返回connect對象-conn def open(self): self.conn=pymysql.connect( host=self.host, port=self.prot, user=self.user, password='1122', db='python2', charset='utf8' ) # 獲得cursor對象 self.cursor=self.conn.cursor() # 關閉連接 def close(self): self.cursor.close() self.conn.close() # 修改提交數據 def change(self, sql, parms=[]): try: self.open() self.cursor.execute(sql,parms) self.conn.commit() self.close() except Exception as result: print(result) def get_all(self,sql,prams=[]): try: self.open() self.cursor.execute(sql, prams) result = self.cursor.fetchall() self.close() for i in result: print(i) except Exception as result: print(result)