MySQL-基础操作之增删改查

1.增

(1)创建数据库dks

create database  dks;

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

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

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

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

(4)插入多条数据

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

(5)在后面增加一列

alter table t1 add job varchar(20);

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

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

2.删

(1)删除数据库dks

drop database dks;

(2)删除表t1

drop  table t1;

(3)清空表内容

delete   from t1;

(4)删除job列

alter  table t1  drop column job;

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

delete from t1  where ages=18;

3.查

(1)查看所有数据库

show databases;

(2)进入dks数据库

use dks;

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

show tables;

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

select * from t1;

(5)只查看id这一列

select id from t1;

(6)查看id、name两列

select id,name  from t1;

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

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

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

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

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

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

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

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

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

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

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

select * from t1 where name like "xiao_";

(13)只查看前三行数据

select * from t1 limit 3;

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

select * from t1 limit 3,2;

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

select * from t1 order by ages desc;

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

select * from t1 order by ages asc;

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

select dks.t1.name from t1;

(18)查看数据库字符集

show variables like '%char%';

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

show engines;

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

show variables like%storage_engine%’;

4.改

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

(2)修改列名

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

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

alter table t1 modify column ages varchar(10);

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

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

(4)修改name字段的内容

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

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

alter table t1 engine=innodb;
原文地址:https://www.cnblogs.com/IMSCZ/p/10965024.html