Mysql中常用语句,以book数据库为例

Posted qq69496

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql中常用语句,以book数据库为例相关的知识,希望对你有一定的参考价值。

查看所有数据库
show databases;
创建数据库
create database book;
删除数据库
drop database book;
查看所有表
show tables;
创建表
create table books(
id int primary key auto_increment,
title varchar(100) not null,
author varchar(50),
price decimal(5,2));
删除表
drop table books;
查看表中字段的属性
desc books;
添加表中字段
alter table books add press varchar(50) comment "出版社";
删除表中字段
alter table books drop column press;
修改表中字段
alter table books change id id int;
添加主键
alter table books add primary key(id,author);
删除所有主键
alter table books drop primary key;		//自增主键删除会报错
添加外键
alter table books add foreign key(author) references author(name);
通查看创建表的代码查看外键
show create table books;
删除外键
alter table books drop foreign key 外键名;
添加数据
insert into books(id,title,author,price,press) values(1001,'Java从入门到放弃','路小狗','9.9','剑桥大学');
删除表中所有数据
delete from books;
查询表中所有数据
select * from books;
带有where条件的查询
select * from books where id=1001 and title='Java从入门到放弃';
带有where条件的更新
update books set id=1002,title='斗罗大陆',author='唐家三少',price=89.99,press=' ' where id = 1001;
带有where条件的删除
delete from books where id = 1002;
查询记录的条数
select count(*) rows from books;
时间差函数
select timestampdiff(day,'2018-03-01 00:00:00', '2021-04-27 16:00:00') 自定义字段名;
select timestampdiff(hour,'2021-04-26 00:00:00', '2021-04-27 16:00:00') 自定义字段名;
select timestampdiff(minute,'2021-04-26 00:00:00', '2021-04-27 16:00:00') 自定义字段名;
select timestampdiff(second,'2021-04-26 00:00:00', '2021-04-27 16:00:00') 自定义字段名;

以上是关于Mysql中常用语句,以book数据库为例的主要内容,如果未能解决你的问题,请参考以下文章

MYSQL列表中常用语句代码块

MySQL中常用语句2

SQL语句优化 -- 以Mysql为例

php 数据访问(以mysql数据库为例)

SQL语句建表

SQL语句建表