您的位置:首页 > 数据库 > MySQL

mysql 数据库的操作

2016-11-26 01:14 309 查看
1、数据库的操作

进入数据库

mysql -u root(用户名) -p(不加空格直接输密码) 或回车 输入密码

创建数据库

create database 数据库名称

create database today;

删除数据库

drop database 数据库名称;

drop database today;

查看数据库

show databases;

查看创建数据的代码

show create database 数据库名称;

show create database today;

使用数据库

首先创建数据库(已有不用创建)——使用数据库

use 数据库名称;

use today;

修改数据库编码

alter database 数据库名称 character set 编码 collate 校验规则;

alter database today character set utf8 [collate utf8_chinese_ci;

2、在数据库中创建表及表和表中数据的修改

创建表

首先要创建数据库(以有不用)——使用数据库——创建表

create table employee(

id int,

name varchar(20),

gender char(2),

birthday datetime,

entry_date date,

job varchar(200),

salary double(9,3),

resume longtext

);

表和表中数据的修改
查看一个数据库中有几个表:show tables;
修改表名:rename table 旧的表名 to 新的表名;
查看建表的代码: show create table 表名;
查看表的详细信息:describe 表名;
修改表的编码:alter table 表名 character set 编码;
删除表:drop table 表名;

增加列:alter table 表名 add 列名 数据类型;
删除列:alter table 表名 drop 列名;
修改列的数据类型:alter table 表名 modify 列名 新的数据类型;
修改列的名称:alter table 表名 change column 旧列名 新列名 数据类型;

例子:
查看数据库中有几个表
show  tables;
在员工列表中增加一个image列
alter table employee add image longblob;
修改job列长度为60;
alter table employee modify job varchar(60);
删除image列;
alter table employee drop image;
改列名name为username;
alter table employee change column name username varchar(100);

查看建表代码:show create talbe employee;
查看表的详细信息:describe employee;
修改表的编码:alter table today character set utf8;
删除表:drop table today;

DDL:数据库定义语言,常用命令——create、alter、drop
DML:数据库操作语言,常用命令——insert、update、delete
DQL:数据库查询语言,常用命令—— select


3、CRUD命令

insert实现数据添加

insert into 表名 [列名] values (值);

由于数据过长,要先设置此昂服务器段提交数据的编码
set character_set_client=gbk;
设置从服务器取出数据的编码
set character_set_results=gbk;
例:
insert into employee values(1,'楚留香','男','1680-6-8','1688-8-6','盗帅',10000,'专业劫富济贫');
insert into employee values(2,'张无忌','男','1880-6-8','1888-8-6','明教教主',1000,'维护正邪之间的和平');

update实现数据更新
update 表名 set 列名=值 where 条件;

例子:
将所有员工薪水改为5000;
update employee set salary=5000;
将楚留香的薪水改为8000,工作改为office;
update employee set salary=8000,job='office' where username='楚留香';
给张无忌涨薪水600;
update employee set salary=salary+600 where username='张无忌';

delete实现删除数据
delete from 表名 where 条件;
truncate table 表名;
区别:delete一行一行删除效率低去,且删除指定条件记录,不会删除表结构
truncate 删除效率高,将整个表删除后重建表结构

例子:
删除张无忌的记录
delete from employee where username='张无忌';
删除表中所有记录
delete from employee;

select实现查询
select 列名(*代表所有列) from 表名 where 条件 order by 列名(asc/desc);
destinct:可以去除重复数据
as:可以修改列名,可以不写
like:可以查询以什么开头(李开头,'李%')或结果(亭结尾'%亭')的数据

例子
create table student(
id int,
name varchar(20),
chinese float,
english float,
math float
);
insert into student(id,name,chinese,english,math) values(1,'张小明',89,78,90);
insert into student(id,name,chinese,english,math) values(2,'李进',67,98,56);
insert into student(id,name,chinese,english,math) values(3,'王五',87,78,77);
insert into student(id,name,chinese,english,math) values(4,'李一',88,98,90);
insert into student(id,name,chinese,english,math) values(5,'李来财',82,84,67);
insert into student(id,name,chinese,english,math) values(6,'张进宝',55,85,45);
insert into student(id,name,chinese,english,math) values(7,'黄蓉',75,65,30);

查询表中所有学生信息
select * from student;
查询表中所有学生的姓名的其对应的语文成绩
select name,chinese from student;
过滤重复数据:以英语为例
select distinct english from student;
统计每个学生的总成绩
select name '姓名',math+english+chinese 总分 from student;
查询王五的成绩
select * from student where name='王五';
所有学生的数学加10分
select name as 姓名,math+10 数学 from student;
查询总成绩>200的学生
select * from student where math+english+chinese>200;
查询语文>80的学生
select * from student where chinese >80;
查询语文在70——80之间的学生
select name from student where chinese between 70 and 80;
select name from student where chinese >=70 and chinese <=80;
查询数学为89,90,91的同学
select name from student where math in(89,90,91);
select name from student where math =89,math=90,math=91;
查询所有姓李的学生的成绩
select math,english,chinese from student where name like'李%';
对数学成绩排序后降序输出
select * from student order by math desc,id desc;
对总成绩排序按照降序输出
select math+english+chinese from student order by math+english+chinese desc;
对姓李的学生成绩安总成绩进行升序输出
select math,english,chinese from student where name like '李%' order by math+english+chinese asc;


4、表之间的关系

一对多;一个顾客可以下多个订单

customer

id

name

address

orders

id

product

num

price

customerid(顾客编号),它的值来源于customer表中的id

数据完整性:

参照完整性

一个表中的某列的取值只能来源于另一个表中的某列的值,

一般可通过外键约束来实施参照完整性约束。

alter table 表名 add constraint 外键名 foreign key(外键字段) refrences 主键表(主键)

接触外键

alter table 表名 drop foreign key 外键名

实体完整性
指的是表中的没有相同的两行,通过主键约束(primary key)来实施
主键约束特性:唯一性,非空性
auto_increment自动增长,从1开始,id不变
域完整性
指列,说明的是某列应该符合的数据要求。
通常有:not null  ,  unique(唯一)

多对多
形成一个中间表,用来描述关系
一对一
用得少,可以通过主键+外键,或外键+唯一约束


5、联接查询

create table department(

id int primary key,

name varchar(100) not null

);

create table employee(

id int primary key,

name varchar(100),

dept_id int

);

alter table employee add constraint

FK_employee_dept_id foreign key(dept_id)

references department(id);

set character_set_client=gbk;
set character_set_results=gbk;
insert into department values(1,'开发部');
insert into department values(2,'销售部');
insert into department values(2,'销售部');//不能重复

insert into employee values(1,'A',1);
insert into employee values(2,'AJ',2);
insert into em
4000
ployee values(3,'CGX',3);

交叉连接
隐式查询 : select * from A,B;
显式查询(cross join): select * from A cross join B;
例:
select * from department cross join employee;
select * from department,employee;

内连接
select * from A inner join B on(连接条件) where 条件
例:
select department.id from department inner join employee  on(department.dept_id =employee.id);

CREATE TABLE customer (
id int(11) NOT NULL auto_increment,
name varchar(100) default NULL,
city varchar(20) default NULL,
PRIMARY KEY  (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE orders (
id int(11) NOT NULL auto_increment,
order_number varchar(100) default NULL,
price float(8,2) default NULL,
customer_id int(11) default NULL,
PRIMARY KEY  (id),
KEY customer_id_fk (customer_id),
CONSTRAINT customer_id_fk FOREIGN KEY (customer_id) REFERENCES customer (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `customer` VALUES (1,'李连杰','香港'),(2,'周华健','台北'),(3,'苍井空','东京'),(4,'钟欣桐','香港'),(5,'林志玲',NULL);
INSERT INTO `orders` VALUES (1,'0001',100.00,1),(2,'0002',200.00,1),(3,'0003',300.00,1),(4,'0004',100.00,2),(5,'0005',200.00,3),(6,'0006',100.00,4),(7,'0007',1000.00,NULL);

外连接
左外连接:select * from A left join B on(连接条件) where 条件
left join左边的表为左表,left join右边的表为右表
例子: select * from customer A left join orders B on (A.id=B.customer_id);

右外连接:select * from A right join B on(连接条件)where 条件
例子:
select * from customer A right join orders B on (A.id=B.customer_id);
select * from orders B right join  customer A on (A.id=B.customer_id);

联合查询
union表示联合查询
查询李连杰所下订单及金额在200以上的订单
select * from orders where customer_id=1 union
select * from orders where price>200;

select * from orders where customer_id =(
select id from customer where name='李连杰'
)union select * from orders where price>200;

子查询
select * from 表1 where id in(select id from 表2 where 条件)
外面的为父查询,里面的为子查询
子查询可以嵌入在(select部分,from部分,where条件部分)
一般都是嵌入在where条件部分

总结:
1.交叉连接一般不用
2.内连接是将A表与B表能连接上的记录显示出来,用得很多
3.外连接用得少,左外连接显示的结果是能连接上的记录及左表中连接不上的记录
右外链接显示的结果是能连接上的记录及右表中连接不上的记录
4.联合查询用UNION关键字,它能去除重复记录,并且要求合并的记录(列数要对应,类型要兼容)
5.子查询,(一般嵌入在where条件中,通过子查询先查出一个结果,
再将子查询返回的结果作为父查询的条件去使用)

create table orderss(
id int,
product varchar(20),
price float
);
insert into orderss(id,product,price) values(1,'电视',900);
insert into orderss(id,product,price) values(2,'洗衣机',100);
insert into orderss(id,product,price) values(3,'洗衣粉',90);
insert into orderss(id,product,price) values(4,'桔子',9);
insert into orderss(id,product,price) values(5,'洗衣粉',90);

分组与统计
select * from  表名 inner join B on(连接条件)
where 条件 group by 分组字段 [having (分组后的条件)] order by asc/desc;

分组:通过group by 实现
统计:max(*)、min(*)、avg(*)、count(*)、sum(*)

例子:
对订单表中的商品归类后显示每类商品的总价
select product,sum(price) from orderss group by product;
查询总价>100的种类的商品
select product,sum(price) from orderss group by product having(sum(price)>100);

统计一个班级共有多少学生
select count(*) from student;
统计数学成绩大于90的学生有多少个
select count(*) from student where math>90;
统计总分大于250的人数有多少
select count(*) from student where (math+english+chinese)>250;
统计一个班级数学总成绩
select sum(path) from student;
统计一个班级语文、英语、数学各科的总成绩
select sum(chinese),sum(english),sum(math) from student;
统计一个班级语文、英语、数学的成绩总和
select sum(chinese)+sum(english)+sum(math) from student;
select sum(chinese+english+math) from student;
统计一个班级语文成绩平均分
select avg(chinese) from student;
select sum(chinese)/count(*) from student;
求一个班级总分平均分
select avg(chinese+english+math) from student;
select sum(chinese+english+math)/count(*) from student;
求班级总分最高分和最低分
select max(chinese+english+math) 最高分,min(chinese+english+math)最低分;


6、备份与还原

备份

window命令下

mysqldump -h localhost -u root -p 数据库名称>路径/数据库名称.sql

例:

mysqldump -h localhost -u root -p today>f:/today.sql;

还原
mysql命令下
source 备份的位置
登陆mysql——create database 数据库名称——use 数据库名称——source 路径

window命令下
登陆mysql——create database 数据库名称——
exit,退出mysql——mysql -u root -p 数据库名称<路径
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql