MySQL数据库基本操作

数据库操作
1.查询所有数据库: show databases;
2.创建数据库: create database 数据库名;
3.删除数据库: drop database 数据库名;

表操作
要操作表,首先要进入库 use库名
进入成功显示:
代表进入成功
1.查看所有的表: show tables;
2.查看表信息: desc 表名;
3.查看建表语句: show create table 表名;
4.创建表
以一个创建dome表为栗子:

CREATE TABLE `dome` (
  `id` tinyint(4) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `username` varchar(30) NOT NULL COMMENT '用户名',
  `age` tinyint(1) NOT NULL COMMENT '年龄',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

5.删除表: drop table 表名;
温馨提示:该操作只能删除无关联表,若有外键必须先删除外键,再解除自增,才能删除。此操作存在风险未成年人建议在监护人看护下进行。

6.修改表
6-1.修改表名: alter table 旧表名 rename to 新表名;
6-2.修改表中字段名: alter table 表名 change 旧字段名 新字段名 数据类型;
6-3.修改表中的数据类型: alter table 表名 modify 字段名 新数据类型;
6-4.修改字段排列位置:
将字段1放在字段2后面:

alter table 表名 modify 字段1 数据类型 after 字段2;

将任意字段放在第一位:

alter table 表名 modify 字段 数据类型 first;

6-5.添加字段:

alter table 表名 add 新字段名 数据类型;

添加一个字段并放在指定位置后面:

alter table 表名 add 新字段名 数据类型 aftet 指定字段;

添加一个字段并放在指定位置前面:

alter table 表名 add 新字段名 数据类型 first 指定字段;

7.删除字段:

alter table 表名 drop 字段名;

表内容操作
1.插入数据: insert into 表名(字段名..) values (值1...),(值2...) ;
2.更新数据: update 表名 set 字段名='新数据' where 字段名='旧数据';
2.删除:
2-1.删除所有数据:delete from 表名;
2-2.删除一行数据: delete from 表名 where 字段名=值;
3.查询所有数据: select * from 表名;

查询扩展
一、单表查询
1.查询所有内容:select * from 表名;
2.查询指定字段: select 字段名 from 表名;
3.查询指定记录: select * from 表名 where 字段名=值;
4.带关键字in的范围查询: select * from 表名 where 字段名 in(值1,值2...);
5.带between and 的范围查询:

select * from 表名 where 字段名 between 值1 and 值2;
select * from 表名 where 字段名 not between 值1 and 值2;

6.带关键字like的模糊查询:

select * from 表名 where 字段名 like'A%';
select * from 表名 where 字段名 like'A%Z';
select * from 表名 where 字段名 like'_B%';  (每一个_都代表一个字母)

7.带关键字find_in_set的模糊查询:
举个栗子:
一篇文章属于多个栏目,表中category字段以 “1,5,8” 的形式存储,
要查询栏目id为5的文章就可以使用find_in_set:

select * from `article` where find_in_set('5',`category`);

8.空值查询:

select * from where 字段名 is null;
select * from where 字段名 is not null;

9.带and的多条件查询:

select * from 表名 where 字段名='值1' and 字段名='值2'	
select * from 表名 where 字段名>'值1' and 字段名<'值2'	

10.带or的多条件查询:

select * from 表名 where 字段名='值1' or 字段名='值2';
select * from 表名 where 字段名>'值1' or 字段名<'值2';

11.关键字distinct的不重复结果查询:select distinct 字段名 from 表名;
12.排序查询:
默认升序:select * from 表名 order by 字段名 asc;
降序:select * from 表名 order by 字段名 desc;
13.分组查询:select * from 表名 group by 字段名;
14.使用limit限制查询结果数量:

select * from 表名 limit 1
select * from 表名 limit 1,3

15.子查询:就是把一句sql当做另一句sql的条件,子查询的关键字主要包括 in、not in、=、!=、exists、not exists 等。
eg:select * from article where uid in(select uid from user where status=1);

二、集合函数查询
1.计数:count()
2.求和:sum()
3.平均数:avg()
4.最大值: max() ;最小值min()
5.group_concat() ,now()
6.取出年份:year(字段);同理date() ,day()

三、多表查询
量表连接
1.普通量表连接:select * from 表A,表B where 表A.字段n=表B.字段n;
2.内/外连接:select * from 表A left join 表B on 表A.字段n=表B.字段n;
3.连接两张字段数量相同的表:union

#扩展
1.按照in后面的顺序排序。
eg:

SELECT * FROM `tx_user_questionnaire` WHERE  `id` IN (5,4,3,2) ORDER BY instr(',5,4,3,2,',CONCAT(',',id,','))

在这里插入图片描述
2.替换语句

UPDATE `user` SET `username` = replace (`username`,'张三','法外狂徒张三') WHERE `id` > 0

解释:把user表里username是‘张三’的替换成“法外狂徒张三”,判断条件是id大于1


sql语句执行顺序:
from
on
join
where
group by
avg(),sum()…
having
select
distinct
order by

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值