MySQL -- 資料庫,表相關操作
阿新 • • 發佈:2021-01-01
MySQL – 資料庫,表相關操作
開啟、關閉MySQL服務:
net start mysql
net stop mysql
刪除 mysql 服務:
sc delete MySql
連線MySQL:
# 最高許可權使用者:root
mysql -u USER_NAME -p
# 進入資料庫同時選擇使用指定資料庫
mysql -u USER_NAME -p DATABASE_NAME
一)User
-
檢視使用者
# 檢視所有使用者 select host,user,authentication_string from mysql.user; # 檢視使用者所有許可權 show grants;
-
新建使用者
create user 'username'@'host' identified by 'password'; mysql> create user 'virgo'@'localhost' identified by 'virgo123';
-
修改使用者
# 修改許可權 grant privileges on DATABASE_NAME.TABLE_NAME to 'username'@'host' identified by 'password'; # PS: # priveleges(許可權列表), 可以是all privileges[所有許可權],也可以是select、update、delete、insert等許可權,多個許可權的名詞, 相互之間用逗號分開。 # identified by 指定使用者的登入密碼,該項可以省略。 # 修改密碼 alter user 'root'@'localhost' identified with mysql_native_password by 'new_password';
-
刪除使用者
drop user 'username'@'host';
二)Databases
-
檢視資料庫
# 顯示所有資料庫 show databases; # 顯示某一資料庫建立資訊 show create database DATABASE_NAME; # 進入或切換某資料庫 # PS: use 這個命令後面的分號可以省略,這是SQL語句中唯一可以省略分號的語句 use DATABASE_NAME use DATABASE_NAME; # 顯示當前資料庫, 檢視當前處於哪個資料庫中 select database();
-
建立資料庫
create database DATABASE_NAME;[資料庫名] # 1. 使用預設編碼方式 latin1 (單位元組編碼) create database DATABASE_NAME; # 2. 自定義編碼方式:如 utf-8,... # PS: character set 可以縮寫成 charset,效果是一樣的。 create database DATABASE_NAME character set utf8;[沒有‘-’] create database DATABASE_NAME charset utf8;
-
修改資料庫屬性
# 修改資料庫編碼方式 alter database DATABASE_NAME character set utf8;[要修改成的型別]
-
刪除資料庫
drop database [if exists] DATABESE_NAME;
三)Tables(att: 欄位名, TYPE: 欄位型別)
-
檢視資料表
# 顯示當前資料庫所有表 show tables; # 顯示某表資訊 show create table TABLE_NAME;[可以顯示錶的欄位資訊,MySQL引擎,預設字元編碼等資訊] desc TABLE_NAME;[以規範的表格形式顯示欄位屬性]
-
建立資料表
# 建立表,及其一些欄位 create table TABLE_NAME(att1 TYPE, att2 TYPE, ...); # 建立帶主鍵,自增,唯一鍵表 create table Student(id INT auto_increment primary key, name VARCHAR(10) not null unique, age int default 10, grade VARCHAR(5));
-
修改資料表
(a) 表的屬性
# 主鍵不能為空 唯一鍵可以為空 # 主鍵只能有一個 唯一鍵可以有多個 # 主鍵可以多個列和並起來作為一個 唯一鍵不行
# 設定主鍵 alter table TABLE_NAME add primary key(att); # 刪除主鍵 alter table TABLE_NAME drop primary key; # 設定唯一鍵 alter table TABLE_NAME add unique(att); # 設定自增列 alter table TABLE_NAME change id id smallint auto_increment; # 取消自增 alter table TABLE_NAME modify column id INT;
(b) 欄位屬性
# 增加欄位 alter table TABLE_NAME add att TYPE; # 刪除欄位 alter table TABLE_NAME drop att; # 修改欄位型別 alter table TABLE_NAME modify att NEW_TYPE; # 修改欄位名(可同時修改其型別) alter table TABLE_NAME change att new_att TYPE;
-
刪除資料表
# 刪除具有主外來鍵關係的表時,要先刪子表(從表),後刪主表 drop table TABLE_NAME;
四)資料增刪改查(Table: Student)
-
增加資料
# 插入完整的一行 insert into Student(name, age, grade) values ('小明', 11, '五年級'); [按指定列名順序插入,如省略列名,則插入值必須按資料表列名順序排列] # 插入多行 insert into Student(name, age, grade) values ('小紅', 10, '四年級'), ('小王', 8, '二年級');
-
檢視資料
# 檢視所有 select * from Student; # 按條件檢視 select * from Student where age=10; # 僅顯示某一或幾列; select name from Student where age<12;
-
修改資料
# update TABLE_NAME set att=value [condition]; update Student set age=12 where id=2;
-
刪除資料
# 刪除物件並不是表或者列,而是行。 # delete from TABLE_NAME condition;[僅where] delete from Student where age>12;
-
組合操作
# 插入檢索出的資料 insert into Student(name, age, grade) select name, age, grade from Others;[另一張表]
五)MySQL 常用資料型別
-
整數
資料型別 資料範圍 TINYINT -128 – 127 SMALLINT -32768 – 32767 MEDIUMINT -2^23 – 2^23-1 INT -2^31 – 2^31-1 BIGINT -2^63 – 2^63-1 -
字串
資料型別 位元組範圍 用途 CHAR(n) 0 – 255位元組 定長字串 VARCHAR(n) 0 – 65535位元組 變長字串 TEXT 0 – 65535位元組 長文字資料 LONGTEXT 0 – 2^32-1位元組 極大文字資料 BLOB 0 – 65535位元組 二進位制長文字資料 LONGBLOB 0 – 2^32-1位元組 二進位制極大文字資料 -
浮點
資料型別 資料用法 資料範圍 Float Float(m,n) 7位有效數 Double Double(m,n) 15位有效數 Decimal Decimal(m,n) 28位有效數 -
時間
資料型別 格式 用途 DATE YYYY-MM-DD 日期 TIME HH:MM:SS 時間 YEAR YYYY 年份 DATETIME YYYY-MM-DD HH:MM:SS 日期和時間 TIMESTAMP 10位或13位整數(秒數) 時間戳 -
列舉
enum(列舉值1,列舉值2,…)