MySQL 是一个 DBMS(数据库管理系统),由瑞典 MySQLAB 公司开发,目前属于 Oracle 公司,MySQL 是最流行的关系型数据库管理系统(关系数据库,是创建在关系数据库模型基础上的数据库,借助于集合代数等概念和方法来处理数据库中的数据)。因为其体积小、速度快、整体拥有成本低,尤为是开放源码这一特色,通常中小型网站的开发者都选择 MySQL 做为网站数据库。MySQL 使用 SQL 语言进行操做。mysql
数据库分为关系型数据库和非关系型数据库。sql
mysql 下载地址: MySQL Community Server 5.7.20数据库
windows安装
安装很简单,直接点击下一步,安装完成后,能够在服务里面(win+R而后输入services.msc)找到mysql服务,能够手动中止或关闭。ubuntu
ubuntu安装vim
# 安装 MySQL 服务端、核心程序 sudo apt-get install mysql-server # 安装 MySQL 客户端 sudo apt-get install mysql-client # 安装结束后,用命令验证是否安装并启动成功 sudo netstat -tap | grep mysql # 能够根据本身的需求,用 vim 修改 MySQL 的配置文件(my.cnf),使用如下命令 sudo vim /etc/mysql/my.cnf # 启动 MySQL 服务 sudo service mysql start # 使用 root 用户登陆,实验楼环境的密码为空,直接回车就能够登陆 mysql -u root -p
中止mysql进程windows
ps -ef #查看全部进程 ps -ef | grep mysql # 筛选mysql kill 4299 # 删除进程号为4299的进程
其余操做学习
mysqladmin --version # 查看mysql版本 mysqladmin -u root password `new_password`; #建立密码 # mysql数据库登录 mysql -h 'remote_ip' -u username -p # 以后会提示输入密码
SELECT column_name, column_name from table_name [WHERE Clause] [LIMIT N][OFFSET M] select * from student limit 10 offset 2; select * from student where id>10; select * from student where register_date > `2016-03-04`; select * from student where register_date like `2016-04%`
insert into student (name, sex, age, register_date) values (`alex`, `M`, 23, `2017-07-12` );
update student set name=`chenronghua`, age=33 where id=4;
delete from stuent where name=`chenronghua`
select * from student order by id desc;
select name,count(*) from student group by name; select register_date,count(*) from student group by register_date; select register_date,count(*) as stu_num from student group by register_date; select name,sum(age) as stu_num from student group by register_date; select coalesce(name,`Total Age`),sum(age) from student group by name with rollup;
alter table study_record modify id int auto_increment; alter table student modify sex enum(`F`,`M`) not null;
alter table student change sex gender char(32) not null default `F`;
create table `study_record`(`id` int(4) auto_increment, `day` int(11) not null, `stu_id` int(11) not null, `status` char(32) not null, primary key (`id`), key `fk_student_key` (`stu_id`), constraint `fk_studnet_key` foreign key (`stu_id`) references `student` (`id`));
先建立两个表A和B,而且增长一些数据网站
create table A(a int not null); create table B(b int not null); insert into A (a) values (1); insert into A (a) values (2); insert into A (a) values (3); insert into A (a) values (4); insert into B (b) values (3); insert into B (b) values (4); insert into B (b) values (5); insert into B (b) values (6); insert into B (b) values (7);
求A和B表格的交集,并集,差集等。code
RIGHT JOIN(右链接): 与 LEFT JOIN 相反,用于获取右表全部记录,即便左表没有对应匹配的记录。server
select * from A inner join B on A.a = B.b select A.*,B.* from A,B where A.a = B.b
select * from A left join B on A.a = B.b select * from A right join B on A.a = B.b
select * from A full join B on A.a = B.b # 出错 -- # 能够经过下面的语法间接支持 select * from A left join B on A.a=B.b union select * from A right join B on A.a=B.b
begin; # 开始事务 rollback; # 回滚 commit; # 提交
-- 已经建立的表增长索引 create index name_index on student (name(32)) -- alter student add index date_index on (register_date(32)) -- 建立表的时候增长索引
drop index index_name on student; show index from student; -- 显示index
-- 已经建立的表增长索引 create unique index name_index on student (name(32))