Linux環境下的Mysql資料庫入門-基於Centos系統

Jason的小世界發表於2024-08-26

關係型資料庫:
oracle ===》收錢,大型的公司
msyql ===》開源的 免費的
sql server ===>微軟

非關係型資料庫:
hbase ===>大資料‘
Redis
mangdb

下載mysql:
yum install mysql
yum install mysql-server
rpm -qa |grep -i mysql ===》檢視資料庫有沒有安裝好
service mysqld restart ===>重啟資料庫 ===>mysql後面的d代表是一個守護程序daemon
service mysqld start ====》啟動資料庫
service mysqld stop ===>關閉資料庫

mysql -uroot -p ====》u是使用者 -p 是密碼
exit ===》等同於ctrl+c 退出資料庫介面
mysqladmin -uroot password "123456" ===》修改資料庫密碼為123456

資料庫sql
show databases; ====》檢視所有的庫
create database dcs46; ===》建立一個倉庫叫做dcs46
drop database dcs46; ===》刪除dcs46這個庫
use dcs46; ===》進入dcs46這個庫
select database(); ===》檢視當前在那個庫
create table test(id int(20) primary key auto_increment,score float(20,2) not null
,name varchar(10),phone bigint(20) default 15377894561,time date);
上面建表的語句解釋
建立一個叫做test的表,表裡面有id欄位 score欄位,name欄位 phone欄位 time欄位

desc test; ===》檢視錶結構

mysql> desc test;
+-------+-------------+------+-----+-------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+-------------+----------------+
| id | int(20) | NO | PRI | NULL | auto_increment |
| score | float(20,2) | NO | | NULL | |
| name | varchar(10) | YES | | NULL | |
| phone | bigint(20) | YES | | 15377894561 | |
| time | date | YES | | NULL | |
+-------+-------------+------+-----+-------------+----------------+

Field ==》欄位
Type ===>資料型別
Null ===》是否可以為空
Key ===》primary key 主鍵
Default ===》預設值
Extra ==》額外的備註

資料庫中常用的資料型別:
數值型:
int ===》整數 最大的儲存值為:2147483647
bigint ====>可以儲存超過2147483647這個數字
float ===》浮點型

文字型:【varchar型別和char型別存的資料需要加上雙引號或單引號】
varchar ===>存字串(不管是中文還是英文 都佔2個位元組 空格也是一個字元)
char ====》存字串(英文佔1個位元組,中文佔2兩個位元組 空格不算字元只能算佔位符)

日期型:【日期型的資料也需要加上單引號或者雙引號】
date ===>"2024-08-23"

資料庫中的約束有哪些?
not null ===>非空約束
primary key ===>主鍵約束,裡面的資料不能重複必須是唯一的
default ====>預設值約束 不填寫就用預設值
auto_increment ===>自增長約束 1 ====》1+1 前面四個約束是對錶資料的約束
foreign key ===>外來鍵約束 對錶和表之間的約束

對錶結構的操作:alter table +表名
show tables; ===》檢視當前庫下有哪些表
alter table test1 rename dcs111; ===》將test1這個表的名字改為dcs111
alter table test change id sid int(10); ===>將test表中的id欄位改為sid 並去掉自增長
alter table test change sid id int(10) auto_increment; ===>將test表中的sid欄位改為id 並加上自增長
alter table test add class int(10); ===》加上一個class欄位(預設放在最後面)
alter table test add sex int(10) first; =》加上一個sex欄位放在最前面
alter table test add sex1 int(10) after id;
=》新增一個sex1欄位放在id欄位後面
alter table test modify class int(10) after id; ==》將已經存在的class欄位移動到id欄位後面
alter table test add(age1 int(10),age2 int(10)); ==》同時新增兩個欄位
alter table test drop sex1,drop age1,drop age2; ===》刪除欄位
drop table test; ==》刪除test表

