mysql-之增删改查

1.增

(1)创建数据库dks

?

1
create database  dks;

(2)创建名为t1的表,并指定引擎和字符集;

?

1
create table t1(id  int , name varchar (20)  not null ,ages  int ) engine=innodb  default charset=utf8;

(3)插入数据,字符类型需要使用双引号;

?

1
insert into t1(id, name ,ages) values (1, "zhangsan" ,28);

(4)插入多条数据

?

1
insert into t1(id, name ,ages) values (5, "xiaohong" ,58),(5, "xiaoming" ,68);

(5)在后面增加一列

?

1
alter table t1  add job  varchar (20);

(6)在id列后面增加一列city;

?

1
alter table t1  add city tinyint(2)  after id;

 

2.删

(1)删除数据库dks

?

1
drop database dks;

(2)删除表t1

?

1
drop  table t1;

(3)清空表内容

?

1
delete   from t1;

(4)删除job列

?

1
alter  table t1   drop column job;

(5)删除数据,where 条件筛选ages=18 数据,删除

?

1
delete from t1   where ages=18;

 

3.查

(1)查看所有数据库

?

1
show databases;

(2)进入dks数据库

?

1
use dks;

(3)查看数据库内有多少张表

?

1
show tables;

(4)查看t1表内数据内容

?

1
select from t1;

(5)只查看id这一列

?

1
select id  from t1;

(6)查看id、name两列

?

1
select id, name  from t1;

(7)查询ages大于20和name等于“zhangsan”的数据;

?

1
select from t1  where ages>20   and  name = "zhangsan" ;

(8)查询ages大于20和name等于“zhangsan”和id不等于1的数据;

?

1
select from t1  where ages>20  and name = "zhangsan" and id !=1;

(9)使用in参数指定多行

?

1
select from where id  in (2,3);

(10)使用not in参数排除多行

?

1
select from t1  where id  not in (2,3);

(11)模糊查询,使用通配符%查询;%相当于linux中的*号,统配所有;

?

1
2
select from t1  where name like "xiao%" ;
#查询所有与xiao有关的字符

(12)一个下划线可以统配一个字符,多个下划线可以匹配多个字符;

?

1
select from t1  where name like "xiao_" ;

(13)只查看前三行数据

?

1
select from t1 limit 3;

(14)查看第三行后面的两行数据

?

1
select from t1 limit 3,2;

(15)将数据从大到小排序

?

1
select from t1  order by ages  desc ;

(16)将数据从小到大进行排序

?

1
select from t1  order by ages  asc ;

(17)指定库、表、字段,进行t1的查询

?

1
select dks.t1. name from t1;

(18)查看数据库字符集

?

1
show variables  like '%char%' ;

(19)查看mysql数据库存储引擎

?

1
show engines;

(20)查看mysql默认存储引擎

?

1
show variables  like ‘%storage_engine%’;

 

4.改

(1)change和modify都可以修改表的定义,不同的时change后面需要写两次列名,不方便。changge的优点时可以修改列名称,modify则不能。

(2)修改列名

?

1
2
alter table t1 change age ages  int ;
#将age 改为 ages

(2)修改字段类型和长度

?

1
alter table t1  modify column ages  varchar (10);

(3)判断修改id=3的数据

?

1
2
update t1  set id = 2   where id=3;
#将id=3的数据改为id=2;

(4)修改name字段的内容

?

1
update t1  set name = 'zhangsan' where id=1;

(5)修改mysql 中 t1表的存储引擎

?

1
alter table t1 engine=innodb;


猜你喜欢

转载自blog.51cto.com/14354846/2408088
今日推荐