Python mysql sql基本操作
一、创建数据库,编码格式为utf-8
create database s12day9 charset utf8;
二、表操作
1、创建表
use s12day9;
create table students(
id int not null auto_increment primary key,
name char(20) not null,
sex char(4) not null,
age tinyint unsigned not null,
tel char(13) null default "_"
);
primary key:主键,每一行的唯一标识符。
tinyint:小整数
2、查看表结构
desc students;
3、查看建表语句:
show create table students;
ENGINE = InnoDB是指默认数据库引擎为innoDB,InnoDB支持事务操作,事务操作是指如果操作过程中断电了会进行回滚操作,意思是在执行操作过程中必须全成功,如果中间有一个操作时候因服务停止或其它原因失败了那么就会全部回滚回去需要重新操作才可以。
三、数据操作
1、增
insert into students(name,sex,age,tel) values("wohaoshuai","man",24,"110");
2、删
delete from students where id = 2;
delete from students where name = "wohaoshuai";
3、改
update students set name = "wohaoshuai2" where id = 2;
update students set age = 26 where name="wohaoshuai"
4、查
a、所有查询
select * from students;
b、条件查询
select * from students where age > 20;
c、多条件查询
select * from students where age>15 and sex="man";
d、模糊查询
select * from students where age like "1%"; #1后面的所有%是指所有的意思
e、部分字段查询
select name,sex from students where age like "%i1%";
四、alter操作
1、插入字段
alter table students add column nal char(64);