这些前端必备常见的 MySQL 语法你会了吗?

成为一名优秀的前端工程师,不仅需要了解还需要了解一些后台和一些基本的sql语句,本篇博文将记录一些mysql基本使用的语法,当然有什么错误的地方欢迎大家批评指正。

常用命令

列出当前数据库

show databases;

创建数据库

create database db;

删除数据库

drop database kitchendb;

使用当前数据库

use mysql;

列出当前数据库的表

show tables;

列出当前表的字段

desc db;

表的创建

# 如果存在表则删除
drop table if exists table;
# 创建表
create table `table`(
	# body......
)engine = InnoDB default  charset = utf8;
常用的字段的类型
属性描述
int整数
bigint长整数
float浮点数
double双精度浮点数
varchar(num)字符串(字符串长度)
timestamp时间

eg:创建案例

drop table if exists userdb;
create table `userdb`(
    `userID` int not null auto_increment,
    `userName` varchar(32) default '默认用户名',
    `userPhone` varchar(11) ,
    `timer` timestamp not null default current_timestamp,
    primary key (`userID`),
    foreign key (`foodID`) references fooddb (`foodID`)
)engine = InnoDB default  charset = utf8;

以上案例中 not null 代表 非空default 代表 默认值auto_increment 代表 自增长primary key 代表 主键foreign key 代表 外键

增删改查

增加数据

insert into table (字段1字段2) values ('值1','值2');

删除数据

delete from table;

更新数据

update table set 字段 = ‘值’;

查询数据

select * from table;

where 条件

无论是删除数据,更新数据还是查询数据都可以进行where条件

select * from table where tableID = 1;

limit 取指定数量的数据

select * from table limit 10; 显示多少条数据
select * from table limit 10,10; 第一个参数省略多少条,第二个参数显示多少条

inner join 联表查询

select * from table1 t1 inner join table2 t2 on t1.table1.id = t2.table2.id;

order dy 排序

select * from table order by tableid desc; # 降序
select * from table order by tableid asc; # 升序

count(*) 表中数据的集合数

select count(*) from table;

sum() 求和

select sum(tableid) from table;

avg() 平均数

select avg(tableid) from table;

rand() 随机数

select rand() as r from table;

好了,一些常见的mysql的语句,有了这些足够你在前端里去使用了,快去耍起来吧!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值