一、數(shù)據(jù)庫(kù)基本操作
數(shù)據(jù)庫(kù)操作:
查看存儲(chǔ)引擎: show engines;
查看數(shù)據(jù)庫(kù): show databases; 或者show create database oldboy\G
創(chuàng)建數(shù)據(jù)庫(kù): create database oldboy default charset=utf8;
刪除數(shù)據(jù)庫(kù): drop database oldboy;
進(jìn)入數(shù)據(jù)庫(kù): use oldboy;
數(shù)據(jù)表操作:
創(chuàng)建數(shù)據(jù)表:
create table tb_emp1 (
-> id int(11),
-> name varchar(25),
-> deptId int(11),
-> salary float
-> )engine=innodb default charset=utf8;
primary key:
create table tb_emp2 (
-> id int(11) primary key,
-> name varchar(25),
-> deptId int(11),
-> salary float
-> )engine=innodb default charset=utf8;
多個(gè)primary key:
create table tb_emp3 ( id int(11), name varchar(25), deptId int(11), salary float,
primary key(name, deptId) )engine=innodb default charset=utf8;
外鍵約束foreign key:
主表:
create table tb_dept1 (
-> id int(11) primary key,
-> name varchar(22) not null,
-> location varchar(50)
-> )engine=innodb default charset=utf8;
從表:
create table tb_emp5 (
-> id int(11) primary key,
-> name varchar(25),
-> deptId int(11),
-> salary float,
-> constraint fk_emp_dept1 foreign key(deptId) references tb_dept1(id)
-> )engine=innodb default charset=utf8;
非空約束not null:
create table tb_emp6 (
-> id int(11) primary key,
-> name varchar(25) not null, #非空
-> deptId int(11),
-> salary float,
-> constraint fk_emp_dept2 foreign key(deptId) references tb_dept1(id)
-> )engine=innodb default charset=utf8;
唯一性約束unique,要求該列唯一,允許為空,但只能有一個(gè)值為空:
create table tb_dept2 (
-> id int(11) primary key,
-> name varchar(22) unique,
-> location varchar(50)
-> )engine=innodb default charset=utf8;
默認(rèn)值default:
create table tb_emp7 (
-> id int(11) primary key,
-> name varchar(25) not null,
-> deptId int(11) default 1111,
-> salary float,
-> constraint fk_emp_dept3 foreign key(deptId) references tb_dept1(id)
-> )engine=innodb default charset=utf8;
自增主鍵auto_increment:
create table tb_emp8 (
-> id int(11) primary key auto_increment,
-> name varchar(25) not null,
-> deptId int(11),
-> salary float,
-> constraint fk_emp_dept5 foreign key(deptId) references tb_dept1(id)
-> )engine=innodb default charset=utf8;
插入tb_emp8三條數(shù)據(jù):
insert into tb_emp8(name,salary) values('Lucy', 1000),('lura', 1200),('Kevin',1500);
id自增
查看表結(jié)構(gòu): desc tb_emp8; 或者show create table tb_emp8\G
修改數(shù)據(jù)表alter
修改表名: alter table tb_dept2 rename tb_deptment3;
修改字段類型: alter table tb_dept1 modify name varchar(30);
修改字段名: alter table tb_dept1 change location loc varchar(50);
添加字段: alter table tb_dept1 add managerId int(10);
添加有約束條件的字段: alter table tb_dept1 add column1 varchar(12) not null;
在某個(gè)位置添加字段: alter table tb_dept1 add column2 int(11) first;
在某個(gè)字段后面添加新字段: alter table tb_dept1 add column3 int(11) after name;
刪除字段: alter table tb_dept1 drop column2;
修改表的存儲(chǔ)引擎: alter table tb_deptment3 engine=MyISAM;
刪除外鍵約束: alter table tb_emp9 drop foreign key fk_emp_dept;
刪除數(shù)據(jù)表: drop table if exists tb_dept2;
刪除外鍵關(guān)聯(lián)的主表,需要首先取消外鍵關(guān)聯(lián),否則刪除主表失敗
數(shù)據(jù)類型
×××: TINYINT 1個(gè)字節(jié) 2**8 - 1=255個(gè)值
×××: smallint 2個(gè)字節(jié)
×××: int 4個(gè)字節(jié)
×××: bigint 8個(gè)字節(jié)
浮點(diǎn)數(shù)
單精度: float 4個(gè)字節(jié)
雙精度: double 8個(gè)字節(jié)
decimal: 不固定,一般用于財(cái)務(wù)系統(tǒng)
日期時(shí)間類型
year: 1個(gè)字節(jié),格式 YYYY
time: 3個(gè)字節(jié),格式 HH:MM:SS
date: 3個(gè)字節(jié),格式 YYYY-MM-DD
datetime: 8個(gè)字節(jié),格式 YYYY-MM-DD HH:MM:SS
timestamp: 4個(gè)字節(jié),格式Y(jié)YYY-MM-DD HH:MM:SS
字符串類型
char(n): 固定長(zhǎng)度字符串 #浪費(fèi)內(nèi)存,但查詢速度快
varchar(n): 非固定長(zhǎng)度字符串 #節(jié)省內(nèi)存,但查詢速度慢
text: 存放文本
longtext: 存放大數(shù)據(jù)文本
between .. and .. 關(guān)鍵字使用
select 4 between 4 and 6, 4 between 4 and 6, 12 between 9 and 10;
in, not in 關(guān)鍵字使用
select 2 in(1,3,5,'thks'), 'thks' in(1,3,5,'thks');
like用來(lái)匹配字符串
'%': 匹配任何數(shù)目的字符
'_': 只能匹配一個(gè)字符
select查詢數(shù)據(jù)
create table fruits (
-> f_id char(10) not null,
-> s_id int not null,
-> f_name char(255) not null,
-> f_price decimal(8,2) not null,
-> primary key(f_id)
-> ) engine -> ) engine=innodb =utf8;
插入字段:
insert into fruits(f_id,s_id,f_name,f_price) values
-> ('a1',101,'apple',5.2),
-> ('b1',102,'blackberry',10.2),
-> ('bs1',102,'orange',11.2),
-> ('bs2',105,'melon',8.2),
-> ('t1',102,'banana',10.3),
-> ('t2',102,'grape',5.3),
-> ('o2',103,'coconut',9.2),
-> ('c0',101,'cherry',3.2),
-> ('a2',103,'apricot',2.2),
-> ('l2',104,'lemon',6.4),
-> ('b2',104,'lemon',7.6),
-> ('m1',106,'mango',15.6),
-> ('m2',105,'xbabay',2.6),
-> ('t4',107,'xbababa',3.6),
-> ('m3',105,'xxtt',11.6),
-> ('b5',107,'xxxx',3.6);
單表查詢:
查詢表: select f_id,f_name from fruits;
查詢條件where:
select f_id,f_name from fruits where f_price=10.2; #等號(hào) =
select * from fruits where f_price < 10; #小于
select * from fruits where s_id in(101,102) order by f_name (desc); #in關(guān)鍵字,按f_name排序,desc降序,asc升序
select * from fruits where f_price between 2.00 and 10.20; #between and
select * from fruits where f_name like 'b%'; #like關(guān)鍵字匹配, %匹配任何多個(gè)字符
select * from fruits where f_name like '_____y'; #_匹配任意一個(gè)字符
select * from fruits where s_id='101' and f_price >=5; #and多條件匹配
select * from fruits where s_id='101' or s_id=102; #or多條件匹配
order by查詢結(jié)果排序
select * from fruits order by f_name;
select * from fruits order by f_price desc; #desc倒敘排列
group by分組
select s_id,count(*) as Total from fruits group by s_id; #根據(jù)s_id分組,s_id相同的數(shù)量
select s_id,count(*) as Total from fruits group by s_id having count(f_name) > 1; #having后面加上查詢條件
limit限制查詢的數(shù)量
select * from fruits limit 4; #查詢四條
select * from fruits limit 4,3; #索引為4,從第五條開(kāi)始返回3條
inner join 內(nèi)連接,返回兩表中都有的記錄
create table suppliers (
-> s_id int(11) not null auto_increment primary key,
-> s_name char(50) not null,
-> s_city char(50),
-> s_zip char(10),
-> s_call char(50) not null
-> )engine=innodb default charset=utf8;
以下操作是fruits和suppliers關(guān)聯(lián)
select suppliers.s_id, s_name, f_name, f_price from fruits inner join suppliers on
-> fruits.s_id = suppliers.s_id; #on后面是條件查詢,
left join 返回包括左表中的所有記錄和右表連接字段的所有記錄
select s_name,f_price from fruits left join suppliers on fruits.s_id = suppliers.s_id;
子查詢:
select s_id, f_name from fruits where s_id=(select s1.s_id from suppliers as s1 where s1.s_city='Tianjin');
union合并查詢結(jié)果并去重
union all合并查詢不去重
select s_id ,f_name,f_price from fruits where f_price < 9.0 union all select s_id,f_name,f_price from fruits where s_id in(101,103);
二、pyMySQL模塊操作數(shù)據(jù)庫(kù)
例子:
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import pymysql
conn = pymysql.Connect(host='127.0.0.1',user='root',password='aixocm',port=3306,database='oldboy',charset='utf8')
cursor = conn.cursor()
v = cursor.execute('select * from student')
print(v)
#cursor.fetchone() #獲取一條數(shù)據(jù)
#cursor.fetchmany(2) #獲取多條數(shù)據(jù)
result = cursor.fetchall() #獲取所有數(shù)據(jù)
print(result)
cursor.close()
conn.close()
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import pymysql
conn = pymysql.Connect(host='127.0.0.1',user='root',password='aixocm',port=3306,database='oldboy',charset='utf8')
cursor = conn.cursor()
#v = cursor.execute('insert into student(name) values("test")')
#print(v)
v = cursor.execute('delete from student where sid=7')
conn.commit() #提交事務(wù)
cursor.close()
conn.close()
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import pymysql
num=8
conn = pymysql.Connect(host='127.0.0.1',user='root',password='aixocm',port=3306,database='oldboy',charset='utf8')
cursor = conn.cursor()
#v = cursor.execute('insert into student(name) values("test")')
#print(v)
v = cursor.execute('delete from student where sid=%d' %(num)) #防止sql注入
conn.commit()
cursor.close()
conn.close()
當(dāng)前文章:python學(xué)習(xí)之?dāng)?shù)據(jù)庫(kù)操作
文章路徑:
http://weahome.cn/article/jsecde.html