首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 建立部落格資料庫結構(建表 SQL)

     編輯:PHP 綜合知識

資料表設計

包含五個核心資料表:站點資訊、文章、會員、分類、留言板。每個函式負責建立對應資料表,先 DROP IF EXISTS 再 CREATE。

核心程式碼

<?php
function create_siteinfo() {
    global $conn;
    $sql = "create table siteinfo (
        title varchar(100) not null,
        keyword varchar(200) not null,
        description varchar(200) not null
    )";
    $dropsql = "drop table if exists siteinfo";
    mysql_query($dropsql, $conn) or die("刪除 siteinfo 失敗!");
    mysql_query($sql, $conn) or die("建立 siteinfo 失敗!");
}

function create_article() {
    global $conn, $table_article;
    $sql = "create table $table_article (
        id int(11) auto_increment not null,
        cid int(5) not null,
        abstract varchar(300) not null,
        title varchar(50) not null,
        posttime datetime not null,
        author varchar(30) not null,
        comefrom varchar(50) not null,
        comeurl varchar(50) not null,
        content TEXT not null,
        keyword varchar(20) not null,
        rank int(2) not null,
        views int(5) not null,
        PRIMARY KEY(id)
    )";
    $dropsql = "drop table if exists $table_article";
    mysql_query($dropsql, $conn) or die("刪除失敗!");
    mysql_query($sql, $conn) or die("建立失敗!");
}

function create_member() {
    global $conn, $table_member;
    $sql = "create table $table_member (
        uid int(5) auto_increment not null,
        u_name varchar(20) not null UNIQUE,
        u_passwd varchar(100) not null,
        rank int(2) not null,
        PRIMARY KEY(uid)
    )";
    $dropsql = "drop table if exists $table_member";
    mysql_query($dropsql, $conn) or die("刪除失敗!");
    mysql_query($sql, $conn) or die("建立失敗!");
}

function create_class() {
    global $conn, $table_class;
    $sql = "create table $table_class (
        cid int(5) auto_increment not null,
        cname varchar(50) not null UNIQUE,
        PRIMARY KEY(cid)
    )";
    $dropsql = "drop table if exists $table_class";
    mysql_query($dropsql, $conn) or die("刪除 $table_class 失敗!");
    mysql_query($sql, $conn) or die("建立 $table_class 失敗");
}

function create_guest() {
    global $conn, $table_guest;
    $sql = "create table $table_guest (
        gid int(11) auto_increment not null,
        g_name varchar(50) not null,
        g_site varchar(50) not null,
        g_mail varchar(50) not null,
        g_cid int(5) not null,
        PRIMARY KEY(gid)
    )";
    $dropsql = "drop table if exists $table_guest";
    mysql_query($dropsql, $conn) or die("刪除 $table_guest 失敗");
    mysql_query($sql, $conn) or die("建立 $table_guest 失敗");
}

function create_sql() {
    global $table_article, $table_member, $table_class, $table_guest, $conn;
    echo "建立 siteinfo 表……"; create_siteinfo(); echo "完成<br>";
    echo "建立 $table_article 表……"; create_article(); echo "完成<br>";
    echo "建立 $table_member 表……"; create_member(); echo "完成<br>";
    echo "建立 $table_class 表……"; create_class(); echo "完成<br>";
    echo "建立 $table_guest 表……"; create_guest(); echo "完成<br>";
    mysql_close($conn);
}
?>

注意

  • 使用已棄用 mysql_* 函數,實際專案請改用 mysqli 或 PDO。
  • 資料表名稱透過全域變數傳入,便於前綴管理。
  • 欄位型別與長度依專案需求調整。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved