标签:崩溃 com 表名 const parent pytho word 删除 std
MySQL
MySQL创建、删除数据库
1 create database alexdb;#创建数据库 2 3 drop database alexdb;#删除数据库 4 5 create database alexdb charset utf8;#创建支持中文的数据库 6 7 show create database alexdb;#查看数据库
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; 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;
删除增加字段
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 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`))
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
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
标签:崩溃 com 表名 const parent pytho word 删除 std
原文地址:http://www.cnblogs.com/x54256/p/7707209.html