一、數據庫基本操作
數據庫操作:
查看存儲引擎: show engines;
查看數據庫: show databases; 或者show create database oldboy\G
創建數據庫: create database oldboy default charset=utf8;
刪除數據庫: drop database oldboy;
進入數據庫: use oldboy;
數據表操作:
創建數據表:
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;
多個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,要求該列唯一,允許為空,但只能有一個值為空:
create table tb_dept2 (
-> id int(11) primary key,
-> name varchar(22) unique,
-> location varchar(50)
-> )engine=innodb default charset=utf8;
默認值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三條數據:
insert into tb_emp8(name,salary) values('Lucy', 1000),('lura', 1200),('Kevin',1500);
id自增
查看表結構: desc tb_emp8; 或者show create table tb_emp8\G
修改數據表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;
在某個位置添加字段: alter table tb_dept1 add column2 int(11) first;
在某個字段后面添加新字段: alter table tb_dept1 add column3 int(11) after name;
刪除字段: alter table tb_dept1 drop column2;
修改表的存儲引擎: alter table tb_deptment3 engine=MyISAM;
刪除外鍵約束: alter table tb_emp9 drop foreign key fk_emp_dept;
刪除數據表: drop table if exists tb_dept2;
刪除外鍵關聯的主表,需要首先取消外鍵關聯,否則刪除主表失敗
數據類型
×××: TINYINT 1個字節 2**8 - 1=255個值
×××: smallint 2個字節
×××: int 4個字節
×××: bigint 8個字節
浮點數
單精度: float 4個字節
雙精度: double 8個字節
decimal: 不固定,一般用于財務系統
日期時間類型
year: 1個字節,格式 YYYY
time: 3個字節,格式 HH:MM:SS
date: 3個字節,格式 YYYY-MM-DD
datetime: 8個字節,格式 YYYY-MM-DD HH:MM:SS
timestamp: 4個字節,格式YYYY-MM-DD HH:MM:SS
字符串類型
char(n): 固定長度字符串 #浪費內存,但查詢速度快
varchar(n): 非固定長度字符串 #節省內存,但查詢速度慢
text: 存放文本
longtext: 存放大數據文本
between .. and .. 關鍵字使用
select 4 between 4 and 6, 4 between 4 and 6, 12 between 9 and 10;
in, not in 關鍵字使用
select 2 in(1,3,5,'thks'), 'thks' in(1,3,5,'thks');
like用來匹配字符串
'%': 匹配任何數目的字符
'_': 只能匹配一個字符
select查詢數據
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; #等號 =
select * from fruits where f_price < 10; #小于
select * from fruits where s_id in(101,102) order by f_name (desc); #in關鍵字,按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關鍵字匹配, %匹配任何多個字符
select * from fruits where f_name like '_____y'; #_匹配任意一個字符
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查詢結果排序
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; #根據s_id分組,s_id相同的數量
select s_id,count(*) as Total from fruits group by s_id having count(f_name) > 1; #having后面加上查詢條件
limit限制查詢的數量
select * from fruits limit 4; #查詢四條
select * from fruits limit 4,3; #索引為4,從第五條開始返回3條
inner join 內連接,返回兩表中都有的記錄
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關聯
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合并查詢結果并去重
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模塊操作數據庫 例子: #!/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() #獲取一條數據 #cursor.fetchmany(2) #獲取多條數據 result = cursor.fetchall() #獲取所有數據 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() #提交事務 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()
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。