1. 程式人生 > 資料庫 >mysql基礎01

mysql基礎01

# 2020.12.06

## 資料庫01
1. 安裝 mysqld(服務端) mysql(客戶端)sql語句(通訊交流) DBMS
2. 關係型資料庫 非關係型資料庫 //瞭解
3. 資料庫 資料表 資料行 的增刪改查
```
資料庫:
//建立資料庫
create database db1;
//刪除資料庫
drop database db1;
//檢視資料庫
show database
//使用/進入資料庫
use database

資料表:
//建立資料表
create table tb1(
id int not null
)

資料行:

//增
insert into tb1(name, age) value('wang', 18);

//刪
delete from tb1;
truncate table tb1;
delete from tb1 where id > 10

//改
update tb1 set name='root' where id > 10

//查
select * from tb1;
select id,name from tb1;
```
3. 非空 not null
4. 主鍵 primary key 提高查詢效率/唯一性
5. 自增 auto_increment
6. 外來鍵約束 constraint 表1 foreign key (表1列名) references 表2 (表2列名)
7. 回滾事物 engine=innodb
8. 設定編碼格式 default charset = utf8
9. 設定每列的預設值 default null
10. 資料型別
```
數字型別: int longint text
字串 char(固定字串的長度)、varchar //建立表的時候應該把char型別(固定長度)的放在前面,把varchar型別的放在後面,這樣查詢的時候可以提高效率
是否為空:null/not null
時間:datatime
列舉
set
```
11. 注意點
```
一個表只能有一個主鍵
一個主鍵可以有多列(primary key(列1, 列2,列3, ... ))
為主鍵的列不能為空(not null)
```
12. 參考
https://www.cnblogs.com/wupeiqi/articles/5713315.html