MySQL数据库的基本操作

下面的内容是根据观看传智播客PHP培训视频整理的:

mysql的入门语句

1.连接服务器

mysql –h localhost –u UserName –pPassWd

-h 如果不写,则默认连localhost

2.  命令以;或g结束

3.  查看当前服务器下面有哪些库   show databases;

4.  创建一个数据库  create database DBName;

5.   删除数据库   drop database DBName;

6.   不能修改数据库的名字

7.   选择数据库   use DBName;

8.   查看一个库的所有表  show tables;

9.   删除一张表  drop table tableName;

10.   修改表名  rename table oldName to NewName;

11.   创建表

Create table 表名(

列名称 列类型[列属性] [默认值],

……..

)Engine 引擎名Charset 字符集

eg.

Create table php13(

id primary key auto_increment,

name char(3) not null default 0,

age tinyint unsigned not null default 0,

email varchar(30) not null default ‘’,

tel char(11) not null default ‘’,

salary decimal(7,2) not null default ‘1800.68’,

riqi date not null default ‘2012-03-13’

)charset utf8;

eg.

create table class (

stu int,

name varchar(20),

age int,

area varchar(20)

);

12.   “C”:退出该语句

13.   desc 表名;------查看表结构

14.  解决字符集问题

默认建表一般用utf8,而我们在windows下窗口是GBK的。

set names gbk;

15.  增删改查语句

(1)增加 

往哪张表增,增哪几列,各为什么值?

Insert into 表名

(列1,列2….列N)

Values

(值1,值2….值N);

*****如果不声明插入的列,则默认插入所有列

eg.insert into msg

(id,title,name,content)

values

(1, ’初来乍到’, ’张三’, ’刚来能不能当老大?’);

(2)更新--update

修改哪张表,修改哪几列,修改成什么值

Update 表名

Set

列1 = 值1,

列2 = 值2,

….

列N = 值N,

Where 表达式;

update msg

set

id = 2,

content = ‘偏要当老大’

where

name = ‘李四’;

(3)查

select * from msg;

select id,title from msg;

select * from msg where id >2;

select name,content

from msg

where id >2;

(4)删除--delete

删除哪张表的数据,删哪些行?

delete from表名

Where 表达式;

eg.delete from msg where  id = 2;

16.  整型列的字节与存储范围

(1)建表:就是声明列的过程。

数据是以文件的形式放在硬盘(也有放在内存里的)。

列:不同的列类型占的空间不一样。选列的原则:够用,又不浪费。

详解列类型

数值型

整型(默认是有符号数)

Tinyint    1字节(8个位)  0~(2^8-1 )  -2^7~2^7-1

Smallint   2字节

Mediumint 3字节

int       4字节

Bigint     8字节

小数型

字符型

日期/时间型

(2)create table class(

id int primary key auto_increment,

name varchar(10),

age tinyint

charset utf8;

insert into class

(name,age)

values

(‘Zhangsan’, 25);

17.  整型列的可选属性     tinyint(M)  unsigned   zerofill

unsigned:是无符号,影响存储范围

M:代表宽度(在zerofill时才有意义)

zerofill:零填充(如果某列zerofill,默认是无符号

(1)alter table class add age2 tinyint unsigned; ----表 class 增加列age2

(2)Insert into class

(name,age ,age2)

Values

(‘lisi’,25,-1);

(3)列可以声明默认值,而且推荐声明默认值

Select null is null;

Alter table class add age5 tinyint not null default 0;

18.  小数型 (浮点型,定点型)

Float(M,D)-------此处定义的都是定点型,即小数点是固定的

M:精度(总位数,不包括点)

D:小数位,以(6,2)为例:-9999.99~9999.99

Create table goods(

Name varchar(10) not null default ‘ ’,

Price float (6,2) not null default 0.00

) charset utf8;

Sql Server 和 Oracle不能定义浮点数的小数位数。

Decimal(M,D) -----更精确

也能zerofill。

Float(4,2)unsigned    0.00~99.99

M<=23,4个字节,>23时,8个字节

补充: FLOATDOUBLE在不指定精度时,默认会按照实际的精度来显示,而DECIMAL在不指定精度 时,默认整数为10,小数为0

19.  字符串类型

Create table stu(

Name char(8) not null default ‘ ’,

Waihao varchar(10) not null default ‘ ’

) charset utf8;

char定长  char(M)---M代表宽度,即可容纳的字符数   M:0~255   如果不够M个字符,内部用空格补齐

varchar变长 varchar(M)---M代表宽度,即可容纳的字节数  M:0~65535(utf8:22000左右)  

区别:

定长实占M个字符,如果存的小于M个字符,右侧补空格,取出时,去除右侧空格。速度上定长一些。

变长:有1-2个字节来标记真实的长度

Char与varchar型的选择原则:

1)空间利用效率

2)速度

