
MySQL数据管理结构
数据库:
数据存储的仓库。内部存放 表。 表有行列之分。
行: 记录、实体
列:域、字段。
数据库简史:
1. 萌芽阶段: 文件存储
2. 第一代:层次模型、网状模型
3. 第二代:关系型数据库:SQL server、Oracle、MySQL、DB2、infomix、SQLlite
非关系型数据库:redis、MongoDB
安装mysql:
按 aptitude:sudo apt-get install aptitude
测试 MySQL是否安装 sudo aptitude show mysql-server—— 未安装
安装MySQL:sudo apt-get/aptitude install mysql-server
测试 MySQL是否安装 sudo aptitude show mysql-server—— 已安装
mysql -V—— 获取安装版本
MySQL配置文件/etc/mysql/mysql.conf.d/mysqld.cnf—— 配置文件
数据库、表存放位置/var/lib/mysql
MySQL的启动:
【重点】:MySQL数据库,要想使用,必须先启动MySQL服务器!!!
启动MySQL:
1. sudo /etc/init.d/mysql start—— 在其他Linux上,存储目录有变化。
2. sudo service mysql start—— 在Linux中 通用。
关闭和重启:
1. sudo /etc/init.d/mysql stop/restart—— 在其他Linux上,存储目录有变化。
2. sudo service mysql stop/restart—— 在Linux中 通用。
判断是否启动成功:
1. sudo service mysql status—— 如果能看到 绿灯:说明启动成功。
2. netstat -apn | grep mysql —— 查看到 LISTENING 状态的进程
3. ps aux | grep mysql
MySQL登录:
mysql -h127.0.0.1 -P3306 -uroot -p123456
-h:主机服务器ip—— 本机可以省略不写。
-P:端口号。默认3306 —— 可以省略不写
-u:用户名 —— 默认root
-p:密码—— 隐藏密码: -p 直接回车。
退出:
quit、exit、\q
mysql:
基于数据库的数据管理系统。
Oracle:
基于用户的数据管理系统。
数据库的 curd:
查询:
show databases;查看当前MySQL下的数据库
show create database 数据库名;查询创建 数据库的语法,以及当前数据库使用的 字符集。
创建:
语法:create database [if not exists] `数据库名` [字符集]
create database `mydb1`;默认创建的数据库 使用 latin1
create database if not exists `mydb2` charset=uf8;创建 使用 utf8 字符集的 mydb2 数据库。
字符集:latin1、GBK、GB2312、utf8
删除:
语法: drop database [if exists] `数据库名`
drop database if exists mydb1;
如果: 数据库在MySQL中已经存在,有 if exists 不会报错,会删除数据库
数据库在MySQL中不存在,有 if exists 不会报错。
没有if exists 会报错。
修改:
只能修改字符集。
语法:alter database `数据库名` charset=新字符集;
alter database mydb2 charset=gbk;
表的curd:
【重点】:操作表之前,必须先选定数据库。 use 数据库名;
查询:
show tables;查询当前使用的数据库中,表信息。
show create table 表名; 查询创建表的 sql语句。可以看到表所使用的引擎和字符集。
show craete table 表名\G按行展示表数据
desc 表名;查看表结构。su
创建:
语法:create table [if not exists] `表名`(
字段名 数据类型 [not null | null] [auto_increment] [primary key] [comment],
字段名 数据类型 [default]....,
....
) engine=innodb charset=gbk;
创建简单表:
create table t3( id int, age int, name varchar(10) );
创建复杂表:
create table if not exists t2(
id int(9) auto_increment primary key comment '主键',
name varchar(20) not null comment '姓名',
phone varchar(20) comment '电话',
addr varchar(20) default '地址不确定' comment '地址'
)engine=innodb charset=gbk;
在 A 数据库中,向B数据库 创建表:
create table 数据库名.表名(字段名 数据类型,....);
删除:
语法:drop table [if exists] 表1,表2,… ;
修改:
语法: alter table 表名 【修改的形式】。
增加一个字段:
语法:alter table 表名 add [column] 字段名 数据类型 [位置];
alter table t2 add column age int(5) first;
alter table t2 add sex char(2) after id;
删除一个字段:
语法: alter table 表名 drop [column] 字段名;
修改一个字段:改名、改类型:
语法:alter table 表名 change [column] 旧字段名 新字段名 数据类型;
alter table t2 change name fullname char(10);
修改一个字段:只改类型:
语法:alter table 表名 modify [column] 字段名 新数据类型;
alter table t2 modify column fullname varchar(15);
只修改表名:
rename table t2 to t8;或 alter table t8 rename to tb2;
修改默认引擎:
alter table tb2 engine=myisam;
复制表:
只复制表数据,不复制表属性。
语法: create table 新表名 select 字段名, 字段名,... from 表名。
create table tt8 select * from tb2;
只复制表属性,不复制表数据。
语法: create table 新表名 like 旧表名。
create table tt9 like tb2;
想得到两张相同的表:
1. 复制表属性。create table tt9 like tb2;
2. 复制表数据。insert into tt9 select * from tb2;
表数据的curd:
插入:
语法:insert into 表名(字段1, 字段2 .....)values(值1, 值2...)
全插入:
insert into tb2(id, fullname, phone, addr) values(1, '张三', '129000', '深圳');
insert into tb2 values(2, '张三', '129000', '深圳');
指定列插入:
语法:insert into 表名(字段1,字段5, 字段3)values(值1, 值5, 值3);
insert into tb2(id,fullname)vaules(3, '李白');
显示插入空值:
在允许为空的列,插入时,值传 null
隐式插入空值:
在允许为空的列,插入时,不插入数据。MySQL数据库会自动写入 null
隐式插入如默认值:
对于有默认值的 列, 插入时,不插入数据。MySQL数据库会自动写入 默认值。
插入多行:
insert into tb2 values(null, '陆游', null, default), (null, '李清照', '7777', '济南'), (null, '辛弃疾', '88888', '北京');
更新:
语法:update 表名 set 字段名=值 [where 条件];
修改一条记录的一个字段:
update tb2 set fullname='杜甫' where id = 4;
如果没有 where 条件,会修改整张表。
修改一条记录的 多个字段:
update tb2 set fullname='杜牧', phone='123987', addr='西安' where id=5;
修改多条记录:
update tb2 set phone='88888' where id =1 or id=3 or id=5 or id=7;
删除:
语法: delete from 表名 [ where 条件 ]
delete from tb2 where id >=5;
清空表:
delete from 表名;
将表中的数据,清空,表空间没有被释放。速度慢
truncate 表名;
将表摧毁,重建新表。表空间会被释放。速度快。
查询:
语法:select 列名,列名… from 表名 [ where 条件 ]
select id, name, addr from tb2 where id >=3;
SQL语句分类:
DML:insert、update、delete、select
DDL: create、truncate、drop、alter
DCL:commit、rollback
本文介绍了MySQL数据库的安装、配置、启动和停止过程,包括如何检查安装状态、查看数据库和表信息、创建和删除数据库及表。还详细讲解了表的增删改查操作以及数据的插入、更新、删除和查询。此外,提到了SQL语句的分类和作用。

253

被折叠的 条评论
为什么被折叠?



