資料庫基礎 1 (安裝與操作mariadb)

2021-09-11 07:35:23 字數 2802 閱讀 6688

為了看陽光 我來到世上

資料庫是乙個存放資料的倉庫,目前市面上最流行的資料庫大致氛圍的兩種,一種叫做關係型資料庫,一種叫做非關係型資料庫,而關係型資料庫近期流行的即為mysql,mysql原本是乙個開源的資料庫後被oracle公司收購,開始進行服務收費,因此,市面上大多數免費的類mysql資料庫為mariadb或percona兩種,centos官方元提供了mariadb版本。

因為是類mysql資料庫,因此使用方式與mysql無異

yum -y install mariadb mariadb-server
systemctl start mariadb

systemctl enable 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 '學員年齡',

*** boolean comment '學員性別');

修改表
alter table test1 modify column *** 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,***) 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';
清空全部資料,不寫日誌,不可恢復,速度極快

truncate table 表名;
清空全部資料,寫日誌,資料可恢復,速度慢

delete from 表名;

注意:

修改字段型別及名稱

如果需要修改字段型別及名稱, 你可以在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;

資料庫 1 基礎 資料庫操作

create database if not exists db name charset set charset collate collation create database if not exists db name charset set charset collate collatio...

資料庫安裝與基本操作

開啟終端,輸入sudo apt get install mysql server 再輸入sudo apt get install mysql client 啟動服務 sudo etc init.d mysql start 關閉服務 mysqladmin u root p shutdown 重啟服務 ...

1 資料庫基礎

1.資料庫是乙個以某種有組織的方式儲存的資料集合。理解資料庫的方式就是將其想象成乙個檔案櫃,此檔案櫃是乙個存放資料的物理位置 不管資料是什麼以及如何組織的。通俗 的來講資料庫就是乙個倉庫,乙個儲存資料的結合。資料庫的定義 儲存有組織的資料的容器 通常是乙個檔案或一組檔案 資料庫軟體應該被稱為dbms...