Mysql数据库操作管理

127 阅读4分钟

一、常用的数据类型:

类型含义
int管理
char固定长度字符类型
varchar可用长度字符类型
text文本

mysql -u root -pabc123 登录数据库

二. 查看数据库结构

1.查看当前服务器中的数据库

show databases;

2.数据库中包含的表

use 数据库名;

3.查看表结构

三. SQL语句

用于维护管理数据库,包含数据查询、数据更新、访问控制、对象管理功能。

3.1 SQL语言分类

  • DDL :用于管理数据库对象 库、表、索引。
  • DML :用于管理表数据。
  • DQL :用于根据条件查询表数据。
  • DCL :用于管理用户与权限。

四 . Mysql数据库的管理命令

1. 管理命令

创建库

create database 库名;
例题:create database YHY;

image.png

查看库

show databases;

image.png

运行库

use 库名;
use YHY

image.png

创建表

create  table  表名;
例create table yhy1 (id int,name char(4),age int,sex char(2),hobby varchar(20));
 id(排号),name(名字),age(年龄),sex(性别),hobby(爱好)
 intchar4),char(2),varchar(20)文件类型,并且指明字段里面有几个字

image.png

查看表

 show tables;

image.png

查看表结构

desc   yhy1;

image.png

指定主键字段

加上:primary key(id)

特性:一个表中只能有一个主键字段,并且值不能为空,不能重复。

create table yhy1 (id int,name char(4),age int,sex char(2),hobby varchar(20),primary key(id));

image.png

image.png

删除表

drop table  yhy1;

image.png

删除库慎用

drop database  库(kcg)

向表内输入数据

insert into yhy2  values (1, 'cyw' , 23, '男' , '睡觉'); 

image.png

查看表内容

select  * from yhy2; 

image.png

插入数据

例题:insert into  yhy2 (id,name,hobby) values (2,'lzq','跳舞');

image.png

select * from yhy2;

image.png

修改表中原有内容

 update  yhy(表名) set  age(年龄) where(=) id=1;
#只修改id=1的一列的年龄

image.png

updata yhy set age=22 ,hobby='原神' where name='yhy';
#修改名字是yhy那一列的 年龄和爱好

image.png

image.png

删除一条记录

delete  from yhy where    id=1 ;
                 #删除来自表中id=1的一列
                          id>=1 and id<=3;
                 #删除1-3的记录包含1和3
                          id=1 or id=3
                          #删除id=1和id=3的一例

image.png

查看表中1,3中的记录

select *from  yhy where id=1 or id=3;
#查看yhy表中的1,3条记录

image.png

查看表前2行数据

 select *from yhy  limit 2#limit 是分页查询
 #查看来自yhy表中前两行数据

image.png

查看后2行数据

 select *from yhy limit 2,2#查看2行之后的连续2行,不包含2(数字可以自定义为n(不包含n行))

image.png

纵向查看表中数据

select * from yhy\G;
#将表中的内容纵向查看。

image.png

查看自己需要的数据

select name,hobby  from yhy;
#查看yhy表中的name,habby这两类数据

image.png

2. 修改表结构

改表名

alter table 旧表名 rename 新表名;
alter table yhy3 rename yyy;

image.png

增加字段

 alter table 表名 add 新字段 数据类型 [字段属性];
例:alter table yhy add address varchar50default'地址不详'

image.png

修改字段

alter table 表名 change 旧字段 新字段 数据类型 [字段属性];   

image.png

删除字段

alter table 表名 drop 字段;
#alter table yhy drop address;

image.png

五.MySQL 的约束特性

  • primary key 主键约束 字段的值不能重复,且不能为null,一个自建的表只能有一个主键

  • unique key 唯一性约束 字段的值不能重复,能为null,一个表可以有多个唯一键

  • not null 非空约束 字段的值不能为null

  • default 默认值约束 字段的值如果没有设置则使用默认值自动填充

  • auto_increment 自增约束 字段的值如果没有设置默认会从1开始每次自动递增1,要求自增字段必须设置主键

  • foreign key 外键约束

  • int(N) zerofill 零填充

六. Mysql 高级用法

克隆表

方法1
 #克隆表结构
create table 新表 like 旧表;       
      
#克隆表数据
insert into 新表 (select * from 旧表);       
可实现表结构和表数据与旧表都一样



#克隆表结构
例:create table yhy like yhy1

#再克隆数据导入
insert inte yhy1 select* yhy
将yhy里面的数据内容传入yhy1

 方法2
 create table 新表 (select * from 旧表);         表数据与旧表一样,表结构与旧表可能不一样

方法1

image.png

image.png

方法2:

image.png

image.png

image.png

清空表

 方法1
 delete from 表名;        
 #一条一条的删除记录,效率较慢;自增字段仍然会按照清空前的最大记录继续自增
 
 方法2
 truncate table 表名;  
 直接重建表,清空表效率更快;自增字段会重新从1开始自增

image.png

临时表

 create temporary table 表名 (....); 
 例:create temporary table yhy (id int,name varchar10),sex char)
 临时表可以跟普通的表一样增删改查表中的数据,但是show tables是查看不到的,
 临时表只能在当前会话中有效,在其它会话中或者退出当前会话连接,临时都会失效

image.png

image.png

image.png