MySQL数据库学习
MySQL是一个关系型数据库管理系统,使用MySQL有以下几点优势:
- 使用MySQL可以持久化数据
- 使用MySQL保证数据的安全性
- 使用MySQL可以保证数据没有冗余
- 使用MySQL查询数据方便
创建一个公司数据库
-- SQL (structured Query Language - 结构化查询语言)
-- DDL (数据定义语言) create / drop / alter
-- DML (Data Manipulation Language) insert / delete / update
-- DQL (Data Query Language) select
-- DCL (Data Control Language) grant / revoke
-- 创建company数据库
drop database if exists company;
create database company default charset utf8;
-- 关系型数据库用二维表组织数据
-- 关系型数据库有自己的编程语言
-- 切换到company数据库
use company;
-- 创建部门表
-- 能够唯一确定一条记录的列可以设置为主键
drop table if exists tb_dept;
create table tb_dept
(
deptno integer not null comment '编号',
dname varchar(10) not null comment '名称',
dloc varchar(10) comment '所在地',
primary key (deptno) #设置deptno为主键
);
alter table tb_dept add ddate date comment '成立日期';
-- 向部门表添加数据
insert into tb_dept values (10, '财务部', '成都', now());
insert into tb_dept values (20, '研发部', null, null);
insert into tb_dept (deptno,dname) values (30, '销售1部'), (40, '销售2部'), (50, '后勤保障部');
-- 删除数据(注意:一定要带上条件)
-- set deptno=50 表示给deptno赋值50
delete from tb_dept where deptno=50;
update tb_dept set dloc='深圳', ddate='2018-4-1' where deptno=30;
-- 查所有行所有列
select * from tb_dept;
-- 查询表里面的 deptno, dname 列
select deptno, dname from tb_dept;
-- 查询表里面的 deptno, dname 列并重新命名
select deptno as 部门编号, dname as 部门名称 from tb_dept;
-- 查询并加上筛选条件
select deptno, dname from tb_dept where dloc='深圳';