插入資料:
insert into 表
insert into test(id,score,name,phone,time)values(1,88.88,"zhangsan",17326056700,"2024-08-24");
insert into test values(2,98.02,"lisi",17326056800,'2024-08-25'); =》第二種插入資料的方式
insert into test(id,score,name,phone,time)values(3,78.00,"zhaoliu",17326056900,"2024-08-23"),(4,66.66,'xiaozhou',17326056400,"2024-01-02");
===》批次插入資料
insert into test(score,name,time)values(88.88,"zhangsan","2024-08-24"); ===》給指定欄位插入資料

0不等於null ,null指的是一個空的屬性,0代表的是一個值
面試題:給你一張表裡面的一個欄位怎麼測試?
資料型別
約束型別
儲存值的邊界值

刪除:delete from +表名
delete from test; ===》刪除表中資料
delete from test where id=10; ==》刪除指定資料,刪除id為10的這條資料
truncate test; ====》快速刪除表中資料
drop table test ===》刪除test這張表

truncate,delete from,drop的區別
truncate:快速刪除表中所有的資料,適用於刪除大量的資料
delete from ===》根據條件刪除資料,如果不接where會刪除表中所有的資料,但是執行的速度沒有truncate快
drop ====>刪除表和表結構

修改表中資料:update
update student set math=81 where id=10; ===》修改id欄位為10 的這個人的分數為81

查的sql:
1.不等於
select * from student where id<>2; ===>查詢id不等於2的所有資訊
select * from student where id!=2; ===>查詢id不等於2的所有資訊
2.同時滿足多個條件,至少滿足一個條件
select * from student where age>24 and age<31; ===》查詢年齡大於24.小於31歲的資訊
select * from student where age=28 or age=27; ===》查詢年齡等於28或者年齡等於27的資訊
3.包含和不包含
select * from student where class in (1833,1835); ==》查詢班級為1833或1835的資訊
select * from student where class not in (1833,1835); ==》查詢班級不是1833或1835的資訊
4.在.....之間 between的值包含自身
select * from student where age between 24 and 31; ==》查詢年齡在24-31之間的資訊
5.為空 不為空
select * from student where class is null; ==》查詢班級為空的
select * from student where class is not null; ==》查詢班級不為空的
6.查詢指定行數
select * from student limit 5; ===》查詢前五行的內容 等同於 limit 0,5
select * from student limit 2,5; ===》查詢3-7行的資料 3-1=2 7-2=5
7.模糊匹配
select * from student where name like "xi%"; ===>查詢姓名以xi開頭的資訊
select * from student where name like "%qi"; ==》查詢姓名以qi結尾的資訊
select * from student where name like "%ao%";-
=》姓名中帶有ao的資訊
select * from student where name like "xiaoli_"; ===》_表示一個字元
8.排序
select * from student order by math; ===》order by 預設是升序排序
select * from student order by math desc; ===》desc是降序排序
select * from student order by math asc; =====》acs是升序排序

9.分組 group by +欄位 ==> 分組函式 group by
select count(),class from student group by class; ====>求每個班級的人數
select count(
) as "班級",class as "人數" from student group by class; ===》給查詢出來的欄位取別名
select count(*) a,class b from student group by class; ===》給查詢出來的欄位取別名

select * from (select max(math)a from student )b where a>90;

常用的聚合函式:
count(*) 統計
sum(math) 求和
avg(math) 求平均
max(math) 求最大值
min(math) 求最小值
distinct(math) 去重

注意點:group by
1.分組函式 group by 只能和聚合函式一起使用,還有分組的欄位
2.where後面可以接group by 但是group by後面不能接where
3.group by 前面加where是為了先過濾在分組,where條件中不能包含聚合函式 統計,求和.....
4.where後面接group by 是為了先過濾在分組,having是跟group by連在一起用的,放在group by的後面,此時having的作用是先分組在過濾

1.求出每個班級數學成績大於80分的人數
select class,count(*) from student where math>80 group by class;

2.求出每個班級性別為1的數學總成績
select class,sum(math) from student where sex=1 group by class;

3.求出每個班數學總成績大於200的班級和成績資訊
select class,sum(math) from student group by class having sum(math)>200;

相關文章