1、安裝mysql-server的命令:sudo apt-get install mysql-server 安裝mysql-client客戶端:sudo apt-get install mysql-client 查是否安裝成功並且啟用:sudo netstat -tap | grep mysql 關 ...
1、安裝mysql-server的命令:sudo apt-get install mysql-server
安裝mysql-client客戶端:sudo apt-get install mysql-client
查是否安裝成功並且啟用:sudo netstat -tap | grep mysql
關閉mysql伺服器命令:service mysql stop
開頭mysql伺服器命令:service mysql start
重啟mysql伺服器命令:service mysql restart
2、創建一個用戶並且授權:grant all privileges on *.* to test@"%" identified by "123456" with grant option;
登錄資料庫:mysql -utest -p123456
顯示所有資料庫:show databases;
選擇某一個資料庫: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
3、E-R模型:E-R模型是當前物理的資料庫都是按照E-R模型進行設計的,其中
E表示entity,實體;相當於python中的對象包含的信息。
R表示relationship,關係;如當百度王寶強時會有馬蓉的相關消息。它是一個實體轉換為資料庫的一個表。
對欄位約束方式:
主鍵primary key;不能重覆,唯一標識,物理存儲方式,速度快
非空not null;當保存為null時就會報錯;
唯一unique;這個值是唯一的,有重覆就會報錯。
預設default ;如果不寫就會有個預設值,這種叫預設,只有類型。
外鍵 foreign key
4、用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;
5、刪除表單:drop table 表單名;
6、查看表單中的所有數據:select * from students;
取別名:select name as 姓名,gender as 性別 fromstudents
7、查找表中id為1的所有欄位:select * from students where id=1;
8、查找表中id為1的id和name:select id,name from students where id=1;
9、從表中查找所有數據中的id和name的欄位數據: select id,name from students
10、insert into students(id,name,gender) values(3,'zhangsan','男'),(4,'lishi','男');
11、刪除表單中id為1的或者name為mysql的數據:delete from students where id=1 or name=mysql;
12、修改表單中id為2的,把name改為mysql的數據:update students set name='mysql' where id=2;
13、將表單中的記錄清空:delete from students;
14、備份:mysqldump -uroot -p test2 > ~/0000mysql.sql
還原:mysql -uroot -p test1 < 0000mysql.sql
15、消除重覆行,如查詢性別: select distinct gender from students;
16、查詢id大於3的女同學:select id,name from students where id>3 and gender =0;
17、查詢姓郭的學生:select * from students where name like '郭%'; %表示一個或者多個
18、查詢姓黃並且名字是一個字的學生:selcet * from students where name like '黃_'; _表示一個
19、查詢姓黃或者叫靖的學生:select name from students where name like '黃%' or name like '%靖%';
20、查詢id是1或3或8的學生:select * from students where id in(1,3,8);
21、查詢學生id是3至8的學生:select * from students where id between 3 and 8;
22、查詢沒有寫性別的學生:select * from students where gender is null;
23、查詢寫了性別的學生:select * from students where gender is not null;
24、查詢女生id的最大值:select max(id) from students where gender=0; 最小值用min ,和用sum,平均值用avg ,總個數用count
25、一個語句統計男生和女生總人數:select gender,count(id) from students group by gender;
26、查詢男生總人數:
使用where語句 :select gender,count(id) from students where gender=1;
使用分組後篩選:select gender,count(id) from students group by gender having gender =1;
27、查詢未刪除男生學生信息,按id降序:select id,name from students where gender=1 order by desc; (order by asc 升序)
28、分面查找:(n-1)*m 從第一頁開始查找 n表示頁數,m表示每頁顯示數據的條數
查詢學生表,每頁5條數據,頁數從1開始 :
第一頁:select * from students limit 0,5;
第二頁:select * from students limit 5,5; ......
29、創建分數表(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),當插入外鍵在主表單中不存在時,會報錯,這就是不合法數據.
31、查詢每個學生的每個科目的分數:
第一種寫法:
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;
32、左連接(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;
33、查詢男生的姓名、總分:
select students.name ,sum(scores.score)from scores
inner join students on scores.stuid=students.id where gender=1 group by students.name;
34、查詢未刪除科目的名稱、平均分:
select subjects.title,avg(scores.score)from scores
inner join subjects on scores.subid=subjects.id where isdelete=0 group by subjects.title;
35、使用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)