1. 程式人生 > 遊戲 >《英雄傳說:黎之軌跡2》官方徵募遊戲海報 獎勵很有意義

《英雄傳說:黎之軌跡2》官方徵募遊戲海報 獎勵很有意義

mysql基礎語法

一.環境搭建

  • my.ini配置檔案
[client]
# 設定mysql客戶端預設字符集
default-character-set=utf8
 
[mysqld]
# 設定3306埠
port = 3306
# 設定mysql的安裝目錄
basedir=C:\\web\\mysql-8.0.11
# 設定 mysql資料庫的資料的存放目錄,MySQL 8+ 不需要以下配置,系統自己生成即可,否則有可能報錯
# datadir=C:\\web\\sqldata
# 允許最大連線數
max_connections=20
# 服務端使用的字符集預設為8位元編碼的latin1字符集
character-set-server=utf8
# 建立新表時將使用的預設儲存引擎
default-storage-engine=INNODB
  • 初始化data目錄

mysqld --initialize-insecure

  • 忘記密碼
    • 修改my.ini
    skip-grant-tables=1
    
    • 重啟服務
    net stop mysql57
    net start mysql57
    
    • 設定新密碼
    use mysql;
    update user set authentication_string = password('新密碼'),password_last_changed = now() where user='root'
    

二.python程式碼操作mysql

import pymysql
#連線 遊標
con=pymysql.connect(host='localhost',port=3306,password='',database='table',charset='utf8')
cur=con.cursor()

# 傳送指令
sql='''
    show databases
'''
cur.execute(sql)

#獲取結果,沒有返回none
result=cur.fetchall()
print(result)

#增刪改
sql='''
    create database if not exists db1 default charset utf8 collate utf8_general_ci
'''
cur.execute(sql)
con.commit()

#關閉連線
cur.close()
con.close()

三.資料表的管理

  1. 建立資料表:
create table if not exists tbl(
   `runoob_id` int unsigned auto_increment,
   `runoob_title` varchar(16) not null,
   `runoob_author` varchar(40)  null,
   `submission_date` date,
   primary key ( runoob_id )
)engine=innodb default charset=utf8;
  1. 刪除表:
drop table tb1;
  1. 清空表:
delete from tb1; --清空表
delete from tb1 where caption='709';--刪除一行資料
  1. 修改表:

    • 新增列
    alter table 表名 add 列名 型別 not null primary key auto_increment;
    
    • 刪除列
    alter table 表名 drop column 列名;
    
    • 修改列型別
    alter table 表名 modify column 列名 型別;
    
    • 修改列型別和名稱
    alter table tb change id id int not null default 5;
    
    • 修改列預設值和刪除列預設值
    alter table 表名 alter 列名 set default 100;
    alter table 表名 alter 列名 drop default;
    
    • 新增主鍵和刪除主鍵
    alter table 表名 add primary key(列名);
    alter table 表名 drop primary key;
    

四.各種資料型別

  • 數值型
int 有符號
int unsigned 無符號
int(5)zerofill 00002
型別 大小 範圍 用途
tinyint 1B (-128-127) (0-255) 小整數
smallint 2B (-32768-32767) (0-65535) 大整數
mediumint 3B
int 4B
bigint 8B
float 4B
double 8B
decimal decimal(8,2)總位數是8,小數點後是2位。
  • 字串

    • char
    定長字串
    char(3)不管輸入多少字元長度是3個字元,超出報錯。一箇中文也是一個字元。
    最多char(255)
    
    • varchar
    變長字串
    varchar(16)最多16個字元,具體大小要看存的什麼。utf8一個漢字3個Byte
    最多varchar(65535)
    
  • 時間

    • datetime

      YYYY-MM-DD HH:MM:SS
      存多少寫多少

    • timestamp

      YYYY-MM-DD HH:MM:SS
      跟時區設定有關係

      查詢,設定時區

          mysql> show variables like '%time_zone%';
          mysql> set time_zone='+0:00';
      
    • date

      格式:YYYY-MM-DD

    • time

      格式:HH:MM:SS

五.資料行的增刪查改

  • 新增資料

    insert into 表名 (列名,列名,列名) values(對應的值,對應的值,對應的值);
    
    insert into tb values('xxx','123123'),('lyj','hehe');--表中只有兩列資料
    
  • 刪除資料

    delete from 表名;--刪除整個表的資料
    
    delete from 表名 where 條件;--按條件刪除資料行
    
  • 修改(更新)資料

    update 表名 set 列名=值;
    
    update 表名 set 列名=值 where 條件;
    
    update user set name=concat(name,'123') where id =2;--concat,可以拼接函式
    
  • 查詢資料

    select 列名,列名 as 別名,列名 from 表名 where 條件;
    
    select * from info where exists (select * from depart where id=5)--判斷條件是否存在,存在則執行前面的查詢語句
    

六.資料行查詢詳解

  1. 萬用字元
select * from info where name like '%駿';--%代指n個字元的意思
select * from info where name like '__駿';--_代表一個字元的意思
  1. 對映
select 
    id,
    name
    (select title from depart where depart.id=info.depart_id) as x1
from info;

select 
    id,
    namem
    case depart_id when 1 then '第1部門' end v1,
    case depart_id when 2 then '第2部門' else '其他' end v2,
    case when age<18 then '少年' end v3,
    case when age<18 then '少年' when age<30 then '青年' else '油膩男' end v4
from info;
  1. 排序
select * from info where id > 6 or name like '%y' order by age asc ,id desc;--asc 升序 desc 降序
  1. 取部分
select * from info limit 3 offset 2;--從第二條資料開始獲取後面的三條資料,不包含2
  1. 分組
select age ,max(id),min(id),count(depart_id) from info group by age having count(depart_id)>2;
--age相同合併為一條,其他列得有聚合函式max,min等知道取哪個值,聚合條件為having。
  1. sql執行順序美化
select
    列名,max(列名),對映
from
    表1,表2,(連表)
where
    條件
group by
    分組列
having 
    聚合條件
order by
    排序
limit
  1. 左右連表
主表 left outer join 從表 on 主表.x=從表.y;
select 
    info.id,info.name,info.email,depart.title
from
    info left outer join depart on info.depart_id=depart.id;
  1. 上下連表
select
    id,title
from
    depart
union--自動去重,如果要全部保留 union all
select
    id,name
from
    info;

七.表關係(外來鍵)

  1. 建立表的時候連線外來鍵
create table info(
    depart_id int not null,
    constraint fx_info_depart foreign key info(depart_id) references depart(id)
)default charset=uft8;
  1. 表結構已經建立好了增加外來鍵
alter table info add constraint fx_info_depart foreign key info(depart_id) references depart(id);
  1. 刪除外來鍵
alter table info drop foreign key fx_info_depart;

八.使用者授權管理

  • 建立和刪除使用者

    create user '使用者名稱'@'連線者的ip地址' identified by '密碼';
    drop user '使用者名稱' @ '連線者的ip地址';
    
  • 修改使用者

    rename user '使用者名稱'@'ip地址' to '使用者名稱'@'ip地址';
    
  • 修改密碼

    set password for '使用者名稱'@'ip地址'=Password('新密碼');
    
  • 使用者許可權

    • 授權
    grant 許可權 on 資料庫.表 to '使用者' @ 'ip地址';
    grant select,insert on *.* to 'lyj'@'localhost';
    
    • 檢視授權
    show grants for '使用者'@'ip地址';
    
    • 取消授權
    revoke 許可權 on 資料庫.表 from '使用者'@'ip地址';
    revoke ALL PRIVILEGES on *.* from 'lyj'@'localhost';
    

九.學生系統