Author: alien

  • Khóa học miễn phí MariaDB – Null Values nhận dự án làm có lương

    MariaDB – Null Values



    When working with NULL values, remember they are unknown values. They are not empty strings or zero, which are valid values. In table creation, column specifications allow for setting them to accept null values, or reject them. Simply utilize a NULL or NOT NULL clause. This has applications in cases of missing record information like an ID number.

    User-defined variables have a value of NULL until explicit assignment. Stored routine parameters and local variables allow setting a value of NULL. When a local variable has no default value, it has a value of NULL.

    NULL is case-insensitive, and has the following aliases −

    • UNKNOWN (a boolean value)
    • N

    NULL Operators

    Standard comparison operators cannot be used with NULL (e.g., =, >, >=, <=, <, or !=) because all comparisons with a NULL value return NULL, not true or false. Comparisons with NULL or possibly containing it must use the “<=>” (NULL-SAFE) operator.

    Other available operators are −

    • IS NULL − It tests for a NULL value.

    • IS NOT NULL − It confirms the absence of a NULL value.

    • ISNULL − It returns a value of 1 on discovery of a NULL value, and 0 in its absence.

    • COALESCE − It returns the first non-NULL value of a list, or it returns a NULL value in the absence of one.

    Sorting NULL Values

    In sorting operations, NULL values have the lowest value, so DESC order results in NULL values at the bottom. MariaDB allows for setting a higher value for NULL values.

    There are two ways to do this as shown below −

    SELECT column1 FROM product_tbl ORDER BY ISNULL(column1), column1;
    

    The other way −

    SELECT column1 FROM product_tbl ORDER BY IF(column1 IS NULL, 0, 1), column1 DESC;
    

    NULL Functions

    Functions generally output NULL when any parameters are NULL. However, there are functions specifically designed for managing NULL values. They are −

    • IFNULL() − If the first expression is not NULL it returns it. When it evaluates to NULL, it returns the second expression.

    • NULLIF() − It returns NULL when the compared expressions are equal, if not, it returns the first expression.

    Functions like SUM and AVG ignore NULL values.

    Inserting NULL Values

    On insertion of a NULL value in a column declared NOT NULL, an error occurs. In default SQL mode, a NOT NULL column will instead insert a default value based on data type.

    When a field is a TIMESTAMP, AUTO_INCREMENT, or virtual column, MariaDB manages NULL values differently. Insertion in an AUTO_INCREMENT column causes the next number in the sequence to insert in its place. In a TIMESTAMP field, MariaDB assigns the current timestamp instead. In virtual columns, a topic discussed later in this tutorial, the default value is assigned.

    UNIQUE indices can hold many NULL values, however, primary keys cannot be NULL.

    NULL Values and the Alter Command

    When you use the ALTER command to modify a column, in the absence of NULL specifications, MariaDB automatically assigns values.


    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 – Join nhận dự án làm có lương

    MariaDB – Join



    In previous discussions and examples, we examined retrieving from a single table, or retrieving multiple values from multiple sources. Most real-world data operations are much more complex, requiring aggregation, comparison, and retrieval from multiple tables.

    JOINs allow merging of two or more tables into a single object. They are employed through SELECT, UPDATE, and DELETE statements.

    Review the general syntax of a statement employing a JOIN as shown below −

    SELECT column
    FROM table_name1
    INNER JOIN table_name2
    ON table_name1.column = table_name2.column;
    

    Note the old syntax for JOINS used implicit joins and no keywords. It is possible to use a WHERE clause to achieve a join, but keywords work best for readability, maintenance, and best practices.

    JOINs come in many forms such as a left join, right join, or inner join. Various join types offer different types of aggregation based on shared values or characteristics.

    Employ a JOIN either at the command prompt or with a PHP script.

    The Command Prompt

    At the command prompt, simply use a standard statement −

    root@host# mysql -u root -p password;
    Enter password:*******
    mysql> use PRODUCTS;
    Database changed
    
    mysql> SELECT products.ID_number, products.Nomenclature, inventory.inventory_ct
       FROM products
       INNER JOIN inventory
       ON products.ID_numbeer = inventory.ID_number;
    +-------------+----------------+-----------------+
    | ID_number   | Nomenclature   | Inventory Count |
    +-------------+----------------+-----------------+
    | 12345       | Orbitron 4000  | 150             |
    +-------------+----------------+-----------------+
    | 12346       | Orbitron 3000  | 200             |
    +-------------+----------------+-----------------+
    | 12347       | Orbitron 1000  | 0               |
    +-------------+----------------+-----------------+
    

    PHP Script Using JOIN

    Use the mysql_query() function to perform a join operation −

    <?php
       $dbhost = ''localhost:3036
       $dbuser = ''root
       $dbpass = ''rootpassword
       $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    
       if(! $conn ) {
          die(''Could not connect: '' . mysql_error());
       }
    
       $sql = ''SELECT a.product_id, a.product_manufacturer, b.product_count
          FROM products_tbl a, pcount_tbl b
          WHERE a.product_manufacturer = b.product_manufacturer
    
       mysql_select_db(''PRODUCTS'');
       $retval = mysql_query( $sql, $conn );
    
       if(! $retval ) {
          die(''Could not get data: '' . mysql_error());
       }
    
       while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
          echo "Manufacturer:{$row[''product_manufacturer'']} <br&gt ".
             "Count: {$row[''product_count'']} <br&gt ".
             "Product ID: {$row[''product_id'']} <br&gt ".
             "--------------------------------<br&gt";
       }
    
       echo "Fetched data successfullyn";
       mysql_close($conn);
    ?>
    

    On successful data retrieval, you will see the following output −

    ID Number: 12345
    Nomenclature: Orbitron 4000
    Inventory Count: 150
    --------------------------------------
    ID Number: 12346
    Nomenclature: Orbitron 3000
    Inventory Count: 200
    --------------------------------------
    ID Number: 12347
    Nomenclature: Orbitron 1000
    Inventory Count: 0
    --------------------------------------
    mysql> Fetched data 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 – Like Clause nhận dự án làm có lương

    MariaDB – Like Clause



    The WHERE clause provides a way to retrieve data when an operation uses an exact match. In situations requiring multiple results with shared characteristics, the LIKE clause accommodates broad pattern matching.

    A LIKE clause tests for a pattern match, returning a true or false. The patterns used for comparison accept the following wildcard characters: “%”, which matches numbers of characters (0 or more); and “_”, which matches a single character. The “_” wildcard character only matches characters within its set, meaning it will ignore latin characters when using another set. The matches are case-insensitive by default requiring additional settings for case sensitivity.

    A NOT LIKE clause allows for testing the opposite condition, much like the not operator.

    If the statement expression or pattern evaluate to NULL, the result is NULL.

    Review the general LIKE clause syntax given below −

    SELECT field, field2,... FROM table_name, table_name2,...
    WHERE field LIKE condition
    

    Employ a LIKE clause either at the command prompt or within a PHP script.

    The Command Prompt

    At the command prompt, simply use a standard command −

    root@host# mysql -u root -p password;
    Enter password:*******
    mysql> use TUTORIALS;
    Database changed
    mysql> SELECT * from products_tbl
       WHERE product_manufacturer LIKE ''XYZ%
    +-------------+----------------+----------------------+
    | ID_number   | Nomenclature   | product_manufacturer |
    +-------------+----------------+----------------------+
    | 12345       | Orbitron 4000  | XYZ Corp             |
    +-------------+----------------+----------------------+
    | 12346       | Orbitron 3000  | XYZ Corp             |
    +-------------+----------------+----------------------+
    | 12347       | Orbitron 1000  | XYZ Corp             |
    +-------------+----------------+----------------------+
    

    PHP Script Using Like Clause

    Use the mysql_query() function in statements employing the LIKE clause

    <?php
       $dbhost = ''localhost:3036
       $dbuser = ''root
       $dbpass = ''rootpassword
       $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    
       if(! $conn ) {
          die(''Could not connect: '' . mysql_error());
       }
    
       $sql = ''SELECT product_id, product_name, product_manufacturer, ship_date
          FROM products_tbl WHERE product_manufacturer LIKE "xyz%"
    
       mysql_select_db(''PRODUCTS'');
       $retval = mysql_query( $sql, $conn );
    
       if(! $retval ) {
          die(''Could not get data: '' . mysql_error());
       }
    
       while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
          echo "Product ID:{$row[''product_id'']} <br> ".
             "Name: {$row[''product_name'']} <br> ".
             "Manufacturer: {$row[''product_manufacturer'']} <br> ".
             "Ship Date: {$row[''ship_date'']} <br> ".
             "--------------------------------<br>";
       }
    
       echo "Fetched data successfullyn";
       mysql_close($conn);
    ?>
    

    On successful data retrieval, you will see the following output −

    Product ID: 12345
    Nomenclature: Orbitron 4000
    Manufacturer: XYZ Corp
    Ship Date: 01/01/17
    ----------------------------------------------
    Product ID: 12346
    Nomenclature: Orbitron 3000
    Manufacturer: XYZ Corp
    Ship Date: 01/02/17
    ----------------------------------------------
    Product ID: 12347
    Nomenclature: Orbitron 1000
    Manufacturer: XYZ Corp
    Ship Date: 01/02/17
    ----------------------------------------------
    mysql> Fetched data 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 – Select Query nhận dự án làm có lương

    MariaDB – Select Query



    In this chapter, we will learn how to select data from a table.

    SELECT statements retrieve selected rows. They can include UNION statements, an ordering clause, a LIMIT clause, a WHERE clause, a GROUP BY…HAVING clause, and subqueries.

    Review the following general syntax −

    SELECT field, field2,... FROM table_name, table_name2,... WHERE...
    

    A SELECT statement provides multiple options for specifying the table used −

    • database_name.table_name

    • table_name.column_name

    • database_name.table_name.column_name

    All select statements must contain one or more select expressions. Select expressions consist of one of the following options −

    • A column name.

    • An expression employing operators and functions.

    • The specification “table_name.*” to select all columns within the given table.

    • The character “*” to select all columns from all tables specified in the FROM clause.

    The command prompt or a PHP script can be employed in executing a select statement.

    The Command Prompt

    At the command prompt, execute statements as follows −

    root@host# mysql -u root -p password;
    Enter password:*******
    mysql> use PRODUCTS;
    Database changed
    mysql> SELECT * from products_tbl
    +-------------+---------------+
    | ID_number   | Nomenclature  |
    +-------------+---------------+
    | 12345       | Orbitron 4000 |
    +-------------+---------------+
    

    PHP Select Script

    Employ the same SELECT statement(s) within a PHP function to perform the operation. You will use the mysql_query() function once again. Review an example given below −

    <?php
       $dbhost = ''localhost:3036
       $dbuser = ''root
       $dbpass = ''rootpassword
       $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    
       if(! $conn ) {
          die(''Could not connect: '' . mysql_error());
       }
    
       $sql = ''SELECT product_id, product_name,product_manufacturer, ship_date FROM products_tbl
       mysql_select_db(''PRODUCTS'');
       $retval = mysql_query( $sql, $conn );
    
       if(! $retval ) {
          die(''Could not get data: '' . mysql_error());
       }
    
       while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
          echo "Product ID :{$row[''product_id'']} <br> ".
             "Name: {$row[''product_name'']} <br> ".
             "Manufacturer: {$row[''product_manufacturer'']} <br> ".
             "Ship Date : {$row[''ship_date'']} <br>".
             "--------------------------------<br>";
       }
    
       echo "Fetched data successfullyn";
       mysql_close($conn);
    ?>
    

    On successful data retrieval, you will see the following output −

    Product ID: 12345
    Nomenclature: Orbitron 4000
    Manufacturer: XYZ Corp
    Ship Date: 01/01/17
    ----------------------------------------------
    Product ID: 12346
    Nomenclature: Orbitron 3000
    Manufacturer: XYZ Corp
    Ship Date: 01/02/17
    ----------------------------------------------
    mysql> Fetched data successfully
    

    Best practices suggest releasing cursor memory after every SELECT statement. PHP provides the mysql_free_result() function for this purpose. Review its use as shown below −

    <?php
       $dbhost = ''localhost:3036
       $dbuser = ''root
       $dbpass = ''rootpassword
       $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    
       if(! $conn ) {
          die(''Could not connect: '' . mysql_error());
       }
    
       $sql = ''SELECT product_id, product_name, product_manufacturer, ship_date FROM products_tbl
       mysql_select_db(''PRODUCTS'');
       $retval = mysql_query( $sql, $conn );
    
       if(! $retval ) {
          die(''Could not get data: '' . mysql_error());
       }
    
       while($row = mysql_fetch_array($retval, MYSQL_NUM)) {
          echo "Product ID :{$row[0]} <br> ".
             "Name: {$row[1]} <br> ".
             "Manufacturer: {$row[2]} <br> ".
             "Ship Date : {$row[3]} <br> ".
             "--------------------------------<br>";
       }
    
       mysql_free_result($retval);
       echo "Fetched data successfullyn";
       mysql_close($conn);
    ?>
    

    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 – Update Query nhận dự án làm có lương

    MariaDB – Update Query



    The UPDATE command modifies existing fields by changing values. It uses the SET clause to specify columns for modification, and to specify the new values assigned. These values can be either an expression or the default value of the field. Setting a default value requires using the DEFAULT keyword. The command can also employ a WHERE clause to specify conditions for an update and/or an ORDER BY clause to update in a certain order.

    Review the following general syntax −

    UPDATE table_name SET field=new_value, field2=new_value2,...
    [WHERE ...]
    

    Execute an UPDATE command from either the command prompt or using a PHP script.

    The Command Prompt

    At the command prompt, simply use a standard commandroot −

    root@host# mysql -u root -p password;
    Enter password:*******
    mysql> use PRODUCTS;
    Database changed
    mysql> UPDATE products_tbl
       SET nomenclature = ''Fiber Blaster 300Z'' WHERE ID_number = 112;
    mysql> SELECT * from products_tbl WHERE ID_number=''112
    +-------------+---------------------+----------------------+
    | ID_number   | Nomenclature        | product_manufacturer |
    +-------------+---------------------+----------------------+
    | 112         | Fiber Blaster 300Z  | XYZ Corp             |
    +-------------+---------------------+----------------------+
    

    PHP Update Query Script

    Employ the mysql_query() function in UPDATE command statements −

    <?php
       $dbhost = ‘localhost:3036’;
       $dbuser = ‘root’;
       $dbpass = ‘rootpassword’;
       $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    
       if(! $conn ) {
          die(‘Could not connect: ‘ . mysql_error());
       }
    
       $sql = ‘UPDATE products_tbl
          SET product_name = ”Fiber Blaster 300z”
          WHERE product_id = 112’;
    
       mysql_select_db(‘PRODUCTS’);
       $retval = mysql_query( $sql, $conn );
    
       if(! $retval ) {
          die(‘Could not update data: ‘ . mysql_error());
       }
    
       echo “Updated data successfullyn”;
       mysql_close($conn);
    ?>
    

    On successful data update, you will see the following output −

    mysql> Updated data 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 – Where Clause nhận dự án làm có lương

    MariaDB – Where Clause



    WHERE clauses filter various statements such as SELECT, UPDATE, DELETE, and INSERT. They present criteria used to specify action. They typically appear after a table name in a statement, and their condition follows. The WHERE clause essentially functions like an if statement.

    Review the general syntax of WHERE clause given below −

    [COMMAND] field,field2,... FROM table_name,table_name2,... WHERE [CONDITION]
    

    Note the following qualities of the WHERE clause −

    • It is optional.

    • It allows any condition to be specified.

    • It allows for the specification of multiple conditions through using an AND or OR operator.

    • Case sensitivity only applies to statements using LIKE comparisons.

    The WHERE clause permits the use of the following operators −

    Operator
    = !=
    > <
    >= <=

    WHERE clauses can be utilized at the command prompt or within a PHP script.

    The Command Prompt

    At the command prompt, simply use a standard command −

    root@host# mysql -u root -p password;
    Enter password:*******
    mysql> use PRODUCTS;
    Database changed
    mysql> SELECT * from products_tbl WHERE product_manufacturer = ''XYZ Corp
    +-------------+----------------+----------------------+
    | ID_number   | Nomenclature   | product_manufacturer |
    +-------------+----------------+----------------------+
    | 12345       | Orbitron 4000  | XYZ Corp             |
    +-------------+----------------+----------------------+
    | 12346       | Orbitron 3000  | XYZ Corp             |
    +-------------+----------------+----------------------+
    | 12347       | Orbitron 1000  | XYZ Corp             |
    +-------------+----------------+----------------------+
    

    Review an example using the AND condition −

    SELECT *
    FROM products_tbl
    WHERE product_name = ''Bun Janshu 3000
    AND product_id <= 344;
    

    This example combines both AND and OR conditions

    SELECT *
    FROM products_tbl
    WHERE (product_name = ''Bun Janshu 3000'' AND product_id < 344)
    OR (product_name = ''Bun Janshu 3000'');
    

    PHP Scripts Using Where Clause

    Employ the mysql_query() function in operations using a WHERE clause −

    <?php
       $dbhost = ''localhost:3036
       $dbuser = ''root
       $dbpass = ''rootpassword
       $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    
       if(! $conn ) {
          die(''Could not connect: '' . mysql_error());
       }
    
       $sql = ''SELECT product_id, product_name, product_manufacturer, ship_date
          FROM products_tbl
          WHERE product_manufacturer = "XYZ Corp"
    
       mysql_select_db(''PRODUCTS'');
       $retval = mysql_query( $sql, $conn );
    
       if(! $retval ) {
          die(''Could not get data: '' . mysql_error());
       }
    
       while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
          echo "Product ID :{$row[''product_id'']} <br> ".
             "Name: {$row[''product_name'']} <br> ".
             "Manufacturer: {$row[''product_manufacturer'']} <br> ".
             "Ship Date: {$row[''ship_date'']} <br> ".
             "--------------------------------<br>";
       }
    
       echo "Fetched data successfullyn";
       mysql_close($conn);
    ?>
    

    On successful data retrieval, you will see the following output −

    Product ID: 12345
    Nomenclature: Orbitron 4000
    Manufacturer: XYZ Corp
    Ship Date: 01/01/17
    ----------------------------------------------
    Product ID: 12346
    Nomenclature: Orbitron 3000
    Manufacturer: XYZ Corp
    Ship Date: 01/02/17
    ----------------------------------------------
    Product ID: 12347
    Nomenclature: Orbitron 1000
    Manufacturer: XYZ Corp
    Ship Date: 01/02/17
    ----------------------------------------------
    mysql> Fetched data 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 – Order By Clause nhận dự án làm có lương

    MariaDB – Order By Clause



    The ORDER BY clause, as mentioned in previous discussions, sorts the results of a statement. It specifies the order of the data operated on, and includes the option to sort in ascending (ASC) or descending (DESC) order. On omission of order specification, the default order is ascending.

    ORDER BY clauses appear in a wide variety of statements such as DELETE and UPDATE. They always appear at the end of a statement, not in a subquery or before a set function, because they operate on the final resulting table. You also cannot use an integer to identify a column.

    Review the general syntax of the ORDER BY clause given below −

    SELECT field, field2,... [or column] FROM table_name, table_name2,...
    ORDER BY field, field2,... ASC[or DESC]
    

    Use an ORDER BY clause either at the command prompt or within a PHP script.

    The Command Prompt

    At the command prompt, simply use a standard command −

    root@ host# mysql -u root -p password;
    Enter password:*******
    mysql> use PRODUCTS;
    Database changed
    
    mysql> SELECT * from products_tbl ORDER BY product_manufacturer ASC
    +-------------+----------------+----------------------+
    | ID_number   | Nomenclature   | product_manufacturer |
    +-------------+----------------+----------------------+
    | 56789       | SuperBlast 400 | LMN Corp             |
    +-------------+----------------+----------------------+
    | 67891       | Zoomzoom 5000  | QFT Corp             |
    +-------------+----------------+----------------------+
    | 12347       | Orbitron 1000  | XYZ Corp             |
    +-------------+----------------+----------------------+
    

    PHP Script Using Order By Clause

    Utilize the mysql_query() function, once again, in statements employing the ORDER BY clause −

    <?php
       $dbhost = ''localhost:3036
       $dbuser = ''root
       $dbpass = ''rootpassword
       $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    
       if(! $conn ) {
          die(''Could not connect: '' . mysql_error());
       }
    
       $sql = ''SELECT product_id, product_name, product_manufacturer, ship_date
          FROM products_tbl ORDER BY product_manufacturer DESC
    
       mysql_select_db(''PRODUCTS'');
       $retval = mysql_query( $sql, $conn );
    
       if(! $retval ) {
          die(''Could not get data: '' . mysql_error());
       }
    
       while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
          echo "Product ID :{$row[''product_id'']} <br> ".
             "Name: {$row[''product_name'']} <br> ".
             "Manufacturer: {$row[''product_manufacturer'']} <br> ".
             "Ship Date : {$row[''ship_date'']} <br> ".
             "--------------------------------<br>";
       }
    
       echo "Fetched data successfullyn";
       mysql_close($conn);
    ?>
    

    On successful data retrieval, you will see the following output −

    Product ID: 12347
    Nomenclature: Orbitron 1000
    Manufacturer: XYZ Corp
    Ship Date: 01/01/17
    ----------------------------------------------
    Product ID: 67891
    Nomenclature: Zoomzoom 5000
    Manufacturer: QFT Corp
    Ship Date: 01/01/17
    ----------------------------------------------
    Product ID: 56789
    Nomenclature: SuperBlast 400
    Manufacturer: LMN Corp
    Ship Date: 01/04/17
    ----------------------------------------------
    mysql> Fetched data 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 – Delete Query nhận dự án làm có lương

    MariaDB – Delete Query



    The DELETE command deletes table rows from the specified table, and returns the quantity deleted. Access the quantity deleted with the ROW_COUNT() function. A WHERE clause specifies rows, and in its absence, all rows are deleted. A LIMIT clause controls the number of rows deleted.

    In a DELETE statement for multiple rows, it deletes only those rows satisfying a condition; and LIMIT and WHERE clauses are not permitted. DELETE statements allow deleting rows from tables in different databases, but do not allow deleting from a table and then selecting from the same table within a subquery.

    Review the following DELETE syntax −

    DELETE FROM table_name [WHERE …]
    

    Execute a DELETE command from either the command prompt or using a PHP script.

    The Command Prompt

    At the command prompt, simply use a standard command −

    root@host# mysql –u root –p password;
    Enter password:*******
    mysql> use PRODUCTS;
    Database changed
    mysql> DELETE FROM products_tbl WHERE product_id=133;
    mysql> SELECT * from products_tbl WHERE ID_number=''133
    ERROR 1032 (HY000): Can''t find record in ''products_tbl''
    

    PHP Delete Query Script

    Use the mysql_query() function in DELETE command statements −

    <?php
       $dbhost = ''localhost:3036
       $dbuser = ''root
       $dbpass = ''rootpassword
       $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    
       if(! $conn ) {
          die(''Could not connect: '' . mysql_error());
       }
    
       $sql = ''DELETE FROM products_tbl WHERE product_id = 261
       mysql_select_db(''PRODUCTS'');
       $retval = mysql_query( $sql, $conn );
    
       if(! $retval ) {
          die(''Could not delete data: '' . mysql_error());
       }
    
       echo "Deleted data successfullyn";
       mysql_close($conn);
    ?>
    

    On successful data deletion, you will see the following output −

    mysql> Deleted data successfully
    mysql> SELECT * from products_tbl WHERE ID_number=''261
    ERROR 1032 (HY000): Can''t find record in ''products_tbl''
    

    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 – Drop Tables nhận dự án làm có lương

    MariaDB – Drop Tables



    In this chapter, we will learn to delete tables.

    Table deletion is very easy, but remember all deleted tables are irrecoverable. The general syntax for table deletion is as follows −

    DROP TABLE table_name ;
    

    Two options exist for performing a table drop: use the command prompt or a PHP script.

    The Command Prompt

    At the command prompt, simply use the DROP TABLE SQL command −

    root@host# mysql -u root -p
    Enter password:*******
    mysql> use PRODUCTS;
    Database changed
    mysql> DROP TABLE products_tbl
    
    mysql> SELECT * from products_tbl
    ERROR 1146 (42S02): Table ''products_tbl'' doesn''t exist
    

    PHP Drop Table Script

    PHP provides mysql_query() for dropping tables. Simply pass its second argument the appropriate 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 = "DROP TABLE products_tbl";
             mysql_select_db( ''PRODUCTS'' );
             $retval = mysql_query( $sql, $conn );
    
             if(! $retval ) {
                die(''Could not delete table: '' . mysql_error());
             }
             echo "Table deleted successfullyn";
    
             mysql_close($conn);
          ?>
       </body>
    </html>
    

    On successful table deletion, you will see the following output −

    mysql> Table deleted 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 – Insert Query nhận dự án làm có lương

    MariaDB – Insert Query



    In this chapter, we will learn how to insert data in a table.

    Inserting data into a table requires the INSERT command. The general syntax of the command is INSERT followed by the table name, fields, and values.

    Review its general syntax given below −

    INSERT INTO tablename (field,field2,...) VALUES (value, value2,...);
    

    The statement requires the use of single or double quotes for string values. Other options for the statement include “INSERT…SET” statements, “INSERT…SELECT” statements, and several other options.

    Note − The VALUES() function that appears within the statement, only applies to INSERT statements and returns NULL if used elsewhere.

    Two options exist for performing the operation: use the command line or use a PHP script.

    The Command Prompt

    At the prompt, there are many ways to perform a select operation. A standard statement is given below −

    belowmysql>
    INSERT INTO products_tbl (ID_number, Nomenclature) VALUES (12345,“Orbitron 4000”);
    mysql> SHOW COLUMNS FROM products_tbl;
    +-------------+-------------+------+-----+---------+-------+
    | Field       | Type        | Null | Key | Default | Extra |
    +-------------+-------------+------+-----+---------+-------+
    | ID_number   | int(5)      |      |     |         |       |
    | Nomenclature| char(13)    |      |     |         |       |
    +-------------+-------------+------+-----+---------+-------+
    

    You can insert multiple rows −

    INSERT INTO products VALUES (1, “first row”), (2, “second row”);
    

    You can also employ the SET clause −

    INSERT INTO products SELECT * FROM inventory WHERE status = ''available
    

    PHP Insertion Script

    Employ the same “INSERT INTO…” statement within a PHP function to perform the operation. You will use the mysql_query() function once again.

    Review the example given below −

    <?php
       if(isset($_POST[''add''])) {
          $dbhost = ''localhost:3036
          $dbuser = ''root
          $dbpass = ''rootpassword
          $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    
          if(! $conn ) {
             die(''Could not connect: '' . mysql_error());
          }
    
          if(! get_magic_quotes_gpc() ) {
             $product_name = addslashes ($_POST[''product_name'']);
             $product_manufacturer = addslashes ($_POST[''product_name'']);
          } else {
             $product_name = $_POST[''product_name''];
             $product_manufacturer = $_POST[''product_manufacturer''];
          }
          $ship_date = $_POST[''ship_date''];
          $sql = "INSERT INTO products_tbl ".
             "(product_name,product_manufacturer, ship_date) ".
             "VALUES"."(''$product_name'',''$product_manufacturer'',''$ship_date'')";
    
          mysql_select_db(''PRODUCTS'');
          $retval = mysql_query( $sql, $conn );
    
          if(! $retval ) {
             die(''Could not enter data: '' . mysql_error());
          }
    
          echo "Entered data successfullyn";
          mysql_close($conn);
       }
    ?>
    

    On successful data insertion, you will see the following output −

    mysql> Entered data successfully
    

    You will also collaborate validation statements with insert statements such as checking to ensure correct data entry. MariaDB includes a number of options for this purpose, some of which are automatic.


    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