mariadb数据库、表基本操作

一、初始化mariadb服务

1.1  mariadb安装与初始化

yum install mariadb-server 

1.2  启动/重启/关闭mariadb服务 

        1. 启动命令  systemctl start mariadb
        2. 重启命令  systemctl restart mariadb
        3. 关闭命令  systemctl stop mariadb
        4. 开机自起动  systemctl enable mariadb
        5. 关闭自起动  systemctl disable mariadb

1.3  初始化配置

mysql_secure_installation
  • 此时没有密码, 直接按enter
  • -> Set Root password? 输入: Y 输入两遍你要设置的新密码
  • -> Remove anonymous users? 输入: Y     是否删除匿名用户
  • -> Disallow root login remotely? 输入: n    允许root远程登录
  • -> Remove test database and access to it? 输入: Y    是否删除test库
  • -> Reload privilege tables now? 输入: Y   是否新加载权限,输入y,回车,安装完成!

1.4  修改数据库密码

 进入数据库内,执行命令:  set password for root@localhost=password("新密码");

1.5  关闭防火墙

systemctl stop firewalld
systemctl disable firewalld

 

二、 mariadb数据库操作

2.1  登陆数据库

mysql -u root -p
或者
mysql -uroot -p

2.2  创建数据库

create database 数据库名称;           # 创建新的数据库

 自定义数据库字符集创建数据库:

GBK:  create database carlos_test1 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
UTF8: create database carlos_test2 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

2.3  显示数据库列表

show databases;         # 显示当前已有的数据库

查询数据字符集:

show create database 数据库名字;

2.4  删除数据库

drop database 数据库名字;

2.5  退出数据库

quit;
或者
exit;

2.6  进数据库

   use 数据库名;

2.7  创建数据库用户

create user 'admin'@'localhost' identified by 'password';

2.8  查询数据中的用户列表

select user,host from mysql.user;

2.9 删除数据库中用户

drop user'carlos'@'localhost';

2.10 授权数据库给用户

grant all on carlos_test.* to 'admin'@'localhost';
flush privileges; 

三、 表字段的操作

3.1  创建表

create table 表名(列名1 数据类型,列名2 数据类型,列名3 数据类型 );

create table list (id int,name varchar(50), passwd varchar(100) );
create table teacher(id int,name varchar(255)),charset utf8;

3.2  查询表

 show tables;    #查询当前数据库所有表
 show create table list;  #查询当前数据库list表的字符集
 desc list;  #查询当前数据库list表查询表字段信息

3.3  删除表

drop table 表名;  #删除一个表

3.4  修改表

  • (1)重命名表:alter table 旧表名 rename 新表名;
  • (2)向表中添加一列:alter table 表名 add 要添加的列名 数据类型;
  • (3)删除表中的一列:alert table 表名 drop column 被删的列名;
  • (4)修改一个列的数据类型:alter table 表名 modify column 列名 数据类型;
  • (5)重命名一个列:alter table 表名 change column 旧列名 新列名 数据类型;

四、 表数据的操作

 4.1  表数据的插入

(1)向表中全部列都插入一条记录:insert into 表名称 values (值1,值2,值3);
(2)指定列插入一条记录:insert into 表名称(列1,列3) values (值1,值3);

 4.2  表数据的修改/更新

update 表名称 set 列名称=新值 where 列=值;     #从表中更新一条记录

 4.3  表数据的查询

  • 查询表中全部列的数据记录:   select*from 表名称;
  • 查询表中指定列的数据记录:   select 列名1,列名2,列名3  from 表名称;
  • 按条件查询比较运算符>、<、 >=、<=、=、!=和<>(不等于):   select 列名称 from 表名 where 指定列 运算符 值;   
  • 按条件查询与或非:    select * from 表名称 where  列名1='值1'  and 列名2='值2';     
                                         select * from 表名称 where  列名1='值1' or 列名2='值2'; 
  • 按条件查询in(关键字):  select * from 表名称 where  列名1='值1'  in(值2,值3) ;  当查询某个字段的值为多个值的时候使用
  • 按条件查询between x and y(在两者之间):
  • 对查询结果进行排序order by:    select * from 表名称 order by 列名称;      从小到大排序
                                                          select * from 表名称 order by 列名称 desc;      从大到小排序
  • 模糊查询like:  select * from 表名 where name like "值%"
  • 分页查询limit :    在SQL的最后添加limit跳过的条数,请求的条数(每页的条数)
  • 数值计数avg、max、min、sum、count(*)

 

 

 

 

 

模糊查询格式:

  • select * from 表名 where name like "孙%"; 查询name字段以孙开头的信息
  • select * from 表名 where name like "%孙"; 查询以孙结尾
  • select * from 表名 where name like "%孙%"; 查询包含孙字
  • select * from 表名 where name like "_孙"; 查询第二位为孙字的
  • select * from 表名 where name like "_ _孙"; 查询第三位为孙字
  • select * from 表名 where name like "孙_"; 查询倒数第二位为孙字

 4.4  表数据的删除

(1)删除表中全部记录:delete  *   from 表名称:
    例:delete * from name;
(2)删除表中指定的记录:delete from 表名称 where 列名 运算符 值:
    例:delete from name where id=3;     注意字符串需要单引号  

......

posted on 2023-02-23 07:36  CARLOS_KONG  阅读(1147)  评论(0编辑  收藏  举报

导航