aruba mysql_mysql基础命令2

FDML: 数据操作语

insert(插入数据)

特殊字符要加引号,不然数据全为0 如时间:0000-00-00 00:00:00

#查看表结构

desc student;

#插入数据(多列)(少一列数指定都会报错)

insert into student values(1,'ss',33,'f','3333-4-5');

###插入数据(多列)(指定列)

insert student(name,age) values('zeng','84');

#插入多条数据(指定列)(不指定的话默认或null)

insert student(name,age) values('zeng','84'),('a','4'),('b','8');

#查看

select * from student;

#into可以省略

#前面指定的话,后面一定要加

#非空不自增的一定要指定

#sql_mode与表的严格程度有关

[root@db03 ~]# vim /etc/my.cnf

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

update命令

使用update语句,一定要加where

#查看数据

mysql> select * from student;

#修改数据(小心指定的值不是唯一 非空)

update student set age=18 where name='ss' and cometime='';

#修改指定数据(有主键就指定主键)

update student set age=18 where id=2;

#查看

mysql> select count(*) from student1;

mysql> select * from student;

delete 删除数据,不会删除表

#查看数据

select count(*) from student;

select * from student;

#删除数据(使用delete语句也一定要加where条件),最好不要批量删除表,要一个一个删除

delete from student where id=1;

#删除表(通过bin-log可以找回来)(可以通过where指定某一行)(不会删除索引)

delete from student (where 1=1);

#删除表(找不回来)(不会触发任何触发器)(不会删除表)

truncate (table) student;

#删除表(找不回来)

drop table student;

使用update代替delete

老玩家回归

1.添加状态字段status

alter table student add status enum('1','0') default 1;

2.使用update代替delete

#删除数据

delete from student where id=1;

#修改数据

update student set status='0' where id=1;

#查看表

select * from student;

desc student;

#status的值要加引号

status='0'

DQL: 数据查询语言

select

1.查询表中所有的数据

#查看数据总行数

select count(*) from student;

#该语句只适合数据量小的表,因为这些数据放到了内存查看

select * from student;

2.查看指定列的数据

查看表结构

desc student;

#查看指定列

select user,host from mysql.user;

3.按条件查询#where

select name,gender from student where id='1';

#select查看表的时候一定要先看一下数据的数量,防止沾满内存

#id='1',无论该字段在不在要查看的字段内,都可以where

#字段不用加引号,值要加引号

#SQL语句,数据库名 表名 字段名 数据名 是否支持大小写,要看字符集的校验规则

#select user,host select后面是要查看要显示的列

查询练习

#上传SQL文件

[root@db03 ~]# rz -E

rz waiting to receive.

[root@db03 ~]# ll

total 396

-rw-r--r-- 1 root root 397334 Jul 15 10:21 world.sql

#查看slq文件是否有drop

[root@db03 ~]# grep -ri drop world.sql

DROP SCHEMA IF EXISTS world;

DROP TABLE IF EXISTS `city`;

DROP TABLE IF EXISTS `country`;

DROP TABLE IF EXISTS `countrylanguage`;

#删除drop语句

#导入

方法1

[root@db03 ~]# mysql -uroot -p123 < world.sql

方法2(有过程)(SQL语句)

mysql> source /root/world.sql

方法3

mysql> \. /root/word.sql

#连接

[root@db03 ~]# mysql -uroot -p123

mysql> show databases;

mysql> use world

mysql> show tables;

#查看表结构(表头)

mysql> desc city;

#查看表行数

select count(*) from city;

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

| count(*) |

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

| 4079 |

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

#查看表

select * from city;

| ID | Name| CountryCode | District| Population |

select Name,Population from city;

#查看数据排序(order by)

升序

mysql> select Name,Population from city order by Population;

降序

mysql> select Name,Population from city order by Population desc;

#查看前十条数据(limit N)

mysql> select population from city order by population limit 10;

mysql> select Name,Population from city order by Population desc limit 10;

#按照步长查询数据,起始,步长(n+1--N)(#翻页)

select Name,Population from city order by xx desc limit 10,50;

select id,Name,Population from city order by xx desc limit 10,50;

#命令行查看(脚本使用)

mysql -uroot -p123 -e 'use world;select id,name from city limit 0,100'

mysql -uroot -p123 -e 'select * from world.city'

条件查询

#条件查询就是使用where语句

where可以使用的符号:(字段和数值之间的符号)

#精确匹配

=

#范围匹配(int tinyint)

!=

>

<

>=

<=

#条件语句

or

and

#模糊匹配,会导致数据的查询非常缓慢

like

#2.查询中国的城市人口

mysql> select name,population from city where CountryCode='CHN';

#3.查询黑龙江人口数量

mysql> select name,population from city where countrycode='CHN' and District='heilongjiang';

#4.查询中国人口数量小于100000的城市

mysql> select name,population from city where countrycode='CHN' and population < 100000;

#5.模糊匹配(值:like %)(like速度慢)

#匹配以N结尾的数据

mysql> select name,countrycode from city where countrycode like '%N';

#匹配以N开头的数据

mysql> select name,countrycode from city where countrycode like 'N%';

#匹配包含N的数据

