蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 PHP 8.4 MyGuests 建表、NOT NULL/主键约束与幂等 DDL 实验

PHPWindows x64、PHP 8.4 本地 CLI 环境。explore-mysql-create-table.php
explore-mysql-create-table.php117 行
<?php
declare(strict_types=1);

$cases = [];

$conn = new PDO('sqlite::memory:', null, null, [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

// Case 1: CREATE TABLE MyGuests with 5 columns and verify schema metadata
$sql = <<<'SQL'
CREATE TABLE MyGuests (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    firstname VARCHAR(30) NOT NULL,
    lastname VARCHAR(30) NOT NULL,
    email VARCHAR(50),
    reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
SQL;
$execRet = $conn->exec($sql);
$cols = $conn->query('PRAGMA table_info(MyGuests)')->fetchAll(PDO::FETCH_ASSOC);
$colSummary = [];
foreach ($cols as $c) {
    $colSummary[$c['name']] = [
        'notnull' => (int)$c['notnull'],
        'pk' => (int)$c['pk'],
        'has_default' => $c['dflt_value'] !== null,
    ];
}

$actual1 = [
    'exec_ret' => $execRet,
    'column_count' => count($cols),
    'columns' => $colSummary,
];
$expected1 = [
    'exec_ret' => 0,
    'column_count' => 5,
    'columns' => [
        'id' => ['notnull' => 0, 'pk' => 1, 'has_default' => false],
        'firstname' => ['notnull' => 1, 'pk' => 0, 'has_default' => false],
        'lastname' => ['notnull' => 1, 'pk' => 0, 'has_default' => false],
        'email' => ['notnull' => 0, 'pk' => 0, 'has_default' => false],
        'reg_date' => ['notnull' => 0, 'pk' => 0, 'has_default' => true],
    ],
];
$cases[] = [
    'name' => 'create_myguests_table_schema',
    'passed' => $actual1 === $expected1,
    'expected' => $expected1,
    'actual' => $actual1,
];

// Case 2: Verify NOT NULL, PRIMARY KEY uniqueness, AUTOINCREMENT, and DEFAULT timestamp
$conn->exec("INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')");
$row = $conn->query('SELECT id, firstname, lastname, email, reg_date FROM MyGuests WHERE id = 1')->fetch(PDO::FETCH_ASSOC);

$notNullRejected = false;
try {
    $conn->exec("INSERT INTO MyGuests (firstname, lastname) VALUES (NULL, 'Doe')");
} catch (PDOException $e) {
    $notNullRejected = str_contains($e->getMessage(), 'NOT NULL');
}

$dupPkRejected = false;
try {
    $conn->exec("INSERT INTO MyGuests (id, firstname, lastname) VALUES (1, 'Jane', 'Doe')");
} catch (PDOException $e) {
    $dupPkRejected = str_contains($e->getMessage(), 'UNIQUE');
}

$actual2 = [
    'auto_id' => (int)$row['id'],
    'reg_date_populated' => !empty($row['reg_date']),
    'not_null_rejected' => $notNullRejected,
    'dup_pk_rejected' => $dupPkRejected,
];
$expected2 = [
    'auto_id' => 1,
    'reg_date_populated' => true,
    'not_null_rejected' => true,
    'dup_pk_rejected' => true,
];
$cases[] = [
    'name' => 'enforce_not_null_and_pk_constraints',
    'passed' => $actual2 === $expected2,
    'expected' => $expected2,
    'actual' => $actual2,
];

// Case 3: Duplicate CREATE TABLE error vs IF NOT EXISTS idempotency
$dupTableRejected = false;
try {
    $conn->exec('CREATE TABLE MyGuests (id INTEGER PRIMARY KEY)');
} catch (PDOException $e) {
    $dupTableRejected = str_contains($e->getMessage(), 'already exists');
}
$idempotentRet = $conn->exec('CREATE TABLE IF NOT EXISTS MyGuests (id INTEGER PRIMARY KEY)');
$conn = null;

$actual3 = [
    'duplicate_create_throws' => $dupTableRejected,
    'if_not_exists_succeeds' => $idempotentRet === 0,
];
$expected3 = [
    'duplicate_create_throws' => true,
    'if_not_exists_succeeds' => true,
];
$cases[] = [
    'name' => 'ddl_idempotency_and_mysql_syntax',
    'passed' => $actual3 === $expected3,
    'expected' => $expected3,
    'actual' => $actual3,
];

echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
本例需在 Windows x64、PHP 8.4 本地 CLI 环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、PHP 8.4 本地 CLI 环境。

入口文件:projects/php-mysql-create-table-lab/explore-mysql-create-table.php

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-mysql-create-table.php,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:PHP

课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip

下载课程关联工程包:PHP

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1266.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习