2021-06-12 mysql基础增删改查

创建表和数据库

create database 数据库名 ;//创建数据库
create table 表名(表结构);//创建数据表 

查看数据库,表和表结构

show databases ;//显示所有的数据库
show tables ;//显示所有的表
desc 表名;//显示表结构

修改表结构

添加列:
alter  table 表名  add  column  列名  数据类型 not null default ‘默认值’;
删除列:
alter table 表名 drop column 列名
修改列数据类型:
alter table 表名 modify 列名  bigint20not null
修改列名name 为 address:
alter table 表名  change name address varchar50default null
添加主键:
alter table 表名 add primary key (主键列名)
删除主键:
alter table 表名 drop primary key
添加普通索引:
alter table 表名 add index 索引名(列名)
添加唯一索引:
alter table 表名 add unique 索引名(列名)
删除索引:
alter table 表名 drop index 索引名;
修改表名:
rename table 原表名 to 新表名
移动表到另一个库:
rename db1.table1 to db2.table1
添加外键:
alter table add constraint 外键名 foreign key(外键列名)references 主表名(主表主键)

增加数据(插入数据)

create table 表名 (表结构);  //增加表
insert into 表名 values(插入的数据);//插入数据
insert into 表名 (要插入的列) values(插入的数据);

删除数据

drop table 表名;//删除表
delete * from 表名 ;//删除表内所有数据
delete * from 表名 where.....;//删除表内指定条件的数据

修改数据(更新数据)

update 表名 set 列名 = 修改后的数据 where ....;//更新某一列满足条件的数据

查找数据

select * from 表名; //查看表全部数据
select 列名 from 表名;//查看一列的数据
select 列名 from 表名 where ...;//查看某一列满足条件的数据

筛选条件

where 列名 = 列名信息;
where 列名 in (列名信息,列名信息);
where 列名 between 列名信息 and 列名信息;
where 列名 like '刘%';//查找这一列中性刘的人
where 列名 like '刘_';//查找这一列中姓刘且名字只有两个字的人 
where 列名 is null ; // 查找列为空
where 列名 = 列名信息 or 列名 = 列名信息;
where 列名 = 列名信息 and 列名 = 列名信息;
select distinct 列名 from 表名;//查询信息并按列名排序
select 列名 from 表名 limit 0,5;//查询0至第5行

查询结果排序

select 字段名1,字段2,....,字段名n from 表名 order by 字段名1[asc |  desc],
字段名2[asc |  desc],.....; //指定的字段1、字段2等是对查询结果排序的依据。
//asc代表升序进行排序,desc代表降序排序。默认情况按asc排序

查询的分组与汇总

1.聚集函数

avg()//返回列的平均值
count()//返回列的行数
max()//返回列的最大值
min()//返回列的最小值
sum()//返回列的值的和

2.分组查询

group by 列名 having 条件表达式 

为查的的表或列取别名

select * from 表名 [as] 别名;//为表取别名
select 列名 [as] 别名 from 表名;// 为列取别名
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值