mysql入门

一、基本操作

#mysql -h localhost -u root -p;//链接mysql

#mysql -u root -p password 123456;//修改root用户密码为123456

 >show databases;//查看数据存在

>create database db1;//创建数据库db1

>use db1;//进入数据库db1

>drop database db1;//删除数据库db1

>show tables;//查看表存在

>exit;//退出mysql

>create table person(id int(4),name char(10),age int);//创建表person

>create table p(id int,name char(10))charset=gbk;//创建支持插入中文(gbk/utf8)记录的表p

>create table p(id int auto_increment primary key,name char(10) not null,age int default 100);//带列完整性约束创建

>desc person;//查看表结构描述

>insert into person(id,name,age) values(1,'aaa',22);//向表person插入记录

>insert into person set id=1,name='aaa',age=22;//另一种插入方式

>select * from person;//查询所有记录

>update person set age=20 where name='aaa';//更新数据记录

*********where条件**********

1.where后跟一个逻辑表达式;

2.逻辑表达式内部可使用比较符号,如=,<,>,<=,>=,<>等,还有between...and,in等运算符;

3.逻辑表达式之间可以用and,or操作;

***************************

>delete from person where id=1;//删除记录

>truncate person;//清空表

>drop table person;//删除表person

********修饰符*****************

1.主键:在写完所有字段后,加一个primary key(col1,col2,....)

2.自增:auto_increment

3.非空:not null

4.唯一:unique

5.默认值:default value

*****************************

二、查询

简单查询

select *是效率最差的查询方式。

语法格式:select 列1,列2... from 表名 其他条件

>select name,age from person where id=1;

>select name as 姓名,age as 年龄 from person;//指定别名

统计信息:count(列名),sum(列名),max(列名),min(列名),avg(列名)

>select now();//当前时间

>select 8*7*9;//计算器

>select distinct id,age from person;//去重

>select id,name from person where id between 3 and 7;//查询id在3和7之间的记录

>select name from person where id not in(3,4,5);//查询id不为3,4,5的记录

>select name from person where age is not null;//查询年龄不为空的记录

not可以和between,in,is搭配,意味取反。

>select name from person where name is like '%A_';//查询名字倒数第二个字符为A的记录

>select name from person where name regexp '大';//正则表达式查询含大的记录

>select count(name) from person where group by age;//按年龄分组统计人数

>select count(name) from person where group by age having count(name)>1;//过滤掉人数为1的记录

>select count(name) from person where group by age having age!=40;//年龄40的不参与分组

  where having
顺序 在group之前 在group之后
可用字段 所有 group,select,外查询
习惯 常用 与group by连用

 

>select * from person limit 0,3;//取前三条记录

>select * from person order by name asc,age desc;//查询结果按姓名升序年龄降序排序

>select * from p1 union select * from p2;//联合,去重

>select * from p1 union all select * from p2;//联合,不去重

子查询

将一个查询语句嵌套到另一个查询、插入、修改、删除语句中去,这个查询语句就是一个子查询。

实例:

>create table p1(id int,name char(10),age int)charset=utf8;

>create table p2(id int,name char(10),age int)charset=utf8;

>select id,name,age from p1 where id>=(select max(id) from p2);//where型子查询(单一标量)

>select id,name,age from p1 where id in (select id from p2);//列子查询

>select * from (select * from p1 where id<=floor(ran()*5)) as p3 where name regexp '大';//from型子查询

>select * from p1 where exists(select * from p2 where p1.age=p2.age);//exists型子查询

连接查询

连接查询即多表查询。

外连接:左连接、右连接、全连接(mysql不支持全连接)

>select * from p1 left join p2 on p1.id=p2.id;//左连接

>select * from p1 right join p2 on p1.id=p2.id;//右连接

交叉连接:表间进行笛卡尔积

>select * from p1,p2;

内连接:内连接每条记录的连接条件都是相同的

>select * from p1 inner join p2 on p1.id=p2.id;//内连接1

>select * from p1 inner join p2 using(id);//内连接2

自然连接:

>select * from p1 natural join p2;

 

posted @ 2014-10-30 17:23  feilv  阅读(205)  评论(0编辑  收藏  举报