eg:四字成语表:char(4)

text:文本类型,可以存比较大的文本段,搜索速度稍慢。因此,如果不是特别大的内容,建议用char,varchar来代替。Text不用加默认值(加了也没用)

20.  日期时间列类型

year

年-月-日date

09:00:00time

年-月-日 hh:mm:ssdatetime

year类型:1个字节,表示1901-2155,【0000,表示错误时选择】

如果输入2位,“00-69”表示2000-2069;“70-99”表示1970-1999年。

Date类型:典型格式  1992-08-12

日期类型:1000-01-01  到 9999-12-31

时间类型:典型格式  hh:mm:ss      -838:59:59到838:59:59

日期时间类型:典型格式 1989-05-06 14:32:08

Datetime:  范围1000-01-01 00:00:00到9999-12-31 23:59:59

注意:

在开发中,很少用日期时间类型来表示一个需要的精确到秒的列。原因:虽然日期时间类型能精确到秒,而且方便查看。用时间戳来表示。时间戳(int存储):是1970-01-01 00:00:00到当前的秒数

21.  查询的五种子句   where, group by, having, order by, limit

(1) where 条件查询

where 表达式

表达式在哪一行成立,哪一行就取出来

where后常用运算符:<, <=, =, !=或<>, >=, >

in:在某集合内  in(值1,值2,…..值N)(等于值1->N任意一个都行)

between:在某范围内 

where后可跟的逻辑运算符

!或NOT, ||或OR, AND或&&

Eg.  not in (4,5)

模糊查询:想查找“诺基亚”开头的所有商品

select goods_id, goods_name from goods where goods_name like ‘诺基亚%’;

%--通配任意字符

‘_’--通配单个字符

(2)  group by----分组,常用于统计场合,一般和统计函数配合使用

统计函数:max、min、sum、avg、count

select max(shop_price) from goods;-----此句话能查到商品最贵的吗?

select goods_id, goods_name, max(shop_price) from goods;

#查询每个栏目下面最贵的商品价格

select cat_id, max(shop_price) from goods group by cat_id;

select count(*) from goods;----总行数

#要把列名当成变量名看

#请查询出本店每个商品比市场价格低多少钱?

select goods_id, goods_name, market_price - shop_price from goods;

#查询每个栏目下面积压的货款

select cat_id, sum(shop_price * goods_number) from goods group by cat_id;

#可以给列或计算结果取别名,as-------表也可以取别名

select cat_id, sum(shop_price * goods_number) as hk from goods group by cat_id;

(3)   having

#查询出本店价格比市场价低多少钱,并且把低200元以上的商品选出来

select goods_id, goods_name, market_price - shop_price as sheng from goods having sheng > 200;

#数据在表中,表在硬盘或内存以文件形式存在。where对表文件起作用

#查询出的结果,也可以看成一张表,其文件一般临时存在于缓冲区。having就是针对查询的结果发挥作用。

#同上题,只不过查第3个栏目下比市场价低200元以上的商品。

select goods_id, cat_id, market_price - shop_price as sheng from goods where cat_id = 3 having sheng > 200;

count(score < 60):数整个表中的行数,“score < 60”没意义

select name, score<60 from stu;

(4)   order by---排序,默认是升序排列

显式声明升序排列,可用asc声明

order by可以按多字段排序,order by 列1[desc/asc],列2[desc/asc],....

select goods_id, cat_id, goods_name, shop_price from goods where cat_id = 3 order by shop_price desc;---降序排序

(5)   limit---在语句的最后,起到限制条目的作用

limit[offset,]N

offset:偏移量

N:取出的条目数

如果不写offset,相当于limit 0,N

select goods_id,goods_name,shop_price from goods order by shop_price desc limit 3,3;

eg.以取第3条-第5条: limit 2,3

5个字句是有顺序要求的:where,group,having,order by,limit

select * from (select goods_id,cat_id,goods_name,shop_price from goods order by cat_id asc, shop_price desc) as tmp group by cat_id;

22.   where子查询

定义:指把内层查询的结果作为外层查询的比较条件

典型题:查询最大商品,最贵商品

如果where 列=(内层sql),则内层sql返回的必是单行单列(单个值)

如果where 列 in(内层sql),则内层sql只返回单列,可以多行

eg1.select goods_id,goods_name from goods where goods_id = (select max(goods_id) from goods);

eg2.select goods_id,goods_name from goods where goods_id in (select max(goods_id) from goods order by cat_id);

23.   from型子查询

定义:把内层的查询结果当成临时表,加“as 临时表名”,供外层sql再次查询

#查出每个栏目下最新/最贵的商品

select * from (select goods_id,cat_id,goods_name from goods order by cat_id asc,goods_id desc) as temp group by cat_id;

#用子查询查出挂科两门及两门以上同学的平均分,where型,from型都行

