PHP ਮਾਇਕਰੋਸਕੁਏਲ ਡਾਟਾਬੇਸ ਬਣਾਉਣ
- Previous Page MySQL Connect
- Next Page MySQL Insert
ਡਾਟਾਬੇਸ ਵਿੱਚ ਇੱਕ ਜਾਂ ਵੱਧ ਤੇਲਕ ਹੁੰਦੇ ਹਨ。
ਡਾਟਾਬੇਸ ਬਣਾਉਣ
CREATE DATABASE ਬਿਆਨ ਮਾਇਕਰੋਸਕੁਏਲ ਵਿੱਚ ਡਾਟਾਬੇਸ ਬਣਾਉਣ ਲਈ ਵਰਤਿਆ ਜਾਂਦਾ ਹੈ。
ਗਣਨਾ
CREATE DATABASE database_name
ਮਾਇਕਰੋਸਕੁਏਲ ਨੂੰ ਉੱਪਰੋਕਤ ਬਿਆਨ ਚਲਾਉਣ ਲਈ ਅਸੀਂ mysql_query() ਫੰਕਸ਼ਨ ਵਰਤਣਾ ਹੋਵੇਗਾ। ਇਹ ਫੰਕਸ਼ਨ ਮਾਇਕਰੋਸਕੁਏਲ ਕਨੈਕਸ਼ਨ ਨੂੰ ਕਮਾਂਡ ਜਾਂ ਕਿਸੇ ਵੀ ਜਾਣਕਾਰੀ ਨੂੰ ਭੇਜਣ ਲਈ ਵਰਤਿਆ ਜਾਂਦਾ ਹੈ。
Examples
ਇਸ ਉਦਾਹਰਣ ਵਿੱਚ ਇੱਕ "my_db" ਨਾਮ ਦਾ ਡਾਟਾਬੇਸ ਬਣਾਇਆ ਗਿਆ ਹੈ:
<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; } else { echo "Error creating database: " . mysql_error(); } mysql_close($con); ?>
ਤੇਲਕ ਬਣਾਉਣ
CREATE TABLE ਬਿਆਨ ਮਾਇਕਰੋਸਕੁਏਲ ਵਿੱਚ ਤੇਲਕ ਬਣਾਉਣ ਲਈ ਵਰਤਿਆ ਜਾਂਦਾ ਹੈ。
ਗਣਨਾ
CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, column_name3 data_type, ....... )
ਇਸ ਕਮਾਂਡ ਨੂੰ ਚਲਾਉਣ ਲਈ ਮੈਂ mysql_query() ਫੰਕਸ਼ਨ ਵਿੱਚ CREATE TABLE ਬਿਆਨ ਜੋੜਨਾ ਹੋਵੇਗਾ。
Examples
ਇਸ ਉਦਾਹਰਣ ਵਿੱਚ ਇੱਕ "Persons" ਨਾਮ ਦਾ ਤੇਲਕ ਬਣਾਉਣ ਦੇ ਤਰੀਕੇ ਦਿਸਾਇਆ ਗਿਆ ਹੈ ਜਿਸ ਵਿੱਚ ਤਿੰਨ ਸਤੰਭ ਹਨ। ਸਤੰਭ ਨਾਮ ਹਨ "FirstName", "LastName" ਅਤੇ "Age":
<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } // Create database if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; } else { echo "Error creating database: " . mysql_error(); } // Create table in my_db database mysql_select_db("my_db", $con); $sql = "CREATE TABLE Persons ( FirstName varchar(15), LastName varchar(15), Age int )"; mysql_query($sql,$con); mysql_close($con); ?>
重要事项:在创建表之前,必须首先选择数据库。通过 mysql_select_db() 函数选取数据库。
ਨੋਟ:ਤੁਸੀਂ varchar ਟਾਈਪ ਦੇ ਡਾਟਾਬੇਸ ਫੀਲਡ ਬਣਾਉਣ ਤੇ, ਫੀਲਡ ਦੀ ਮਹਿੰਮਤ ਲੰਬਾਈ ਨਿਰਧਾਰਿਤ ਕਰਨੀ ਚਾਹੀਦੀ ਹੈ, ਉਦਾਹਰਣ ਵਜੋਂ: varchar(15)
ਮਾਇਕਰੋਸਾਫਟ ਡਾਟਾ ਟਾਈਪ
ਹੇਠ ਲਿਖੇ ਵਿਆਵਹਾਰਕ ਮਾਇਕਰੋਸਾਫਟ ਡਾਟਾ ਟਾਈਪਾਂ ਦੀ ਵਰਤੋਂ ਕਰੋ:
ਨੰਬਰ ਟਾਈਪ | Description |
---|---|
|
ਸਿਰਫ਼ ਅੰਕ ਸਮਰਥਤ ਹੈ।sਾਈਜ਼ ਪੈਰਾਮੀਟਰ ਵਿੱਚ ਅੰਕ ਦਾ ਮਹਿੰਮਤ ਮੁੱਲ ਨਿਰਧਾਰਿਤ ਕਰੋ。 |
|
ਦਸਮੀ ਸਮਰਥਤ ਅੰਕ ਸਾਈਜ਼ ਪੈਰਾਮੀਟਰ ਵਿੱਚ ਨਿਰਧਾਰਿਤ ਅੰਕ ਦਾ ਮਹਿੰਮਤ ਮੁੱਲ।d ਪੈਰਾਮੀਟਰ ਵਿੱਚ ਮਹਿੰਮਤ ਦਸਮੀ ਅੰਕ ਦਾ ਮੁੱਲ ਨਿਰਧਾਰਿਤ ਕਰੋ。 |
ਟੈਕਸਟ ਡਾਟਾ ਟਾਈਪ | Description |
---|---|
ਚਾਰ(ਸਾਈਜ਼) |
ਮੰਗਦਾ ਹੈ ਕਿ ਨਿਰਧਾਰਿਤ ਲੰਬਾਈ ਦਾ ਸਟਰਿੰਗ。(ਹਰਫ਼ਾਂ, ਅੰਕਾਂ ਅਤੇ ਵਿਸ਼ੇਸ਼ ਚਿੰਨ੍ਹਾਂ ਨੂੰ ਸ਼ਾਮਲ ਕਰ ਸਕਦਾ ਹੈ)。 ਸਾਈਜ਼ ਪੈਰਾਮੀਟਰ ਵਿੱਚ ਨਿਰਧਾਰਿਤ ਲੰਬਾਈ |
ਵਰਚਾਰ(ਸਾਈਜ਼) |
ਮੰਗਦਾ ਹੈ ਕਿ ਸਰਵਲੰਬਿਤ ਲੰਬਾਈ ਦਾ ਸਟਰਿੰਗ。(ਹਰਫ਼ਾਂ, ਅੰਕਾਂ ਅਤੇ ਵਿਸ਼ੇਸ਼ ਚਿੰਨ੍ਹਾਂ ਨੂੰ ਸ਼ਾਮਲ ਕਰ ਸਕਦਾ ਹੈ)。 ਸਾਈਜ਼ ਪੈਰਾਮੀਟਰ ਵਿੱਚ ਮਹਿੰਮਤ ਲੰਬਾਈ ਨਿਰਧਾਰਿਤ ਕਰੋ。 |
ਟਾਈਨੀਟੈਕਸਟ | ਮੰਗਦਾ ਹੈ ਕਿ ਸਰਵਲੰਬਿਤ ਲੰਬਾਈ ਦਾ ਸਟਰਿੰਗ, ਮਹਿੰਮਤ ਲੰਬਾਈ 255 ਅੰਕਾਂ ਹੈ。 |
|
ਮੰਗਦਾ ਹੈ ਕਿ ਸਰਵਲੰਬਿਤ ਲੰਬਾਈ ਦਾ ਸਟਰਿੰਗ, ਮਹਿੰਮਤ ਲੰਬਾਈ 65535 ਅੰਕਾਂ ਹੈ。 |
|
ਮੰਗਦਾ ਹੈ ਕਿ ਸਰਵਲੰਬਿਤ ਲੰਬਾਈ ਦਾ ਸਟਰਿੰਗ, ਮਹਿੰਮਤ ਲੰਬਾਈ 16777215 ਅੰਕਾਂ ਹੈ。 |
|
ਮੰਗਦਾ ਹੈ ਕਿ ਸਰਵਲੰਬਿਤ ਲੰਬਾਈ ਦਾ ਸਟਰਿੰਗ, ਮਹਿੰਮਤ ਲੰਬਾਈ 4294967295 ਅੰਕਾਂ ਹੈ。 |
ਦਾਤੇ ਡਾਟਾ ਟਾਈਪ | Description |
---|---|
|
Supports date or time |
Miscellaneous Data Types | Description |
---|---|
enum(value1,value2,ect) | ENUM is the abbreviation for ENUMERATED list. Up to 65535 values can be placed in parentheses. |
set | SET is similar to ENUM. However, SET can have up to 64 list items and can store more than one choice |
Primary Key and Auto Increment Fields
Each table should have a primary key field.
The primary key is used to uniquely identify the rows in the table. Each primary key value must be unique in the table. In addition, the primary key field cannot be empty because the database engine needs a value to locate the record.
The primary key field must always be indexed. There are no exceptions to this rule. You must index the primary key field so that the database engine can quickly locate the row with the given key value.
The following example sets the personID field as the primary key field. The primary key field is usually an ID number and is usually set with AUTO_INCREMENT. AUTO_INCREMENT will increment the value of this field one by one when a new record is added. To ensure that the primary key field is not empty, we must add the NOT NULL setting to this field.
Examples
$sql = "CREATE TABLE Persons ( personID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(personID), FirstName varchar(15), LastName varchar(15), Age int )"; mysql_query($sql,$con);
- Previous Page MySQL Connect
- Next Page MySQL Insert