Mysql 常用命令 -- 附实例

 

mysql -uroot -p

--提示输入密码

 

select version() as '版本号', current_date,now() as '当前系统时间';  --查看系统版本和系统时间

 

select 4*8-4;  --作为一个计算器使用

 

select 1-5,

45-9;            --它收集输入行但直到看见分号才执行,分号作为结束语句

 

select user();   --当前数据库

 

show databases;   --服务器上当前存在什么数据库

 

use test;    --使用test数据库

 

create database mydb;  --创建自己的数据库

 

use mydb;   --使用我自己的数据库

 

show tables;  --显示数据库下的表

 

--删除表

drop table pet;

 

--创建宠物表

create table pet(

    name varchar(20) not null,

    birthday date not null,

    sex char(2) not null,

    type char(16),

    owner varchar(20)

);

 

--再次查看数据库下的表

show tables;

--查看表结构

describe pet;

 

--初始化数据

insert into pet values ('爱丽斯','2000-12-12 12:12:12','m','dog','A');

insert into pet values ('cena','2000-12-11 12:11:12','f','cat','B');

insert into pet values ('cena','2000-12-10 12:10:12','m','cat','A');

 

 

--查看数据

select * from pet;

select * from pet where name = '爱丽斯';   --根据姓名查询

select * from pet where name like '%爱%';  --模糊查询

select birthday as '生日' from pet where name = '爱丽斯';  --查看列

SELECT * FROM pet WHERE birth > '1998-1-1';   --操作时间

select * from pet where birthday > '2000-12-11';

 

--查询雌性的dog或cat

select * from pet where sex = 'f' and type = 'dog' or type = 'cat' ;

 

--查看所有宠物主人(distinct去除重复列)

select distinct owner from pet;

 

--按生日升序

select name ,birthday from pet order by birthday,name desc;

 

--分组查询宠物主人拥有的宠物个数

select owner,count(owner) from pet group by owner;

 

--每个宠物种类的个数

select type,count(type) from pet group by type;

 

--每种性别、种类的宠物个数

select sex,type,count(*) from pet group by sex,type;

 

--统计名狗有主的个数

select count(*) from pet where owner is not null;

 

--查看流浪狗

select count(*) from pet where owner is null;

 

 

SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL;

 

--查询name包含w的

SELECT * FROM pet WHERE name LIKE '%W%';

 

--子查询生日最小的(保证括号里的结果只有一条)

select * from pet where birthday = (select min(birthday) from pet);

 

--和oracle类似,没有像sql server那样的top语句,使用limit控制结果集个数

select * from pet where birthday = '2000-12-12' limit 2;

 

--查询name有三个字的

 

SELECT * FROM pet WHERE name LIKE '___';

 

--当前使用的数据库

select database();

 

--查看表存在的索引

show index from pet;

 

 

 

你可能感兴趣的:(mysql,常用命令,sql语句)