select name,avg(score) from stu where name in (select name from (select name,count(*) as gk from stu where score < 60 group by name having gk >= 2) as temp) group by name;

24.   exists型子查询

定义:把外层的查询结果,拿到内层,看内层的查询是否成立

#查有商品的栏目

select cat_id,cat_name from category where exists(select * from goods where goods.cat_id = category.cat_id);

25.   union:联合

作用:把2次或多次查询结果合并起来

要求:两次查询的列数一样

推荐:查询的每一列,相对应的列类型也一样

可以来自于多张表

多次sql语句取出的列名可以不一致,此时,以第一个sql的列名为准。

如果不同的语句中取出的行,有完全相同(每个列的值都相同),那么相同的行将会合并(去重复)

如果不去重复,可以加all来指定。

如果子句中有order by,limit,需加(),推荐放到所有子句之后,即对最终合并后的结果来排序。

在子句中,order by配合limit使用才有意义,如果order by不配合limit使用,会被语法分析器优化分析时去除。

eg. 

(select goods_id,cat_id,goods_name,shop_price from goods where cat_id = 3 order by shop_price desc limit 3) union (select goods_id,cat_id,goods_name,shop_price from goods where cat_id = 4 order by shop_price desc limit 2);

26.   连接查询与左连接

左连接:left

select 列1,列2,列N from

tableA left join tableB

On tableA.列 = tableB   [此处表连接成一张大表,完全当成普通表看]

where,group,having...照常写

右连接:right

select 列1,列2,列N from

tableA right join tableB

On tableA.列 = tableB   [此处表连接成一张大表,完全当成普通表看]

where,group,having...照常写

内连接:inner

select 列1,列2,列N from

tableA inner join tableB

On tableA.列 = tableB   [此处表连接成一张大表,完全当成普通表看]

where,group,having...照常写

左右内连接的区别:

左连接:----左连接兼容性好一点

以左表为准,去右表找匹配数据,找不到匹配,用NULL补齐。

A left join B =====> B right join A

内连接:查询左右表都有的数据,即不要左/右中的NULL的那一部分。

内连接是左右连接的交集。

能否查出左右连接的并集?

答:目前不能,目前的mysql不支持外连接(outer join),但是可以用union来达到目的。

27.   表管理之列的增删改

列名称 列类型 [列属性][默认值]---->列声明

增加列

Alter table 表名 add 列声明

增加的列默认是在表的最后一列。

可以用after来声明新增的列在哪一些后面

Alter table 表名 add 列声明 after 字段名;

如果新增的列放在最前面,怎么做?

Alter table 表名 add 列声明 first;

修改列

Alter table 表名 change 被改变的列名 列声明

28.   视图--view

视图是由查询结果形成的一张虚拟表。

视图的创建:create view 视图名 as select语句;

视图的作用:

1)可以简化查询

2)进行权限控制

  将表的权限封闭,但是开放相应的视图权限,视图里只开放部分数据。

3)大数据分表时可以用到。

视图的修改:Alter view as select语句

视图与表的关系

1)视图是表的查询结果,自然表的结果改变了,影响视图的结果。

2)如果视图改变了,视图增删改也会影响表,但是视图并不是总是能增删改的,只有视图的数据与表的数据一一对应时,视图才可以进行增删改。

对应视图insert还应注意:视图必须包含表中没有默认值的列。

视图的algorithm:

algorithm = merge/temptable/undefined

merge:当引用视图时,引用视图的语句与定义视图的语句合并。

temptable:当引用视图时,根据视图的创建语句建立一个临时表

undefined:未定义,自动,让系统帮你选

merge,意味着视图只是一个规则,语句规则,当查询视图时,把查询视图的语句(比如where那些)与创建时的语句where子句等合并,分析形成一条select语句.

例:创建视图的语句

create view g2 as select goods_id,cat_id,goods_name,shop_price from goods order by cat_id asc,shop_price desc;

查询视图的语句

select * from g2 group by cat_id;

这两句相当于:

select goods_id,cat_id,goods_name,shop_price from goods group by cat_id order by cat_id asc,shop_price desc;

而temptable是根据创建语句瞬间创建一张临时表,然后查询视图的语句从该临时表查数据

create view g2 algorithm = temptable as select goods_id,cat_id,goods_name,shop_price from goods order by cat_id asc,shop_price desc;

select * from g2 group by cat_id;

最终执行2句话,取数据并放在临时表,然后去查临时表。

 

29.数据库是.frm,.myd,myi备份如何导入mysql  

http://blog.sina.com.cn/s/blog_8028ba2f0100rzpt.html

MySQL数据库文件放在哪里  

http://ccncc.blog.163.com/blog/static/208904920104254511716/

MySQL数据库.frm文件、.MYD文件和.MYI文件的修复方法

http://database.51cto.com/art/201108/281396.htm

  

原文地址:https://www.cnblogs.com/-lee-/p/3679204.html