本文共 5483 字,大约阅读时间需要 18 分钟。
MySQL 是甲骨文 Oracle 公司开发的开源关系型数据库管理系统,广泛应用于各类企业级应用开发。作为数据库中最常用的系统,它以其高效的性能和灵活的配置选项著称,支持多种数据类型和复杂的查询操作。
数据库是现代应用开发的核心基础设施。它通过将数据组织成结构化的表格,实现了数据的持久化存储和高效管理。MySQL 等数据库管理系统(DBMS)能够帮助开发者快速完成数据的插入、查询、更新和删除操作,满足了大部分应用程序的需求。
数据库系统具有以下特点:
MySQL 的主要特点包括:
在 MySQL 中,服务的管理可通过以下命令实现:
service mysqld start
service mysqld stop
service mysqld restart
登录 MySQL 服务:
mysql -h localhost -uroot -p
退出登录状态:
mysql -uroot -p
以下是一些常用的 MySQL 命令:
show databases;
use mydb
show tables;
select database();
create table stu_info (id int, name varchar(20));
desc stu_info;
select * from stu_info;
insert into stu_info (id, name) values (1, '陈翔');
select version();
MySQL SQL 语法的规范包括:
/* */
或 #
。MySQL 提供多种图形化管理工具,例如 Navicat,可以简化数据库管理操作。
MySQL 支持以下四种主要的 SQL 语言类型:
以下是常用表的创建示例:
create table employees ( employee_id int primary key, first_name varchar(20), last_name varchar(25), email varchar(25), phone_number varchar(20), job_id varchar(10), salary double(10,2), commission_pct double(4,2), manager_id int(4), department_id int(4), hiredate datetime);
create table departments ( department_id int primary key, department_name varchar(3), manager_id int(6), location_id int(4));
select last_name from employees;
select last_name, salary, email from employees;
select * from employees;
可以使用常量值、表达式或函数进行查询:
select 100;select '起飞';select 100%98;
select 100%98;
select version();
为查询结果中的字段起别名:
select 100%98 as result;
select salary as 'out_put' from employees;
select GROUP_CONCAT(first_name, last_name) as 姓名 from employees where employee_id=1;
desc departments;
select ifnull(commission_pct, 0) as 奖金率, commission_pct from employees;
select first_name as 姓 from employees where employee_id=1;
and, or, not, &&, ||
like 'between' and in is null;
select * from employees where salary > 12000;
select last_name, department_id from employees where department_id != 1;
用于指定范围查询:
select * from employees where employee_id between 2 and 3;
用于批量查询或判断字段是否属于列表:
select * from employees where department_id in (1,3);
判断字段是否为空:
select last_name, commission_pct from employees where commission_pct is null;
select last_name, commission_pct from employees where commission_pct is not null;
用于安全比较:
select last_name from employees where salary > 15000;
select * from employees order by salary asc;
select * from employees order by salary desc;
select * from employees order by salary;
select * from employees where department_id > 2 order by hiredate;
select last_name, salary * (1 + ifnull(commission_pct, 0)) as 年薪 from employees order by salary;
MySQL 提供丰富的函数,涵盖字符、数学、日期等类型:
select length('陈翔六点半之铁头无敌');select concat(last_name, '_', first_name) as 姓名 from employees;
select round(1.45);select ceil(1.21);select floor(9.99999);select truncate(1.678932, 1);select mod(10, 3);
select year(now());select date_format(now(), '%y年%m月%d日');
select last_name, if(salary > 12000, '工资大于1.2', '工资小于1.2') from employees;
select last_name, case when salary > 18000 then 'A级工资', when salary > 12000 then 'B级工资', else 'C级工资' end as salary_level from employees;
select count(*) from employees group by department_id;
select max(salary) from employees where commission_pct is not null group by job_id;
insert into girl (id, name) values (4, '佟丽娅');
insert into girl values ('高圆圆');
update girl set name='李亚茹的伙伴' where name='李亚茹';
update girl set name='李亚茹', boy_friend_id=4 where name='李亚茹';
delete from girl where id=15;
delete from girl where name='张无忌' and boy_friend_id=(select id from boy where name='张无忌');
set autocommit=0;start transaction;
commit;
rollback;
create view v1 as select girl.name, boy.name as boy_friend_name from girl left join boy on girl.boy_friend_id=boy.id where girl.name like '%李%';
select * from v1 where name like '%李%';
delimiter $create procedure my_pro() begin insert into boy (name) values ('张飞'), ('关羽'), ('赵云'); end$
delimiter $create procedure my_pro2(in girl_name varchar(20)) begin select * from boy left join girl on boy.id=girl.boy_friend_id where girl.name=girl_name; end$
delimiter $create function my_fun1() returns int begin declare result int default 0; select count(*) into result from boy; return result; end$
select my_fun1();
if (condition, true_value, false_value)
while loop
MySQL 是一款强大的关系型数据库管理系统,适合处理结构化数据存储和复杂查询需求。通过掌握数据库的基本操作和高级功能,可以有效提升应用开发效率。
转载地址:http://xabfk.baihongyu.com/