mysql常用指令

一、基础指令

show databases;    --显示所有的数据库

show tabels;       --显示所有的数据库表

use databasename;  --切换数据库

desc tablename;    --显示表中所有的字段信息

create databases;  --建数据库

create table;      --建表

drop database dataset_name;  --删除数据库

drop table table_name;       --将整个数据表的所有信息全部删除,不可回滚

truncate table_name;         --删除数据行,但保留表结构,不可回滚

delete from table_name;      --清空一个表内容,但不清空表结构,可回滚

执行速度drop>truncate>delete

1.LIKE操作符

select * from websites where url like 'https%';  --url包含https的数据

select * from websites where name like 'G%';     --name以G开头

select * from websites where url like '%h%';     --url包含h的字段数据

select * from websites where name like '_o%';    --name中o在第二个字符的数据

2.ORDER BY关键字(对结果按照一列或多列进行排序)

select name,alexa from websites order by alexa sedc;  --默认升序,desc降序

select * from websites order by country,alexa;  --多列排序时按照顺序,先排country再排alexa

3.INSERT INTO(插入数据)

insert into websites valus (6,'百度','https://www.baidu.com/','22','ZG');  --可以省略指定列,但要所有的值都插入

insert into websites (id,name,alexa) values (7,'网易','www.wangyi.com','25','ZG');  --可以指定插入哪一列

4.UPDATE(更新数据)

update websites set url='http://www.wangyiyun.com' where id=7;

update websites set alexa=5000,country='USA' where id=3;

5.DISTINCT(过滤数据)

select distinct country from websites;

6.DELETE(删除数据)

delete from websites where id=7;

7.AND,OR,IN(筛选数据)

8.INSERT INTO SELECT(拷贝其他表的数据)

create tabke mytable select * from websites;                  --创建相同表结构

insert into mytable select * from websites;                   --复制整张表数据

insert into mytable(id,name) select id,name from websites;    --复制某列数据

9.LIMIT(过滤数据,记录行从0开始)

select * from websites limit 3;     --获取前三行数据

select * from websites limit 2,3;   --获取3,4,5行数据

10.BETWEEN AND

select * from websites where id between 1 and 3;    --获取1到3行数据

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

select * from websites where id < 4;

11.AS操作符(给列,表起别名)

select name as '名字' from websites;                                    --对列起别名
select name as '名字', concat(url,country) as '网站国家' from websites;  --合并列需要CONCAT
select M.ID,M.NAME from mytable as M;                                   --对表起别名

12.JION(连接两个表,分左连接和右连接)

select w.*,a.site_id from websites as w inner join access_log as a on w.id=a.site_id order by w.id;      --内连接

select w.* from websites as w left join access_log as a on w.id=a.site_id order by w.id;                 --左连接

select w.* from websites as w right join access_log as a on w.id=a.site_id order by a.site_id;           --右连接

select * from websites as w left join access_log as a on w.id=a.site_id
union
select * from websites as w right join access_log as a on w.id=a.site_id;   --全连接

参考文章https://www.cnblogs.com/wenhuohuo/p/13801169.html

13.UNION(合并两个或者多个select语句结果集)

14.建表的约束

NOT NULL           --指示某列不能存储NULL值

UNIQUE             --保证某列的每行必须有唯一值

PRIMARY KEY        --前两个约束的结合,每个表只能有一个primary key约束

CHECK              --保证列中的值符合指定的条件

DEFAULT            --规定默认值
create table infomation
(
ID INT NOT NULL AUTO_INCREMENT,
NAME VARCHAR(255) UNIQUE,
AGE INT,
CITY VARCHAR(255) DEFAULT 'CHINA',
PRIMARY KEY (ID),
CHECK (AGE>18)
);

15.ALTER TABLE(更新数据)

alter table information add unique(id);
或alter table information add constraint un_name unique(id,name);

alter table information add column score int;        --新增字段scope
alter table information modify score varchar(255);   --修改字段数据类型
alter table information drop column scope;           --删除字段

16.view(视图)

待学习,后续更新。

17.Date函数

NOW()               --返回当前的日期和时间  

CURDATE()           --返回当前的日期

CURTIME()           --返回当前的时间

DATE()              --提取日期或日期/时间表达式的日期部分

EXTRACT()           --返回日期/时间的单独部分

DATE_ADD()          --向日期添加指定的时间间隔

DATE_SUB()          --向日期减去指定的时间间隔

DATEDIFF()          --返回两个日期之间的天数

DATE_FORMAT()       --用不同的格式显示日期/时间
select now();
select curdate();
select curtime();
select date(now());
select extract(year from now());
select date_add(now(),interval 3 day);
select date_sub(now(),interval 3 day);
select datediff('2021-03-16','2021-03-13');  --左边日期减右边,有正负

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值