成为一名优秀的前端工程师,不仅需要了解还需要了解一些后台和一些基本的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
的语句,有了这些足够你在前端里去使用了,快去耍起来吧!!!