python學習——day12(MySQL常用命令,連接python)alex:http://www.cnblogs.com/wupeiqi/articles/5713330.html
阿新 • • 發佈:2017-10-22
www base drop cal esc username prim ber .com
MySQL
mysql 常用命令
MySQL創建、刪除數據庫
1 create database alexdb;#創建數據庫 2 3 drop database alexdb;#刪除數據庫 4 5 create database alexdb charset utf8;#創建支持中文的數據庫 6 7 show create database alexdb;#查看數據庫創建查看數據庫
MySQL 創建數據表、插入數據、查詢數據
1 create table student( #創建表 2 stu_id int not null auto_increment,創建數據表、插入數據、查詢數據3 name char(32) not null, 4 age int not null, 5 register_date date, 6 primary key(stu_id)); 7 8 insert into student (name,age,register_date) values ("xinge",21,"2017-10-21"); #增 9 10 select * from student; #查詢表格
where條件查詢、修改、刪除表中數據
1 select * from student;where條件查詢、修改、刪除表中數據2 3 select * from student where register_date like "2017-10%"; #把2017-10-xx 的顯示出來 4 5 update student set name="wupeiqi",age=33 where stu_id=4; #修改,stu_id>4 6 7 delete from student where name=‘x54256‘; #刪除所有name是x54256的數據
排序
1 select * from student order by stu_id; #排序升序 2 3 select * from student order by stu_id desc; #降序
分組
select name,count(*) from student group by name;
MySQL ALTER命令
- 刪除,添加或修改表字段
- 修改字段類型及名稱
- ALTER TABLE 對 Null 值和默認值的影響
- 修改表名
刪除增加字段
alter table student drop register_date; #從student表刪除register_date字段 alter table student add phone int(11) not null; #添加phone字段
修改類型
ALTER TABLE student MODIFY age CHAR(10);
ALTER TABLE 對 Null 值和默認值的影響
修改表名
ALTER TABLE student RENAME TO student2;
外鍵
1 CREATE TABLE Sign ( 2 id int auto_increment, 3 day_1 char(32) NOT NULL, 4 day_2 char(32) NOT NULL, 5 class_id int NOT NULL, 6 PRIMARY KEY (id), 7 KEY fk_class_key (class_id), 8 CONSTRAINT fk_class_key FOREIGN KEY (class_id) REFERENCES student2 (stu_id) 9 ) ;
1 此時如果class 表中不存在id 1,student表也插入不了,這就叫外鍵約束 2 3 mysql> insert into student2(id,name,class_id) values(1,‘alex‘, 1); 4 5 ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`student2`, CONSTRAINT `fk_class_key` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`)) 6 7 mysql> insert into class(id,name) values(1,"linux"); 8 9 Query OK, 1 row affected (0.01 sec) 10 11 mysql> insert into student2(id,name,class_id) values(1,‘alex‘, 1); 12 13 Query OK, 1 row affected (0.00 sec) 14 15 #如果有student表中跟這個class表有關聯的數據,你是不能刪除class表中與其關聯的紀錄的 16 17 mysql> delete from class where id =1; 18 19 ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`testdb`.`student2`, CONSTRAINT `fk_class_key` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`))
Mysql 連接
Inner join(內連接,等值連接)
select
*
from
a
INNER
JOIN
b
on
a.a = b.b;
select
a.*,b.*
from
a,b
where
a.a = b.b;
a | b
--+--
3 | 3
4 | 4
Left join(左連接)
select
*
from
a
LEFT
JOIN
b
on
a.a = b.b;
a | b
--+-----
1 |
null
2 |
null
3 | 3
4 | 4
Right join(右連接)
select
*
from
a
RIGHT
JOIN
b
on
a.a = b.b;
select
*
from
a
RIGHT
JOIN
b
on
a.a = b.b;
a | b
-----+----
3 | 3
4 | 4
null
| 5
null
| 6
事務
1、事務的原子性:一組事務,要麽成功;要麽撤回。
2、穩定性 : 有非法數據(外鍵約束之類),事務撤回。
3、隔離性:事務獨立運行。一個事務處理後的結果,影響了其他事務,那麽其他事務會撤回。事務的100%隔離,需要犧牲速度。
4、可靠性:軟、硬件崩潰後,InnoDB數據表驅動會利用日誌文件重構修改。可靠性和高速度不可兼得, innodb_flush_log_at_trx_commit選項 決定什麽時候吧事務保存到日誌裏。
1 mysql> begin; #開始一個事務 2 3 mysql> insert into a (a) values(555); 4 5 mysql> rollback; #回滾 , 這樣數據是不會寫入的 6 7 mysql> commit; #提交命令
索引
1 CREATE INDEX indexName ON mytable(username(length)); #最基本的索引
剩下的看這:http://www.cnblogs.com/alex3714/articles/5950372.html
python連接MySQL(pymysql連接python:http://www.cnblogs.com/wupeiqi/articles/5713330.html)
1 import pymysql 2 # 創建連接 3 conn = pymysql.connect(host=‘localhost‘, port=3306, user=‘root‘, passwd=‘abc123‘, db=‘xingedb‘) 4 # 創建遊標 5 cursor = conn.cursor() 6 7 data=[ 8 ("N1",22,"2017-10-21"), 9 ("N2",22,"2017-10-21"), 10 ("N3",22,"2017-10-21"), 11 ] 12 effect_row = cursor.executemany("insert into student (name,age,register_date) VALUES (%s,%s,%s)",data) # 執行SQL,並返回收影響行數 13 print(effect_row) 14 15 # print(cursor.fetchall()) 16 # print(cursor.fetchone()) 17 conn.commit() # 關閉遊標 18 cursor.close() # 關閉連接 19 conn.close()
python學習——day12(MySQL常用命令,連接python)alex:http://www.cnblogs.com/wupeiqi/articles/5713330.html