数据库基础[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

相关文章

安装开始...1.打开“https://dev.mysql.com/downloadsepo/yu...
安装Helm3#官网下载慢#wgethttps://get.helm.sh/helm-v3.5.4...
通过Linux命令行启动用的指令:systemctlstartmariadb.servi...
InstallingMariaDBServer10.4TodeployMariaDBCommunityServe...
阅读目录一什么是存储引擎二mysql支持的存储引擎三使用存储引...
1、安装MariaDB安装命令yum-yinstallmariadbmariadb-server安...