1. 程式人生 > 其它 >MySQL -- 資料庫,表相關操作

MySQL -- 資料庫,表相關操作

技術標籤:MySqlmysql資料庫sql

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

  1. 檢視使用者

    # 檢視所有使用者
    select host,user,authentication_string from mysql.user;
    
    # 檢視使用者所有許可權
    show grants;
    

    在這裡插入圖片描述

  2. 新建使用者

    create user 'username'@'host' identified by 'password';
    
    mysql> create user 'virgo'@'localhost' identified by 'virgo123';
    
  3. 修改使用者

    # 修改許可權
    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';
    

    在這裡插入圖片描述

  4. 刪除使用者

    drop user 'username'@'host';
    

二)Databases

  1. 檢視資料庫

    # 顯示所有資料庫
    show databases;
    
    # 顯示某一資料庫建立資訊
    show create database DATABASE_NAME;
    
    # 進入或切換某資料庫
    # PS: use 這個命令後面的分號可以省略,這是SQL語句中唯一可以省略分號的語句
    use DATABASE_NAME
    use DATABASE_NAME;
    
    # 顯示當前資料庫, 檢視當前處於哪個資料庫中
    select database();
    
  2. 建立資料庫

    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;
    
  3. 修改資料庫屬性

    # 修改資料庫編碼方式
    alter database DATABASE_NAME character set utf8;[要修改成的型別]
    
  4. 刪除資料庫

    drop database [if exists] DATABESE_NAME;
    

三)Tables(att: 欄位名, TYPE: 欄位型別)

  1. 檢視資料表

    # 顯示當前資料庫所有表
    show tables;
    
    # 顯示某表資訊
    show create table TABLE_NAME;[可以顯示錶的欄位資訊,MySQL引擎,預設字元編碼等資訊]
    desc TABLE_NAME;[以規範的表格形式顯示欄位屬性]
    

    在這裡插入圖片描述

  2. 建立資料表

    # 建立表,及其一些欄位
    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));
    

    在這裡插入圖片描述

  3. 修改資料表

    (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;
    
  4. 刪除資料表

    # 刪除具有主外來鍵關係的表時,要先刪子表(從表),後刪主表
    drop table TABLE_NAME;
    

四)資料增刪改查(Table: Student)

在這裡插入圖片描述

  1. 增加資料

    # 插入完整的一行
    insert into Student(name, age, grade) values ('小明', 11, '五年級'); [按指定列名順序插入,如省略列名,則插入值必須按資料表列名順序排列]
    
    # 插入多行
    insert into Student(name, age, grade) values ('小紅', 10, '四年級'), ('小王', 8, '二年級');
    

    在這裡插入圖片描述

  2. 檢視資料

    # 檢視所有
    select * from Student;
    
    # 按條件檢視
    select * from Student where age=10;
    
    # 僅顯示某一或幾列;
    select name from Student where age<12;
    

    在這裡插入圖片描述
    在這裡插入圖片描述
    在這裡插入圖片描述

  3. 修改資料

    # update TABLE_NAME set att=value [condition];
    update Student set age=12 where id=2;
    

    在這裡插入圖片描述

  4. 刪除資料

    # 刪除物件並不是表或者列,而是行。
    # delete from TABLE_NAME condition;[僅where]
    delete from Student where age>12;
    
  5. 組合操作

    # 插入檢索出的資料
    insert into Student(name, age, grade) 
    select name, age, grade
    from Others;[另一張表]
    

五)MySQL 常用資料型別

  1. 整數

    資料型別資料範圍
    TINYINT-128 – 127
    SMALLINT-32768 – 32767
    MEDIUMINT-2^23 – 2^23-1
    INT-2^31 – 2^31-1
    BIGINT-2^63 – 2^63-1
  2. 字串

    資料型別位元組範圍用途
    CHAR(n)0 – 255位元組定長字串
    VARCHAR(n)0 – 65535位元組變長字串
    TEXT0 – 65535位元組長文字資料
    LONGTEXT0 – 2^32-1位元組極大文字資料
    BLOB0 – 65535位元組二進位制長文字資料
    LONGBLOB0 – 2^32-1位元組二進位制極大文字資料
  3. 浮點

    資料型別資料用法資料範圍
    FloatFloat(m,n)7位有效數
    DoubleDouble(m,n)15位有效數
    DecimalDecimal(m,n)28位有效數
  4. 時間

    資料型別格式用途
    DATEYYYY-MM-DD日期
    TIMEHH:MM:SS時間
    YEARYYYY年份
    DATETIMEYYYY-MM-DD HH:MM:SS日期和時間
    TIMESTAMP10位或13位整數(秒數)時間戳
  5. 列舉
    enum(列舉值1,列舉值2,…)