MySQL数据库基础
数据库的认识
数据库类型
关系型数据库:MySQL 、Oracle(性能好,超量数据)、 DB2、SQL server、……
非关系型数据库:Redis、Memcached(内存型k-v数据库)MongoDB(文档型数据库)、……
数据库基础及重要职责
1、查询数据
2、存储数据
数据库系统的特点:
数据结构化、
数据度的冗余度小、
较高的数据独立性。
配置 mysql 环境变量
第⼀次连接可能出现如下情况:
mysql’ 不是内部或外部命令,也不是可运⾏的程序或批处理⽂件。
这时我们需要将 mysql 添加到系统的连接变量中,操作思路:
- 找到 mysql 的安装⽬录;
- 将 mysql 配置到系统环境中。
具体操作如下
点击确认后返回!
重启控制台,链接MySQL服务器
MySQL -uroot -p 密码
SQL命令分类
DDL【Data Definition Language】数据定义语⾔,⽤来维护存储数据的结构代表指令: create,drop, alter;
DML【Data Manipulation Language】数据操纵语⾔,⽤来对数据进⾏操作代表指令:insert,delete,update,
DML中⼜单独分了⼀个DQL,数据查询语⾔,代表指令:select;
DCL【Data Control Language】数据控制语⾔,主要负责权限管理和事务代表指令:grant,revoke,commit。
数据库的操作(DDL)
- 显示服务器中的所有数据库
** show databases;**
- 创建数据库
create database(if not exists) 数据库名;
MySQL命名规则:
1、 所有的数据库名称和表名,以及字段名必须要求全部小写。
2、多个字母之间使用“_”分隔。
- 使用数据库
use 数据库名;
- 删除数据库
drop database(if exists )数据库名;
MySQL常用数据类型
数值类型
字符串类型
日期类型
MySQL表的操作
创建表
create table 表名(
field1 datatype,
field2 datatype,
field3 datatype
);
使用comment字段添加说明
mysql> Create table student(
-> Sno nvarchar(100) not null primary key,
-> Sname nvarchar(100),
-> Ssex nvarchar(20),
-> Sclass nvarchar(20),
-> Sage int
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> show full columns from student;
+--------+--------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+--------+--------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| Sno | varchar(100) | utf8_general_ci | NO | PRI | NULL | | select,insert,update,references | |
| Sname | varchar(100) | utf8_general_ci | YES | | NULL | | select,insert,update,references | |
| Ssex | varchar(20) | utf8_general_ci | YES | | NULL | | select,insert,update,references | |
| Sclass | varchar(20) | utf8_general_ci | YES | | NULL | | select,insert,update,references | |
| Sage | int(11) | NULL | YES | | NULL | | select,insert,update,references | |
+--------+--------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
5 rows in set (0.01 sec)
查询表
查询所有表名
show tables;
查询表结构
desc table_name;
查询表所有字段
show create table 表名;
show full columns from 表名;
删除表
drop table [if exists] 表名 ..
mysql> drop table student;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+------------------+
| Tables_in_java33 |
+------------------+
| book |
| books |
| commodity |
| teachers |
| user |
+------------------+
5 rows in set (0.00 sec)
修改表结果
添加字段
alter table 表名 add column 列名 列类型 [ comment 备注信息]
mysql> alter table customer add column age int comment'年龄';
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
删除字段
alter table 表名 drop column 字段名;
mysql> alter table customer drop column age;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
修改字段
alter table 表名 change 原字段名 新字段名 类型[约束]
mysql> alter table customer change name name varchar(250)comment '客户名称';
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
修改字段不会对原有数据产生影响
修改表名
alter table 旧表名 rename 新表名;
修改某个表编码格式
alter table 表名 convert to character set 新编码;
内容总结
数据库操作
创建数据库: create databases 数据库名;
使用数据库:use 数据库名;
删除数据库:drop database 数据库名;
查询所有数据库:show databases;
查询当前所在数据库:select databases();
表操作
查看表结构:desc 表名;
创建表: create table 表名(
字段 类型 comment‘注释’
);
查询表:show tables;
删除表: drop table 表名;
修改表 alter table 表名……;