1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | 設定資料庫 如何在mysql建立一個叫做`scrap_wheel_cake`的DB 建立新的使用者帳號`db_user`,並設定密碼,授予使用者帳號對於新的資料庫的權限(ip設定為%) 並建立以下資料表 1. 'city', 欄位有id和name, create_at, update_at, name欄位有unique 2. 'area', 欄位有 id, name, city_id, city_id有對‘city'的foreign_key, name和city_id有unique 3. 'shop', 欄位有id, name, city_id, area_id, address, lat, lng , city_id有對‘city'的foreign_key, carea_id有對‘area'的foreign_key, forieign key 都設定成 CASCADE 幫我產生一個可以直接貼到phpmyadmin執行的script檔案 你可以使用以下的 SQL 語句,在 MySQL 中建立名為 scrap_wheel_cake 的資料庫,以及相對應的使用者帳號、資料表和外鍵約束。 請將 <your_password> 替換為你想要設定的密碼。 ```sql -- 建立資料庫 CREATE DATABASE scrap_wheel_cake; -- 建立使用者帳號並授予資料庫權限 CREATE USER 'db_user'@'%' IDENTIFIED BY '<your_password>'; GRANT ALL PRIVILEGES ON scrap_wheel_cake.* TO 'db_user'@'%'; -- 使用 scrap_wheel_cake 資料庫 USE scrap_wheel_cake; -- 建立 city 資料表 CREATE TABLE city ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) UNIQUE, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); -- 建立 area 資料表 CREATE TABLE area ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), city_id INT, UNIQUE KEY name_city_id_unique (name, city_id), FOREIGN KEY (city_id) REFERENCES city(id) ON DELETE CASCADE ); -- 建立 shop 資料表 CREATE TABLE shop ( id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), city_id INT, area_id INT, address VARCHAR(255), lat DECIMAL(9,6), lng DECIMAL(9,6), FOREIGN KEY (city_id) REFERENCES city(id) ON DELETE CASCADE, FOREIGN KEY (area_id) REFERENCES area(id) ON DELETE CASCADE ); ``` 你可以將上述 SQL 語句保存為 .sql 檔案,然後將該檔案上傳到 phpMyAdmin,並執行該檔案以建立資料庫、使用者和資料表。請確保你有足夠的權限來執行這些操作。 請注意,這只是一個範例,你可能需要根據實際需求進行修改。 |
Direct link: https://paste.plurk.com/show/PYqc7xoduPRDeva1V7Yl