谁能阻止少年武士赴死,他们听不到,斗士的剑一挥出,必会听到战败者的哀嚎。

整理的mysql笔记

上一篇 / 下一篇  2013-03-11 10:58:45 / 个人分类:技术

在家HO,也没有浪费掉学习的时间了,把整理的笔记放这里,也算有个备份吧

 

更改root用户密码

Set password for’root’@’localhost’=password(‘momo’)

删除一个不确定存在的数据库

Drop database if exists emp;

删除一个不确定存在的数据表

Drop table if exists emp;

Mysql数据库端口号:3306

Oracle数据库端口号:1521

显示数据库版本:

Select version();

显示时间:

Select now();

显示年月日:

Select dayofmonth(current_date)显示日

Select month(current_date);显示月

Select year(current_date);显示年

输出汉字

Select “welecome to beijing”

做计算器用:

Select 4*4+5

建数据表

Create table Myclass(

Id int(4) not null primary key auto_increment,

Name char(20) not null,

Sex int(4) not null default ‘0’,

Degree double(16,2));(小数点后两位,总长度是16

显示表结构:

Desc myclass;

Show columns from myclass;

如果keynull,那么该列的值可以重复,该列是一个非唯一索引的前导列(第一列)或者是一个唯一性索引的组成部分但是可以包含有空值null

删除数据表

Drop table myclass;

某表增加字段

Alter table myclass add grade int;

Mysql默认int位数为11位。

删除表的一个字段

Alter table myclass drop grade;

表中插入中文:

Insert into myclass values(‘李明’);

模糊查询:

Select * from myclass where name like ‘%d%’

多条件查询

Select * from myclass where name like ‘%d%’ and sex=1;

数据库中插入数据id自动排序号:

Create table myclass (id int not null primary key auto_increment);

Insert into myclass(name,sex,degree) values (‘kk’,1,90.90);

排序查询

 Select * from myclass order by degree;

分页查询:

Select * from myclass limit 1,2;从第一条开始查询2条记录

更新指定记录

Update myclass set sex=1 where id=3;

删除指定记录(不指定删除条件则会删除所有记录)

Delete from myclass where id=8;

查询总成绩

Select sum(degree) from myclass;

求最大数

Select max(degree) from myclass;

求平均数

Select avg(degree)….

统计一个表有多少记录(求和)

Select count(*) from myclass;

分组查询(按照性别分组)

Select sex,sum(degree) from myclass group by sex;

左连接和右连接区别:

整理如下:

左连接时显示的是left左边的表。右连接显示的是右边的表。

Left join是以表1的记录为基础,即左表的记录全部显示。而表2只显示符合搜索条件的记录显示出来。

mysql> select * from myclass a

   -> left join mygrade b

   -> on a.id=b.id;

+----+------+-----+--------+------+--------+

| id | name | sex | degree | id  | degree |

+----+------+-----+--------+------+--------+

| 1 | tom |  0 | 90.34 |   1 | 90.34 |

| 2 | dd  |  0 | 90.23 |   2 | 45.34 |

| 3 | ffdd |  0 |  0.85 |   3 | 56.89 |

| 5 | mm  |  1 | 90.34 | NULL |  NULL |

| 6 |李明|  0 | 56.78 | NULL |  NULL |

| 8 | hjkk |  1 | 90.90 | NULL |  NULL |

+----+------+-----+--------+------+--------+

6 rows in set (0.04 sec)

 

mysql> select * from myclass a

   -> right join mygrade b

   -> on a.id=b.id;

+------+------+------+--------+----+--------+

| id  | name | sex | degree | id | degree |

+------+------+------+--------+----+--------+

|   1 | tom |   0 | 90.34 | 1 | 90.34 |

|   2 | dd  |   0 | 90.23 | 2 | 45.34 |

|   3 | ffdd |   0 |  0.85 | 3 | 56.89 |

| NULL | NULL | NULL |  NULL | 4 | 90.56 |

+------+------+------+--------+----+--------+

4 rows in set (0.00 sec)

 

Inner join 内部连接:只显示符合条件的数据

Select * from myclass a inner join mygrade b

On a.id=b.id;

mysql> select * from myclass a inner join mygrade b

   -> on a.id=b.id;

+----+------+-----+--------+----+--------+

| id | name | sex | degree | id | degree |

+----+------+-----+--------+----+--------+

| 1 | tom |  0 | 90.34 | 1 | 90.34 |

| 2 | dd  |  0 | 90.23 | 2 | 45.34 |

| 3 | ffdd |  0 |  0.85 | 3 | 56.89 |

+----+------+-----+--------+----+--------+

3 rows in set (0.08 sec)

别名查询:

Select a.id,a.degree from myclass a,mygrade b where a.id=b.id;

表连接查询:

Select a.id,a.name from myclass a join mygrade b on a.id=b.id;

子查询:

Select * from myclass where id in (select id from mygrade where grade=’90.34’);

 


TAG:

 

评分:0

我来说两句

Open Toolbar