一、數據庫簡介
1.1 概念
管理文件的一種軟件,分為數據庫---表---行
1.2 分類
關系型數據庫:有強限制關系的MySql、Sql Server
非關系型數據庫:沒有限制關系MongGo、Redis
二、Windows安裝
2.1 exe安裝(自動)
2.2 zip包安裝(手動)
下載:網盤地址https://pan.baidu.com/s/1nwnX3nz
1)初始化:新建data文件夾、cmd:路徑\bin\mysqld --initialize-insecure 沒有空格!
遇到的錯誤:mysqld: Could not create or access the registry key needed for the MySQL application to log to the Windows EventLog. Run the application with sufficient privileges once to create the key, add the key manually, or turn off
logging for that application.
解決方法:以管理員身份運行cmd
2)運行服務器端
3)運行客戶端,以mysql -u root -p密碼為空登陸
4)可以加入環境變量方便使用
5)添加應用服務:路徑\bin\mysqld -install
刪除應用服務:路徑\bin\mysqld -remove
啟動:net start MySql
停止:net stop MySql
三、登陸
sql (-h ip地址) -u 用戶名 -p
四、語法
4.1 用戶管理
新增:create user 用戶名@登陸IP identified by 密碼; 授權:grant 權限 on 數據庫.表名 to 用戶名@登陸IP; 取消授權:revoke 權限 from 數據庫.表名 to 用戶名@登陸IP; 公司一般有DBA,專為各員工分配權限
4.2 數據庫操作
增:create database 數據庫名 default charset utf8;
刪:drop database 數據庫名;
改:
查:show databases;
4.3 表操作
增:create table 表名( 列名1 類型1, 列名2 類型2, id int not null auto_increment primary key #auto_increment自增 primary key字典值-加速查找 ) engine=innodb default charset=utf8 #innodb支持事務,原子性操作
清空:delete from 表名 #自增號繼續 truncate table 表名 #自增號從頭開始 刪:drop table 表名 改: 查:show tables;
4.4 行操作
增:insert into 表名(列1、列2) values(值1、值2); 刪:delete from 表名 where 改:update 表名 set 列1=新值 (where) 查:select 列名 from 表名
五、列支持的數據類型
5.1 數字
tinyint
int
bigint
float
double
decimal
5.2 字符串
char #固定長度,速度快
varchar #變長,節省空間
技巧:定長的列往前放
test
5.3 時間
DATETIME
5.4 其他
enum枚舉
set集合---枚舉的任意組合
六、外鍵
1、和別的表指定列的映射關系
2、創建表時:
constraint fk_別名 foreign key (本表列) references 外表名(外表列)