資料庫了解
- 概念
- 資料庫就是一種特殊的檔案,其中存盤著需要的資料
- 一個資料庫可以有多張表
- MySQL是一種關系型資料庫
- 具有關聯性資料的就是關系型資料庫
- MySQL是一種軟體可以用來創建mysql資料庫
- MySQL也是C/S構架(底層TCP)
- MySQL客戶端
- 客戶端連接服務端使用TCP協議連接
- 使用時SQL陳述句操作
- MySQL服務器
- MySQL服務器操作資料庫
- MySQL客戶端
- 資料庫優點
- 持久化存盤
- 讀寫速度高
- 保存速度有有效性
- 對程式支持非常好,容易擴展
- 資料庫詞匯
- 列:欄位
- 行:記錄
- 表:記錄的集合
- 主鍵:唯一標記一行記錄的
- 外鍵:對于一張表中某個欄位的值是另一張表的主鍵的值
- 常見的關系型資料庫及底層了解
- RDBMS
- 是一種程式的簡稱,分為關系型資料庫和非關系型資料庫
- 關系型資料庫
- MySQL(常用與制作網站)
- 常用版本:5.1 5.6 5.7 5.8
- sqlite
- Oracle
- 常用版本 Oracle 10g Oracle 11g
- sqlserver(Microsoft)
- db2(IBM)
- MySQL(常用與制作網站)
- 非關系型資料庫
- redis(處理快取)
- mangodb(存盤非關系型資料)
- RDBMS
SQL語言概念
- SQL陳述句是一種結構化查詢陳述句,可以用來操作RDBMS資料庫語言
- sql陳述句分類
- DQL查詢
- select
- DML操作
- insert update delete
- DQL查詢
MySQL基礎操作
- 安裝
- ubuntu:
- sudo apt-get install mysql-server(服務端)
- sudo apt-get install mysql-client(客戶端)
- ubuntu:
- 啟動
- sudo service mysql start
- 暫停
- sudo service mysql stop
- 重啟
- sudo service mysql restart
- 組態檔
- /etc/mysql/mysql.conf.d/mysqld.cnf
MySQL的主要資料型別和約束
資料型別
-
整數型
- int
- bit
-
小數
- decimal
- decimal(5, 2),表示一共存5位數,小數占2位
- decimal
-
字串
- varchar
- char
- char(3)表示固定長度的字串,長度不夠會用空格補全,不可以超過3個字符
- varchar
- varchar(3)填充ab就會存盤ab,不可以超過3個字符
-
時間型別
- data, time, datatime
-
列舉型別
- enum
- 比如性別,代表只有固定的型別讓人選擇
- enum
-
test型別
- 當字符大于4000之后推薦使用
-
ps:
- 對于圖片,音頻,視頻等檔案,不存盤在資料庫中,而是上傳到服務器中,資料庫只保存檔案的保存路徑
約束
-
約束是用來限制每一個欄位的
-
主鍵primary key
- 物理上存盤的順序
-
非空not null
- 此欄位不允許為空
-
外鍵
-
數值型別
- tinyint 1個位元組,0-255
- smallint 2個位元組 0-65535
- mediumint 3個位元組
- int/integer 4個位元組
- bigint 8個位元組
-
字串
- char
- barchar
- text
用命令來操作資料庫
資料庫連接
- mysql -uroot -pmysql
- mysql -uroot -p (這種方式需要寫密碼)
退出資料庫
- quit/exit/ctrl+d
查看資料庫
- show databases;
顯示時間
- select now();
顯示版本
- select version();
創建資料庫
- create database python;(創建一個名稱為Python的資料庫)
- create database pythonnew charset=utf8;(創建一個字符集為utf8編碼名稱為pythonnew的資料庫)
洗掉資料庫
- drop database python;(洗掉名稱為Python的資料庫)
使用資料庫
- use 資料庫名稱
查看當前使用資料庫
- select database();
資料表操作
- 顯示資料庫所有的表
- show tables;
- 創建一個資料表
- create table xxxxx(id int, name varcharm(30)); # 一個逗號創建一個欄位
- create table xxxxx(id int primary key not null auto_increment, name varcharm(30)); # 添加約束在每一個欄位后面加上約束
- 案例創建一個students表(id,name,age,high,gender,cls_id)
create table students(
id int unsigned not null auto_increment parimary key,
name varchar(30),
age tinyint unsigned defult 0,
high decimal(5,2),
gender enum("男","女") default "男",
cls_id int unsigned
-
ps:
- 創建表順序(欄位名 型別 約束)
-
desc 資料表的名字;(可以快速查看表結構)
資料表欄位結構的的增刪減查
- 增
- alter table students add birthday datetime;
- alter table 表名
- 刪
- drop table 表名
- 改
- alter table 表名 modify brithday data; 不重名
- alter table 表名 change brithday birth date default '1990-01-01'; 重名
- 查
ps:
show create table students; # 可以查看創建這個資料表的SQL陳述句
資料的增刪改查
- 增:
- insert into 表名 values(....)
- insert into 表名 values(....),(....)
- insert into 表名(欄位,欄位) values (值1, 值2)
- insert into 表名(字段,欄位) values (值1, 值2),(值1,值2)
- ps:
- 列舉中數字1代表
- 改:
- update 表名 set 欄位=值 (直接修改全部欄位)
- update 表名 set 欄位=值 where name=值 (在滿足where后面的條件,一般用主鍵來判斷來指定修改)
- update 表名 set 欄位=值,欄位2=值 where name=值 (一次修改多個值)
- 查詢
- select 指定欄位 from 表名 (*代表所有的欄位)
- select 指定欄位 as 命名 from 表名 (可以修改欄位的名稱顯示出來)
- 刪
- truncate students;(清空表)
- delete from 表名; (洗掉表的所有內容)
- delete from 表名 where id<6; (指定洗掉)
- 不要去洗掉資料,可以添加欄位來邏輯洗掉
- alter table students add is_delete bit default 0;
- update students set is_delete=1 where id=6;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/89045.html
標籤:MySQL
上一篇:MySQL基礎篇(03):系統和自定義函式總結,觸發器使用詳解
下一篇:mysql累加、累減