mysql> select name,countrycode from city where countrycode like '%N%';

#6.查询中国或美国的人口数量

#使用 ...or...

mysql> select name,population from city where countrycode = 'CHN' or countrycode = 'USA';

#使用in(...)

mysql> select name,population from city where countrycode in ('CHN','USA');

#使用(select ...union all ...select ...)(查询速度最快)

mysql> select name,population from city where countrycode = 'CHN' union all select name,population from city where countrycode = 'USA';

select高级用法

#多表联查,联表查询

多表联查的意义就在于把不同表的列组合成一个表,显示出来,关键在于'联'(where),

1.传统连接,#联表,字段可以不同,指定列值必须相同

2.自连接,#自己查找相同字段,使用自连接两个表必须有相同字段和相同的值

3.内连接

4.外连接

传统连接

#建表

mysql> create table students(id int,name varchar(10));

mysql> create table score(id int,mark int);

#插入数据

mysql> insert into students values(1,'qiudao'),(2,'qiandao'),(3,'zengdao');

mysql> insert into score values(1,80),(2,90),(3,100);

#数据查询

mysql> select * from students;

mysql> select * from score;

------------------------------------ 2表联查

#查看邱导的分数

mysql> select students.name,score.mark from students,score where students.id=score.id and name='qiudao';

#查询所有学生成绩

mysql> select students.name,score.mark from students,score where students.id=score.id

练习题1

连2表查询:世界上小于100人的城市在哪个国家?请列出城市名字,国家名字与人口数量

#1.确认我要查哪些内容

国家名字 城市名字 城市人口数量 小于100人

#2.确认在哪个表

country.name city.name city.population

#3.找出两个表相关联的字段

city.countrycode country.code

#4.编写语句

desc xx;

mysql> select country.name,city.name,city.population from country,city where city.countrycode=country.code and city.population < 100;

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

| name | name | population |

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

| Pitcairn | Adamstown | 42 |

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

练习题2

连3表查询:世界上小于100人的城市在哪个国家,是用什么语言?请列出城市名字,国家名字与人口数量和国家语言

#1.确认我要查哪些内容

国家名字 城市名字 城市人口数量 国家使用的语言 小于100人

#2.确认在哪个表

country.name city.name city.population countrylanguage.language

#3.找出三个表相关联的字段

country.code city.countrycode countrylanguage.countrycode

#4.写sql语句(and两两分开联)

desc xx;

mysql> select country.name,city.name,city.population,countrylanguage.language from country,city,countrylanguage where country.code=city.countrycode and city.countrycode=countrylanguage.countrycode and city.population < 100;

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

| name | name | population | language |

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

| Pitcairn | Adamstown | 42 | Pitcairnese |

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

#多联表查询

1.要看哪些表,哪些列

2.找出关联字段

select 表1.列,表2.列,表3.列... from 表1,表2,表3... where 表1.列a=表2.列b and 表2.列b=表3.列c... and 表xx.列xx < 100;

#city.population 或者population 可以指定表,也可以不指定(默认是当前表)

自连接(natural join)

自连接会自己查找相同字段

适用于2表联查

#两个关联的表必须有相同字段和相同数据

mysql> select city.name,city.countrycode,countrylanguage.language,city.population from city natural join countrylanguage where population < 100 order by population;

#两个表中 指定的字段和数据列 必须相同 (缺一不可)

mysql> select country.name,city.name,city.population from city natural join country where population < 100;

这两个表中虽然都有Name列,但是数值不一样,所以#SQL语句错误,但是不报错

#查看列

mysql> desc city;

| Name

mysql> desc country;

| Name

#查看值

select * from city limit 10;

select * from city limit 10;

#注意:

1.自连接必须有相同字段和相同值(指定列相同)

2.小表命中大表(selec count(*))

3.NATURAL JOIN 自连接

4.可以自连接就可以传统连接

mysql> select 表1.列1,表2.列2... from 表1 natural join 表2 where 列x < 100;

内连接

关联数据相同即可

适用于2表联查,3表联查

#注意:命中率(驱动的概念),小表在join前

表1 小表

表2 大表

#2表联查

#小于100人的城市在哪个国家,国家代码是什么?

select city.name,city.population,city.countrycode,country.name

from city join country on city.countrycode=country.code

where city.population < 100;

#3表联查

#世界上小于100人的城市在哪个国家?是用什么语言?

select country.name,city.name,city.population,countrylanguage.language

from city join country on city.countrycode=country.code

join countrylanguage on country.code=countrylanguage.countrycode

where city.population < 100;

select 表1.列1,表2.列2... from 表1 join 表2 on 表1.a=表2.b... where 表x.列x < 100;

外连接

外连接基于内连接,使用了......left (right) join ...... on...

外连接可以做脱敏

and不能替换为where

#左外连接

select city.name,city.countrycode,country.name

from city left join country on city.countrycode=country.code and city.population < 100;

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

| name | countrycode | name |

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

| Kabul | AFG | NULL |

#右外连接

select city.name,city.countrycode,country.name,city.population

from city right join country on city.countrycode=country.code and city.population < 100;

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

| name | countrycode | name |

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

| NULL | NULL | Aruba |

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值