{"id":138,"date":"2021-03-08T00:28:35","date_gmt":"2021-03-08T00:28:35","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=138"},"modified":"2021-07-08T04:13:00","modified_gmt":"2021-07-08T04:13:00","slug":"pdo-creating-new-tables","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-pdo\/pdo-creating-new-tables\/","title":{"rendered":"Creating New Tables"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you wil learn how to create new tables in the database using PDO.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='sql-statements-for-creating-new-tables'>SQL statements for creating new tables <a href=\"#sql-statements-for-creating-new-tables\" class=\"anchor\" id=\"sql-statements-for-creating-new-tables\" title=\"Anchor for SQL statements for creating new tables\">#<\/a><\/h2>\n\n\n\n<p>To create a new table in a database, you use the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-create-table\/\">CREATE TABLE<\/a> statement. For example, the following <code>CREATE TABLE<\/code> statement creates the <code>authors<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-keyword\">authors<\/span>( \n  author_id   <span class=\"hljs-built_in\">INT<\/span> AUTO_INCREMENT,\n  first_name  <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>, \n  middle_name <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">50<\/span>) <span class=\"hljs-literal\">NULL<\/span>, \n  last_name   <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>) <span class=\"hljs-literal\">NULL<\/span>,\n  PRIMARY <span class=\"hljs-keyword\">KEY<\/span>(author_id)\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And the following <code>CREATE TABLE<\/code> statement creates the <code>book_authors<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> book_authors (\n  book_id   <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>, \n  author_id <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>, \n  PRIMARY <span class=\"hljs-keyword\">KEY<\/span>(book_id, author_id), \n  <span class=\"hljs-keyword\">CONSTRAINT<\/span> fk_book \n    <span class=\"hljs-keyword\">FOREIGN<\/span> <span class=\"hljs-keyword\">KEY<\/span>(book_id) \n    <span class=\"hljs-keyword\">REFERENCES<\/span> books(book_id) <span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">CASCADE<\/span>, \n  <span class=\"hljs-keyword\">CONSTRAINT<\/span> fk_author \n    <span class=\"hljs-keyword\">FOREIGN<\/span> <span class=\"hljs-keyword\">KEY<\/span>(author_id) \n    <span class=\"hljs-keyword\">REFERENCES<\/span> <span class=\"hljs-keyword\">authors<\/span>(author_id) <span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">CASCADE<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"note\">Note that the <code>book_authors<\/code> table links to the <code>books<\/code> created from the <a href=\"https:\/\/phptutorial.net\/php-pdo\/php-pdo-mysql\/\">previous tutorial<\/a>.<\/p>\n\n\n\n<p>Typically, you execute these <code>CREATE TABLE<\/code> statements using a MySQL client tool to create the <code>authors<\/code> and <code>book_authors<\/code> tables. However, in this tutorial, you will learn how to create them from PHP using PDO.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='using-pdo-to-create-new-tables'>Using PDO to create new tables <a href=\"#using-pdo-to-create-new-tables\" class=\"anchor\" id=\"using-pdo-to-create-new-tables\" title=\"Anchor for Using PDO to create new tables\">#<\/a><\/h2>\n\n\n\n<p>To execute an SQL statement using PDO, you use follow these steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, connect to the database (MySQL server in this case) by creating a new instance of the PDO class.<\/li><li>Second, execute an SQL statement by calling the <code>exec()<\/code> method of the PDO instance.<\/li><\/ul>\n\n\n\n<p>The <code>exec()<\/code>\u00a0method returns the number of affected rows on success or <code>false<\/code> on failure.<\/p>\n\n\n\n<p>The following script illustrates how to create the <code>authors<\/code> and <code>book_authors<\/code> tables:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\r\n\r\n<span class=\"hljs-comment\">\/\/ SQL statement for creating new tables<\/span>\r\n$statements = &#91;\r\n\t<span class=\"hljs-string\">'CREATE TABLE authors( \r\n        author_id   INT AUTO_INCREMENT,\r\n        first_name  VARCHAR(100) NOT NULL, \r\n        middle_name VARCHAR(50) NULL, \r\n        last_name   VARCHAR(100) NULL,\r\n        PRIMARY KEY(author_id)\r\n    );'<\/span>,\r\n\t<span class=\"hljs-string\">'CREATE TABLE book_authors (\r\n        book_id   INT NOT NULL, \r\n        author_id INT NOT NULL, \r\n        PRIMARY KEY(book_id, author_id), \r\n        CONSTRAINT fk_book \r\n            FOREIGN KEY(book_id) \r\n            REFERENCES books(book_id) \r\n            ON DELETE CASCADE, \r\n            CONSTRAINT fk_author \r\n                FOREIGN KEY(author_id) \r\n                REFERENCES authors(author_id) \r\n                ON DELETE CASCADE\r\n    )'<\/span>];\r\n\r\n<span class=\"hljs-comment\">\/\/ connect to the database<\/span>\r\n$pdo = <span class=\"hljs-keyword\">require<\/span> <span class=\"hljs-string\">'connect.php'<\/span>;\r\n\r\n<span class=\"hljs-comment\">\/\/ execute SQL statements<\/span>\r\n<span class=\"hljs-keyword\">foreach<\/span> ($statements <span class=\"hljs-keyword\">as<\/span> $statement) {\r\n\t$pdo-&gt;exec($statement);\r\n}\r<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that this script reuses the <code>connect.php<\/code> script that <a href=\"https:\/\/phptutorial.net\/php-pdo\/php-pdo-mysql\/\">creates a connection to the MySQL database server<\/a>.<\/p>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, define an array that holds the SQL statements for creating the <code>authors<\/code> and <code>book_authors<\/code> tables.<\/li><li>Second, connect to the MySQL database server using the <code>connect.php<\/code> script. The <code>connect.php<\/code> connects to the <code>book<\/code>db database on the local MySQL Server.<\/li><li>Third, execute each statement in the <code>$statements<\/code> array by calling the <code>exec()<\/code> method of the PDO instance.<\/li><\/ul>\n\n\n\n<p>After executing the script, you can open the <code>bookdb<\/code> database in a MySQL client tool to check if the <code>authors<\/code> and <code>book_authors<\/code> tables are created successfully.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the <code>exec()<\/code> method of the PDO instance to execute the <code>CREATE TABLE<\/code> statement to create a new table in the database from PHP.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"138\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-pdo\/pdo-creating-new-tables\/\"\n\t\t\t\tdata-post-title=\"Creating New Tables\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"138\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-pdo\/pdo-creating-new-tables\/\"\n\t\t\t\tdata-post-title=\"Creating New Tables\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you wil learn how to create new tables in the database using PDO.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":21,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-138","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=138"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/138\/revisions"}],"predecessor-version":[{"id":2164,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/138\/revisions\/2164"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/21"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}