当前位置:Gxlcms > 数据库问题 > mysql笔记

mysql笔记

时间:2021-07-01 10:21:17 帮助过:2人阅读

查看有多少个表

创建表

create table 表名(字段名 类型,字段名 类型);

descripition 表名 查看表的结构

show create table hreo \G

*************************** 1. row ***************************

       Table: hreo

Create Table: CREATE TABLE `hreo` (

  `id` int(20) DEFAULT NULL,

  `name` char(40) DEFAULT NULL,

  `leveal` int(2) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1

1 row in set (0.00 sec)


ENGINE=InnoDB DEFAULT CHARSET=latin1


CHARSET=latin1

默认向下兼容ASCLL码


Create Table: CREATE TABLE `hreo1` (

  `id` int(20) DEFAULT NULL,

  `name` char(40) DEFAULT NULL,

  `leveal` int(2) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=UTF8


CREATE TABLE 命令的TYPE子句是

可选的,同时innodb表类型

create table hreo(id int(20),name char(40),leveal int(2));

drop table hero ;

插入数据 

insert into hero values(1,‘mk‘,6); 这样就插入了一条记录

id 1 name 是hero 等级为6 mk 这里一定要加引号

insert into hero values(2,"dk",10),(3,"dh",98) ; 这样就插入了两条记录

insert into hero (id,name) values(1,‘mk‘); 这样就只给前两个字段插入了数据 

插入的值和字段,字段类型要一一对应

查询

select * from 表名

select * from hero ;

   

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

| id   | name | level |

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

|    1 | mk   |     6 |

|    2 | dk   |    10 |

|    3 | dh   |    98 |

|    3 | fm   |  NULL |

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

select * from hero \G

*************************** 1. row ***************************

   id: 1

 name: mk

level: 6

*************************** 2. row ***************************

   id: 2

 name: dk

level: 10

*************************** 3. row ***************************

   id: 3

 name: dh

level: 98

*************************** 4. row ***************************

   id: 3

 name: fm

level: NULL

4 rows in set (0.00 sec)

\G 以行的方式来查看

如果只查id字段

select id from hero ;


use mysql

select id ,name from bb.hero ;

use bbReading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A


alter table 表名 rename 新表名;


修改表字段的类型

     alter table hero modify id int(10);

     由int(20) 变为 int(10)

alter table hero change name heroname char(20);

 将name 字段改变为heroname 且定义为字符型20位长

 alter table 表名 change 原字段名 新字段名 字段类型;

 添加字段

 例:使用alter 字段添加字段team 

 alter table hero add team enum("B","D");

 在某个字段前面添加某个字段

 alter table hero add teamid int(2) first ;

 insert into hero (teamid,id,heroname) values(1,123,"demohunter");

删除定做

alter hero drop 

alter table hero drop teamidd

alter table 表名 drop 字段名


select * from hero

    -> ;

ERROR 1046 (3D000): No database selected

MariaDB [(none)]> select * from hero ;

ERROR 1046 (3D000): No database selected

MariaDB [(none)]> select * from bb.hero ;

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

| teamid | id   | heroname   | level | team |

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

|   NULL |    1 | mk         |     6 | NULL |

|   NULL |    2 | dk         |    10 | NULL |

|   NULL |    3 | dh         |    98 | NULL |

|   NULL |    3 | fm         |  NULL | NULL |

|      1 |  123 | demohunter |  NULL | NULL |

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

delete from hero where id =3;

就会把dk 和fm 删除

select * from hero ;

 select * from hero ;

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

| teamid | id   | heroname   | level | team |

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

|   NULL |    1 | mk         |     6 | NULL |

|   NULL |    2 | dk         |    10 | NULL |

|      1 |  123 | demohunter |  NULL | NULL |

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

3 rows in set (0.00 sec)


删除teamid为空值的行

delete from hero where teamid is null ;

select * from hero ;

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

| teamid | id   | heroname   | level | team |

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

|      1 |  123 | demohunter |  NULL | NULL |

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

空值不能写等号

更新一些值

update hero set teamid=3 ; 就会将表中的teamid改成3

update hero set teamid=1 where id=123 ;

会改id=123 的teamid改成1

where 条件

SQL查询语句:



select heroname,team from hero ;

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

| heroname   | team |

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

| demohunter | NULL |

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

1 row in set (0.00 sec)


这样就只显示两个字段。

使用insert into 命令添加一些字记录进来

elect * from hero ;

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

| teamid | id   | heroname    | level | team |

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

|      1 |  123 | demohunter  |  NULL | NULL |

|      2 |  124 | blademaster |    10 | B    |

|      3 |  125 | deadknight  |    10 | D    |

|      4 |  126 | faseer      |    10 | B    |

|      4 |  126 | faseer      |    10 | B    |

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


有一第重复的记录126 faseer 

去掉重复的记录

select distinct heroname,team from hero ;

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

| heroname    | team |

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

| demohunter  | NULL |

| blademaster | B    |

| deadknight  | D    |

| faseer      | B    |

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

4 rows in set (0.00 sec)

这样就会去掉重复的记录

这个只是

or和and 同时存在时,先算and两边值,逻辑与先执行

 select * from hero where teamid=3 ;

select teamid,heroname from hero where teamid=3 or id>100;

mysql 默认查询不区分大小写,如何区分大小写

insert into hero values(7,99,"FARSEER",10,"D");


select * from hero where heroname=‘farseer‘;

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

| teamid | id   | heroname | level | team |

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

|      7 |   99 | FARSEER  |    10 | D    |

|      7 |   99 | FARSEER  |    10 |      |

|      7 |   99 | FARSEER  |    10 |      |

|      7 |   99 | farseer  |    10 | D    |

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

当查找farseer时,大小写的就全部查询出来了,如果想严格区分大小写的话在字段前加binary即可

即:


 select * from hero where binary heroname=‘farseer‘;

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

| teamid | id   | heroname | level | team |

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

|      7 |   99 | farseer  |    10 | D    |

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


排序

select *  from hero order by id;

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

| teamid | id   | heroname    | level | team |

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

|      7 |   99 | FARSEER     |    10 | D    |

|      7 |   99 | FARSEER     |    10 |      |

|      7 |   99 | FARSEER     |    10 |      |

|      7 |   99 | farseer     |    10 | D    |

|      1 |  123 | demohunter  |  NULL | NULL |

|      2 |  124 | blademaster |    10 | B    |

|      3 |  125 | deadknight  |    10 | D    |

|      4 |  126 | faseer      |    10 | B    |

|      4 |  126 | faseer      |    10 | B    |

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

默认的排序做的是升序

如果想用降序的得用

select * from hero order by id desc ;

select * from hero order by id desc ;

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

| teamid | id   | heroname    | level | team |

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

|      4 |  126 | faseer      |    10 | B    |

|      4 |  126 | faseer      |    10 | B    |

|      3 |  125 | deadknight  |    10 | D    |

|      2 |  124 | blademaster |    10 | B    |

|      1 |  123 | demohunter  |  NULL | NULL |

|      7 |   99 | FARSEER     |    10 | D    |

|      7 |   99 | FARSEER     |    10 |      |

|      7 |   99 | FARSEER     |    10 |      |

|      7 |   99 | farseer     |    10 | D    |

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

9 rows in set (0.01 sec)


总结:

对库的操作

create,drop,show

对库里建好的表的数据 :

select 

alter

insert

delete

update


mysql笔记

标签:mysql

人气教程排行