Jump to content

Archive:PHP and MySQL Programming/Creating a Table: Difference between revisions

From IdeaWazaWiki
wikademia>The Kid
No edit summary
wikademia>Wikademia
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{delete|see: Talk:Programming:PHP and MySQL}}
[[Programming:PHP and MySQL|TOC]]
== Introduction ==
== Introduction ==
 
{{?}}
Before creating a table, please read the previous section on [[Programming:PHP and MySQL:Creating a Database|Creating a Database]].
Before creating a table, please read the previous section on [[Programming:PHP and MySQL:Creating a Database|Creating a Database]].


Line 18: Line 14:
  +------------+--------------+------------------------------+------+
  +------------+--------------+------------------------------+------+


As you can see, We have rows (horizontal collection of fields), as well as columns (the vertical attirbutes and values).
As you can see, We have rows (horizontal collection of fields), as well as columns (the vertical attributes and values).


== Creating a Table ==
== Creating a Table ==
Line 74: Line 70:
  1 row in set (0.05 sec)
  1 row in set (0.05 sec)


And then to show the same information, in a tabluated format :
And then to show the same information, in a tabulated format :


  mysql> DESCRIBE `books`;
  mysql> DESCRIBE `books`;
Line 90: Line 86:
  4 rows in set (0.18 sec)
  4 rows in set (0.18 sec)


[[Category:PHP]]
[[../Creating a Database/|Previous: Creating a Database]] | [[../|TOC]] | [[../Basic Queries/|Next: Basic Queries]]
[[Category:MySQL]]
 
[[Category:PHP and MySQL Programming|{{SUBPAGENAME}}]]

Latest revision as of 07:37, 12 June 2009

Introduction

? Before creating a table, please read the previous section on Creating a Database.

A Table resides inside a database. Tables contain rows, which are made up of a collection of common fields or columns. Here is a sample output for a SELECT * query :

mysql> SELECT * FROM `books`;
+------------+--------------+------------------------------+------+
| ISBN       | Author       | Title                        | Year |
+------------+--------------+------------------------------+------+
| 1234567890 | Poisson, R.W | Programming PHP and MySQL    | 2006 |
| 5946253158 | Wilson, M    | Java Secrets                 | 2005 |
| 8529637410 | Moritz, R    | C from Beginners to Advanced | 2001 |
+------------+--------------+------------------------------+------+

As you can see, We have rows (horizontal collection of fields), as well as columns (the vertical attributes and values).

Creating a Table

The SQL code for creating a table is as follows :

mysql> CREATE TABLE `table_name` (
       `field1` type NOT NULL|NULL default 'default_value',
       `field2` type NOT NULL|NULL default 'default_value',
       ...
       );

Example

Here is an example of creating a table called `books` :

mysql> CREATE TABLE `books` (
       `ISBN` varchar(35) NOT NULL default ,
       `Author` varchar(50) NOT NULL default ,
       `Title` varchar(255) NOT NULL default ,
       `Year` int(11) NOT NULL default '2000'
       );

Getting Information about Tables

To get a list of tables :

mysql> SHOW TABLES;

Which produces the following output :

+-------------------+
| Tables_in_library |
+-------------------+
| books             |
+-------------------+
1 row in set (0.19 sec)

To show the CREATE query used to create the table :

mysql> SHOW CREATE TABLE `books`;

Which produces the following output :

+-------+--------------------------------------------+ 
| Table | Create Table 
+-------+--------------------------------------------+
| books | CREATE TABLE `books` (
  `ISBN` varchar(35) NOT NULL default ,
  `Author` varchar(50) NOT NULL default ,
  `Title` varchar(255) NOT NULL default ,
  `Year` int(11) NOT NULL default '2000'
) TYPE=MyISAM |
+-------+--------------------------------------------+
1 row in set (0.05 sec)

And then to show the same information, in a tabulated format :

mysql> DESCRIBE `books`;

Which produces the following output :

+--------+--------------+------+-----+---------+-------+
| Field  | Type         | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| ISBN   | varchar(35)  |      |     |         |       |
| Author | varchar(50)  |      |     |         |       |
| Title  | varchar(255) |      |     |         |       |
| Year   | int(11)      |      |     | 2000    |       |
+--------+--------------+------+-----+---------+-------+
4 rows in set (0.18 sec)

[[../Creating a Database/|Previous: Creating a Database]] | [[../|TOC]] | [[../Basic Queries/|Next: Basic Queries]]