mysql基础语法

连接服务器:

Mysql -h host -u user -p //连接服务器

创建数据库:

show databases;    //显示当前服务器上有什么服务器

use DataBaseName;        //选择数据库

create database DataBaseName //创建数据库

创建表:

show tables    //显示当前服务器库中的表

create table tablename (name varchar(20),owner varchar(20),species varchar(20),

sex char(1),birth date,death date);        //创建表

describe tablename;        //显示数据表创建方式

将数据装入表中:

load data local infile '/path/pet.txt' into table pet;    //将文本文件pet.txt装载到pet表中

load data local infile '/path/pet.txt' into table pet 

lines terminated by '\r\n';     //windows使用\r\n作为行的结束符

insert into pet VALUES ('Puffball','Diane','hamster','f','1999-03-30',NULL);//插入一条新记录

select 语句从表中检索信息。语句一般格式为 select what_to_select from which_table where conditions_to_satisfy;

select //查询语句

select version()     //查询版本号

select current_date;    //查询系统当前日期

select now()    //查询系统当前日期与时间

select * from pet;    //显示表中所有记录

select * from pet where name='bowser';    //选择特定行,只选择name='bowser'的行

select * from pet where birth>'1998-1-1';    //查找日期大于1998年的记录

select * from pet where species ='dog' and sex='f';    //查找组合条件

select * from pet where species ='dog' or sex='f';    //查找组合条件

select * from pet where (species='cat' and sex='m') or (species='dog' and sex='f');    //查找组合条件and的优先级大于or

select name,birth from pet;    //显示特定列

select distinct owner from pet;    //显示唯一的输出记录请使用distinct关键字

select name,birth from pet order by birth;    //显示name和birth列并根据birth分组

select name,birth from pet order by birth desc;    //显示name和birth列并根据birth分组降序排列

select name,birth,curdate(),(year(curdate())-year(birth))-(right(curdate(),5)<right(birth,5))as age from pet;    //显示name,birth,curdate(),age,列计算当前日期与birth日期的差并保存到age显示出来

select name,birth,death,(year(death)-year(birth))-(right(death,5)<right(birth,5)) as age from pet where death IS NOT NULL ORDER BY age;    //查询death不为NULL的

SELECT * FROM pet WHERE name LIKE 'b%';    //查询b开头的任意名字

SELECT * FROM pet WHERE name LIKE '_____';    //查询五个字符的任意名字

SELECT * FROM pet WHERE name REGEXP '^b';    //查询以B开头的名字

SELECT * FROM pet WHERE name REGEXP BINARY '^b';    //区分大小写查询B开头的名字

SELECT * FROM pet WHERE name REGEXP 'fy$';    //查询以FY结尾的名字

SELECT * FROM pet WHERE name REGEXP 'w';    //查询名字中包含W的名字

SELECT * FROM pet WHERE name REGEXP '^.....$';    //查询正好包含5个字符的名字

SELECT * FROM pet WHERE name REGEXP '^.{5}$';    //查询正好包含5个字符的名字

select count(*) from pet;    //查询pet中有多少行

select owner,count(*) from pet group by owner;    //查询对owner分组统计个数

使用1个以上的表:

select p1.name,p1.sex,p2.name,p2.sex,p1.species from pet as p1,pet as p2 WHERE p1.species = p2.species AND p1.sex = 'f' AND p2.sex = 'm';

select database();    //显示当前选择了那个库

更新数据:

update pet set birth = '1989-01-02' where name = 'bowser';     //更改一条数据

删除表:

delete from pet;     //删除表

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值