数据库基本命令
一:Mysql是C/S架构,也就是客户端和服务端。
Server : mysqld.exe.
Client: mysql.exe.
客户端想要连接服务器,必须要提供ip,端口,用户名,密码
mysql –h127.0.0.1 –p3306(一般端口号是3306),-uroot, xxxx(密码)
mysql安装目录/data,这个目录就是所有的数据库目录。
查看当前服务器中有那些数据库:
show databases;
创建数据库
cteate database company(company是数据库名,当前创建的为company)
切换工作数据库
use company
查看数据库中的表
show tables;
导入数据(重点,之前没有了解过)
source d:/company.sql
查看表中数据
select * from employee;
__________________________________________________________________________________________________________________________________
---------------------------------------------------------------------------------------------------------------------------------------
mysql架构
mysqld服务器
数据库1(目录)
表1
记录1
记录2
记录3
表2
表3
数据库2(目录)
---------------------------------------------------------------------------------------------------------------------------------------
__________________________________________________________________________________________________________________________________
查看当前数据库
select database();
创建表
create table customer (
id int,
name varchar(20),
age int,
gender enum('男', '女'),
phone varchar(20)
);
__________________________________________________________________________________________________________________________________
查看表结构
describe(desc) customer
查看服务器参数
show variables like ‘%char%’;
一般来说这里是utf-8的编码方式,为了让中文在里面显示,所以我们需要更改设置,
更改客户端字符集
set name gbk;
所以上图是更改后的状态。
丢弃数据库
drop database 数据库名
丢弃表
drop table customer
插入数据
insert into customer(
id,
name,
age,
gender,
phone
)values(
1,
‘佟丽娅’,
15,
‘女’,
‘123456789’
);
__________________________________________________________________________________________________________________________________
更新数据,如果没有where限定,会修改所有数据
updata customer set
age=10,
gender=’男’,
phone=’4578912’
where
id=1;
__________________________________________________________________________________________________________________________________
删除记录
delete from customer
where id=1;
**********************************************************************************************************************************
总的来说,就是增删改查
Insert 插入 C(create)
select查询 R(retrive)
updata修改 U (updata)
delete删除 D (delete)
关系型数据
RMDB
__________________________________________________________________________________________________________________________________
describe country;
+----------------+---------------------------------------------------------------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------------------------------------------------------------------------------------+------+-----+---------+-------+
| Code | char(3) | NO | PRI | | |
| Name | char(52) | NO | | | |
| Continent | enum('Asia','Europe','North America','Africa','Oceania','Antarctica','South America') | NO | | Asia | |
| Region | char(26) | NO | | | |
| SurfaceArea | float(10,2) | NO | | 0.00 | |
| IndepYear | smallint(6) | YES | | NULL | |
| Population | int(11) | NO | | 0 | |
| LifeExpectancy | float(3,1) | YES | | NULL | |
| GNP | float(10,2) | YES | | NULL | |
| GNPOld | float(10,2) | YES | | NULL | |
| LocalName | char(45) | NO | | | |
| GovernmentForm | char(45) | NO | | | |
| HeadOfState | char(60) | YES | | NULL | |
| Capital | int(11) | YES | | NULL | |
| Code2 | char(2) | NO | | | |
+----------------+---------------------------------------------------------------------------------------+------+-----+---------+-------+
___________________________________________________________________________________________________________________________________________
查询*代表任意
select * from country
指定列
select code ,name
from country
___________________________________________________________________________________________________________________________________________
查询过程
就是把基表切开,看select 需要什么列,把什么列现粘贴,产生一个虚表
注意点:
SQL语言大小写不敏感
SQL可以写一行或者多行
关键字不能被缩写也不能被分行
各个子句一般要分行写
使用缩进提高语句的可读性。
给列起别名, 便于显示 和计算, 别名只在虚表中起作用, 不会影响基表
别名可以加上"", 可以保证别名总是原样显示.
___________________________________________________________________________________________________________________________________________
select
capital as 首都,
code,
continent "大 洲",
name
from
country;
where过滤,作用是过虑行,where后面结果一定是一个boolean ,遍历所有结果时,会让每条记录都经过条件判断
如果判断的结果为true,就留下,为false就丢弃他。
还有一个数据库是conpany公司。里面有几个表.
___________________________________________________________________________________________________________________________________________
工资大于等于2500并且 小于等于3500
SELECT last_name, salary
FROM employees
WHERE salary BETWEEN 2500 AND 3500;
___________________________________________________________________________________________________________________________________________
只要是等于集合中的某个值就留下
SELECT employee_id, last_name, salary, manager_id
FROM employees
WHERE manager_id IN (100, 101, 201);
___________________________________________________________________________________________________________________________________________
查询人口在5000万到2亿的国家的国家代码,名称,大洲和人口信息
select
code,
name,
continent,
population
from
country
where
population between 50000000 and 200000000;
___________________________________________________________________________________________________________________________________________
查询中国的代码和地区及平均寿命
select
code,
region,
lifeexpectancy
from
country
where
code = 'chn';
___________________________________________________________________________________________________________________________________________
like
like 过滤只适用于字符串, 用于模糊查询\
下面的like和=一样的效果
select
code,
region,
lifeexpectancy
from
country
where
name like 'china';
___________________________________________________________________________________________________________________________________________
查询的是国家名称以c开头. %表示任意个任意字符
select
code,
name,
region,
lifeexpectancy
from
country
where
name like 'c%';
___________________________________________________________________________________________________________________________________________
查询名字中第二个字母是o的
_代表一个任意字符
SELECT last_name
FROM employees
WHERE last_name LIKE '_o%';
___________________________________________________________________________________________________________________________________________
查询一个城市, 城市名称中包含ing
select
*
from
city
where
name like '%ing%'
___________________________________________________________________________________________________________________________________________
查询国家表, 国家名称中的第3个字母是i,后面包含a的国家
select
code,
name,
continent
from
country
where
name like '__i%a%';
___________________________________________________________________________________________________________________________________________
null值只要是参与比较运算, 结果一定是false, null值在参与统计时会被忽略.
select
code,
name,
capital
from
country
where
capital = null;
null值只支持is, is not操作
select
code,
name,
continent,
capital
from
country
where
capital is null;
___________________________________________________________________________________________________________________________________________
查询有首都的国家
select
code,
name,
continent,
capital
from
country
where
capital is not null;
AND是并且, 要求两个条件都为true
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE
salary >=10000
AND
job_id LIKE '%MAN%';
_______________________________________________________________________________________________________________________________________
OR是或者, 要求两个条件只要有一个为true
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE
salary >=10000
OR
job_id LIKE '%MAN%';
___________________________________________________________________________________________________________________________________________
NOT取反
SELECT last_name, job_id
FROM employees
WHERE job_id
NOT IN ('IT_PROG', 'ST_CLERK', 'SA_REP');
where过滤中不可以使用列的别名.
反映了where是先于select执行的.
过滤完记录后才生成虚表.
select
code,
name,
continent cont
from
country
where
capital is null;
执行顺序:
from where select
___________________________________________________________________________________________________________________________________________
查询国家有多少不同的大洲信息, 需要去重
select
distinct continent
from
country;
order by 排序, 给虚表进行指定的排序.默认就是升序
order by 中可以使用列的别名, 说明它真的就是基于虚表排序.
where中不可以使用别名, 必须使用基表的原名.
from => where => select => order by(通常在SQL的最后)
___________________________________________________________________________________________________________________________________________






浙公网安备 33010602011771号