Your cart is currently empty!
Author: alien
-
Khóa học miễn phí MariaDB – Create Tables nhận dự án làm có lương
MariaDB – Create Tables
In this chapter, we will learn how to create tables. Before creating a table, first determine its name, field names, and field definitions.
Following is the general syntax for table creation −
CREATE TABLE table_name (column_name column_type);
Review the command applied to creating a table in the PRODUCTS database −
databaseproducts_ tbl( product_id INT NOT NULL AUTO_INCREMENT, product_name VARCHAR(100) NOT NULL, product_manufacturer VARCHAR(40) NOT NULL, submission_date DATE, PRIMARY KEY ( product_id ) );
The above example uses “NOT NULL” as a field attribute to avoid errors caused by a null value. The attribute “AUTO_INCREMENT” instructs MariaDB to add the next available value to the ID field. The keyword primary key defines a column as the primary key. Multiple columns separated by commas can define a primary key.
The two main methods for creating tables are using the command prompt and a PHP script.
The Command Prompt
Utilize the CREATE TABLE command to perform the task as shown below −
root@host# mysql -u root -p Enter password:******* mysql> use PRODUCTS; Database changed mysql> CREATE TABLE products_tbl( -> product_id INT NOT NULL AUTO_INCREMENT, -> product_name VARCHAR(100) NOT NULL, -> product_manufacturer VARCHAR(40) NOT NULL, -> submission_date DATE, -> PRIMARY KEY ( product_id ) -> ); mysql> SHOW TABLES; +------------------------+ | PRODUCTS | +------------------------+ | products_tbl | +------------------------+
Ensure all commands are terminated with a semicolon.
PHP Create Table Script
PHP provides mysql_query() for table creation. Its second argument contains the necessary SQL command −
<html> <head> <title>Create a MariaDB Table</title> </head> <body> <?php $dbhost = ''localhost:3036 $dbuser = ''root $dbpass = ''rootpassword $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ){ die(''Could not connect: '' . mysql_error()); } echo ''Connected successfully<br /> $sql = "CREATE TABLE products_tbl( ". "product_id INT NOT NULL AUTO_INCREMENT, ". "product_name VARCHAR(100) NOT NULL, ". "product_manufacturer VARCHAR(40) NOT NULL, ". "submission_date DATE, ". "PRIMARY KEY ( product_id )); "; mysql_select_db( ''PRODUCTS'' ); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die(''Could not create table: '' . mysql_error()); } echo "Table created successfullyn"; mysql_close($conn); ?> </body> </html>
On successful table creation, you will see the following output −
mysql> Table created successfully
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc
Khóa học miễn phí MariaDB – Create Database nhận dự án làm có lương
MariaDB – Create Database
Creation or deletion of databases in MariaDB requires privileges typically only given to root users or admins. Under these accounts, you have two options for creating a database − the mysqladmin binary and a PHP script.
mysqladmin binary
The following example demonstrates the use of the mysqladmin binary in creating a database with the name Products −
[root@host]# mysqladmin -u root -p create PRODUCTS Enter password:******
PHP Create Database Script
PHP employs the mysql_query function in creating a MariaDB database. The function uses two parameters, one optional, and returns either a value of “true” when successful, or “false” when not.
Syntax
Review the following create database script syntax −
bool mysql_query( sql, connection );
The description of the parameters is given below −
S.No | Parameter & Description |
---|---|
1 |
sql This required parameter consists of the SQL query needed to perform the operation. |
2 |
connection When not specified, this optional parameter uses the most recent connection used. |
Try the following example code for creating a database −
<html> <head> <title>Create a MariaDB Database</title> </head> <body> <?php $dbhost = ''localhost:3036 $dbuser = ''root $dbpass = ''rootpassword $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die(''Could not connect: '' . mysql_error()); } echo ''Connected successfully<br /> $sql = ''CREATE DATABASE PRODUCTS $retval = mysql_query( $sql, $conn ); if(! $retval ) { die(''Could not create database: '' . mysql_error()); } echo "Database PRODUCTS created successfullyn"; mysql_close($conn); ?> </body> </html>
On successful deletion, you will see the following output −
mysql> Database PRODUCTS created successfully mysql> SHOW DATABASES; +-----------------------+ | Database | +-----------------------+ | PRODUCTS | +-----------------------+
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc