微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

数据库基础[1]安装与操作mariadb

为了看阳光 我来到世上

数据库介绍

数据库一个存放数据的仓库,目前市面上最流行的数据库大致氛围的两种,一种叫做关系型数据库,一种叫做非关系型数据库,而关系型数据库近期流行的即为MysqL,MysqL原本是一个开源的数据库后被oracle公司收购,开始进行服务收费,因此,市面上大多数免费的类MysqL数据库为mariadb或percona两种,centos官方元提供了mariadb版本。

mariadb使用

因为是类MysqL数据库,因此使用方式与MysqL无异

mariadb安装

yum -y install mariadb mariadb-server

mariadb启动

systemctl start mariadb
systemctl enable mariadb

mariadb基本使用

MysqL进行基本设定,增删改查等基本操作

初始化密码

MysqLadmin -u root password 123456 -p
set password for 'root'@'localhost'=password('123456');
数据库

针对于数据库的增删改查等

查询数据库
show databases;
show create database mydb;
创建数据库
create database mydb;
create database mydb default character set utf8 collate utf8_general_ci;
修改数据库
alter database mydb default character set utf8 collate utf8_general_ci;
删除数据库
drop database mydb;
使用数据库
use mydb;

针对与某一数据库中表的增删改查等

查询表
show tables;
show create table test1;
desc test1;
创建表
create table test1 (
id int primary key auto_increment comment '学员序号',
name char comment '学员姓名',
age char comment '学员年龄',
sex boolean comment '学员性别');
修改
alter table test1 modify column sex boolean comment '学员性别';
删除
drop table test1;
表内数据

针对于某一数据库中,某一张表内数据的曾删改查等

查询数据
select * from test1;
select id,name,age from test1;
select id,name,age from test1 where age=17;
select id,name,age from test1 where age=17 order by id;
select id,name,age from test1 where age=17 group by name;
插入数据
insert into test1 (name,age,sex) values
('zhangsan', 17, 1),
('lisi', 17, 1),
('xiaoming', 17, 1),
('xiaoli', 15, 0);
修改数据
update test1 set name='xiaoxingxing' where name='xiaoli';
删除数据
delete from test1 where name='zhangsan';
注意:

修改字段类型及名称
如果需要修改字段类型及名称, 你可以在ALTER命令中使用 MODIFY 或 CHANGE 子句 。

例如,把字段 id 的类型从 CHAR(1) 改为 CHAR(11),可以执行以下命令:

alter table test1 modify id char(11);

使用 CHANGE 子句, 语法有很大的不同。 在 CHANGE 关键字之后,紧跟着的是你要修改的字段名,然后指定新字段名及类型。尝试如下实例:

alter table test1 change id name BIGINT;
alter table test1 change name name INT;

如果遇到下面这个报错怎么解决

ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'

解决办法:

alter table sg_medal_action drop primary key;

解释地址:https://yq.aliyun.com/articles/516990

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