Author: alien

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

    PostgreSQL – WHERE Clause



    The PostgreSQL WHERE clause is used to specify a condition while fetching the data from single table or joining with multiple tables.

    If the given condition is satisfied, only then it returns specific value from the table. You can filter out rows that you do not want included in the result-set by using the WHERE clause.

    The WHERE clause not only is used in SELECT statement, but it is also used in UPDATE, DELETE statement, etc., which we would examine in subsequent chapters.

    Syntax

    The basic syntax of SELECT statement with WHERE clause is as follows −

    SELECT column1, column2, columnN
    FROM table_name
    WHERE [search_condition]
    

    You can specify a search_condition using like >, <, =, LIKE, NOT, etc. The following examples would make this concept clear.

    Example

    Consider the table having records as follows −

    testdb# select * from COMPANY;
     id | name  | age | address   | salary
    ----+-------+-----+-----------+--------
      1 | Paul  |  32 | California|  20000
      2 | Allen |  25 | Texas     |  15000
      3 | Teddy |  23 | Norway    |  20000
      4 | Mark  |  25 | Rich-Mond |  65000
      5 | David |  27 | Texas     |  85000
      6 | Kim   |  22 | South-Hall|  45000
      7 | James |  24 | Houston   |  10000
    (7 rows)
    

    Here are simple examples showing usage of PostgreSQL Logical Operators. Following SELECT statement will list down all the records where AGE is greater than or equal to 25 AND salary is greater than or equal to 65000.00 −

    testdb=# SELECT * FROM COMPANY WHERE AGE >= 25 AND SALARY >= 65000;
    

    The above given PostgreSQL statement will produce the following result −

     id | name  | age |  address   | salary
    ----+-------+-----+------------+--------
      4 | Mark  |  25 | Rich-Mond  |  65000
      5 | David |  27 | Texas      |  85000
    (2 rows)
     

    The following SELECT statement lists down all the records where AGE is greater than or equal to 25 OR salary is greater than or equal to 65000.00 −

    testdb=# SELECT * FROM COMPANY WHERE AGE >= 25 OR SALARY >= 65000;
    

    The above given PostgreSQL statement will produce the following result −

     id | name  | age | address     | salary
    ----+-------+-----+-------------+--------
      1 | Paul  |  32 | California  |  20000
      2 | Allen |  25 | Texas       |  15000
      4 | Mark  |  25 | Rich-Mond   |  65000
      5 | David |  27 | Texas       |  85000
    (4 rows)
    

    The following SELECT statement lists down all the records where AGE is not NULL which means all the records, because none of the record has AGE equal to NULL −

    testdb=#  SELECT * FROM COMPANY WHERE AGE IS NOT NULL;
    

    The above given PostgreSQL statement will produce the following result −

      id | name  | age | address    | salary
     ----+-------+-----+------------+--------
       1 | Paul  |  32 | California |  20000
       2 | Allen |  25 | Texas      |  15000
       3 | Teddy |  23 | Norway     |  20000
       4 | Mark  |  25 | Rich-Mond  |  65000
       5 | David |  27 | Texas      |  85000
       6 | Kim   |  22 | South-Hall |  45000
       7 | James |  24 | Houston    |  10000
    (7 rows)
    

    The following SELECT statement lists down all the records where NAME starts with ”Pa”, does not matter what comes after ”Pa”.

    testdb=# SELECT * FROM COMPANY WHERE NAME LIKE ''Pa%
    

    The above given PostgreSQL statement will produce the following result −

     id | name | age |address    | salary
    ----+------+-----+-----------+--------
      1 | Paul |  32 | California|  20000
    

    The following SELECT statement lists down all the records where AGE value is either 25 or 27 −

    testdb=# SELECT * FROM COMPANY WHERE AGE IN ( 25, 27 );
    

    The above given PostgreSQL statement will produce the following result −

     id | name  | age | address    | salary
    ----+-------+-----+------------+--------
      2 | Allen |  25 | Texas      |  15000
      4 | Mark  |  25 | Rich-Mond  |  65000
      5 | David |  27 | Texas      |  85000
    (3 rows)
    

    The following SELECT statement lists down all the records where AGE value is neither 25 nor 27 −

    testdb=# SELECT * FROM COMPANY WHERE AGE NOT IN ( 25, 27 );
    

    The above given PostgreSQL statement will produce the following result −

     id | name  | age | address    | salary
    ----+-------+-----+------------+--------
      1 | Paul  |  32 | California |  20000
      3 | Teddy |  23 | Norway     |  20000
      6 | Kim   |  22 | South-Hall |  45000
      7 | James |  24 | Houston    |  10000
    (4 rows)
    

    The following SELECT statement lists down all the records where AGE value is in BETWEEN 25 AND 27 −

    testdb=# SELECT * FROM COMPANY WHERE AGE BETWEEN 25 AND 27;
    

    The above given PostgreSQL statement will produce the following result −

     id | name  | age | address    | salary
    ----+-------+-----+------------+--------
      2 | Allen |  25 | Texas      |  15000
      4 | Mark  |  25 | Rich-Mond  |  65000
      5 | David |  27 | Texas      |  85000
    (3 rows)
    

    The following SELECT statement makes use of SQL subquery where subquery finds all the records with AGE field having SALARY > 65000 and later WHERE clause is being used along with EXISTS operator to list down all the records where AGE from the outside query exists in the result returned by sub-query −

    testdb=# SELECT AGE FROM COMPANY
            WHERE EXISTS (SELECT AGE FROM COMPANY WHERE SALARY > 65000);
    

    The above given PostgreSQL statement will produce the following result −

     age
    -----
      32
      25
      23
      25
      27
      22
      24
    (7 rows)
    

    The following SELECT statement makes use of SQL subquery where subquery finds all the records with AGE field having SALARY > 65000 and later WHERE clause is being used along with > operator to list down all the records where AGE from outside query is greater than the age in the result returned by sub-query −

    testdb=# SELECT * FROM COMPANY
            WHERE AGE > (SELECT AGE FROM COMPANY WHERE SALARY > 65000);
    

    The above given PostgreSQL statement will produce the following result −

     id | name | age | address    | salary
    ----+------+-----+------------+--------
      1 | Paul |  32 | California |  20000
    

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

    PostgreSQL – UPDATE Query



    The PostgreSQL UPDATE Query is used to modify the existing records in a table. You can use WHERE clause with UPDATE query to update the selected rows. Otherwise, all the rows would be updated.

    Syntax

    The basic syntax of UPDATE query with WHERE clause is as follows −

    UPDATE table_name
    SET column1 = value1, column2 = value2...., columnN = valueN
    WHERE [condition];
    

    You can combine N number of conditions using AND or OR operators.

    Example

    Consider the table , having records as follows −

    testdb# select * from COMPANY;
     id | name  | age | address   | salary
    ----+-------+-----+-----------+--------
      1 | Paul  |  32 | California|  20000
      2 | Allen |  25 | Texas     |  15000
      3 | Teddy |  23 | Norway    |  20000
      4 | Mark  |  25 | Rich-Mond |  65000
      5 | David |  27 | Texas     |  85000
      6 | Kim   |  22 | South-Hall|  45000
      7 | James |  24 | Houston   |  10000
    (7 rows)
    

    The following is an example, which would update ADDRESS for a customer, whose ID is 6 −

    testdb=# UPDATE COMPANY SET SALARY = 15000 WHERE ID = 3;
    

    Now, COMPANY table would have the following records −

     id | name  | age | address    | salary
    ----+-------+-----+------------+--------
      1 | Paul  |  32 | California |  20000
      2 | Allen |  25 | Texas      |  15000
      4 | Mark  |  25 | Rich-Mond  |  65000
      5 | David |  27 | Texas      |  85000
      6 | Kim   |  22 | South-Hall |  45000
      7 | James |  24 | Houston    |  10000
      3 | Teddy |  23 | Norway     |  15000
    (7 rows)
    

    If you want to modify all ADDRESS and SALARY column values in COMPANY table, you do not need to use WHERE clause and UPDATE query would be as follows −

    testdb=# UPDATE COMPANY SET ADDRESS = ''Texas'', SALARY=20000;
    

    Now, COMPANY table will have the following records −

     id | name  | age | address | salary
    ----+-------+-----+---------+--------
      1 | Paul  |  32 | Texas   |  20000
      2 | Allen |  25 | Texas   |  20000
      4 | Mark  |  25 | Texas   |  20000
      5 | David |  27 | Texas   |  20000
      6 | Kim   |  22 | Texas   |  20000
      7 | James |  24 | Texas   |  20000
      3 | Teddy |  23 | Texas   |  20000
    (7 rows)
    

    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í PostgreSQL – AND & OR Clauses nhận dự án làm có lương

    AND and OR Conjunctive Operators



    The PostgreSQL AND and OR operators are used to combine multiple conditions to narrow down selected data in a PostgreSQL statement. These two operators are called conjunctive operators.

    These operators provide a means to make multiple comparisons with different operators in the same PostgreSQL statement.

    The AND Operator

    The AND operator allows the existence of multiple conditions in a PostgreSQL statement”s WHERE clause. While using AND operator, complete condition will be assumed true when all the conditions are true. For example [condition1] AND [condition2] will be true only when both condition1 and condition2 are true.

    Syntax

    The basic syntax of AND operator with WHERE clause is as follows −

    SELECT column1, column2, columnN
    FROM table_name
    WHERE [condition1] AND [condition2]...AND [conditionN];
    

    You can combine N number of conditions using AND operator. For an action to be taken by the PostgreSQL statement, whether it be a transaction or query, all conditions separated by the AND must be TRUE.

    Example

    Consider the table having records as follows −

    testdb# select * from COMPANY;
     id | name  | age | address   | salary
    ----+-------+-----+-----------+--------
      1 | Paul  |  32 | California|  20000
      2 | Allen |  25 | Texas     |  15000
      3 | Teddy |  23 | Norway    |  20000
      4 | Mark  |  25 | Rich-Mond |  65000
      5 | David |  27 | Texas     |  85000
      6 | Kim   |  22 | South-Hall|  45000
      7 | James |  24 | Houston   |  10000
    (7 rows)
    

    The following SELECT statement lists down all the records where AGE is greater than or equal to 25 AND salary is greater than or equal to 65000.00 −

    testdb=# SELECT * FROM COMPANY WHERE AGE >= 25 AND SALARY >= 65000;
    

    The above given PostgreSQL statement will produce the following result −

     id | name  | age | address    | salary
    ----+-------+-----+------------+--------
      4 | Mark  |  25 | Rich-Mond  |  65000
      5 | David |  27 | Texas      |  85000
    (2 rows)
    

    The OR Operator

    The OR operator is also used to combine multiple conditions in a PostgreSQL statement”s WHERE clause. While using OR operator, complete condition will be assumed true when at least any of the conditions is true. For example [condition1] OR [condition2] will be true if either condition1 or condition2 is true.

    Syntax

    The basic syntax of OR operator with WHERE clause is as follows −

    SELECT column1, column2, columnN
    FROM table_name
    WHERE [condition1] OR [condition2]...OR [conditionN]
    

    You can combine N number of conditions using OR operator. For an action to be taken by the PostgreSQL statement, whether it be a transaction or query, only any ONE of the conditions separated by the OR must be TRUE.

    Example

    Consider the table, having the following records −

     # select * from COMPANY;
      id | name  | age | address   | salary
     ----+-------+-----+-----------+--------
       1 | Paul  |  32 | California|  20000
       2 | Allen |  25 | Texas     |  15000
       3 | Teddy |  23 | Norway    |  20000
       4 | Mark  |  25 | Rich-Mond |  65000
       5 | David |  27 | Texas     |  85000
       6 | Kim   |  22 | South-Hall|  45000
       7 | James |  24 | Houston   |  10000
    (7 rows)
    

    The following SELECT statement lists down all the records where AGE is greater than or equal to 25 OR salary is greater than or equal to 65000.00 −

    testdb=# SELECT * FROM COMPANY WHERE AGE >= 25 OR SALARY >= 65000;
    

    The above given PostgreSQL statement will produce the following result −

     id | name  | age | address    | salary
    ----+-------+-----+------------+--------
      1 | Paul  |  32 | California |  20000
      2 | Allen |  25 | Texas      |  15000
      4 | Mark  |  25 | Rich-Mond  |  65000
      5 | David |  27 | Texas      |  85000
    (4 rows)
    

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

    PostgreSQL – Operators



    What is an Operator in PostgreSQL?

    An operator is a reserved word or a character used primarily in a PostgreSQL statement”s WHERE clause to perform operation(s), such as comparisons and arithmetic operations.

    Operators are used to specify conditions in a PostgreSQL statement and to serve as conjunctions for multiple conditions in a statement.

    • Arithmetic operators
    • Comparison operators
    • Logical operators
    • Bitwise operators

    PostgreSQL Arithmetic Operators

    Assume variable a holds 2 and variable b holds 3, then −

    Operator Description Example
    + Addition – Adds values on either side of the operator a + b will give 5
    Subtraction – Subtracts right hand operand from left hand operand a – b will give -1
    * Multiplication – Multiplies values on either side of the operator a * b will give 6
    / Division – Divides left hand operand by right hand operand b / a will give 1
    % Modulus – Divides left hand operand by right hand operand and returns remainder b % a will give 1
    ^ Exponentiation – This gives the exponent value of the right hand operand a ^ b will give 8
    |/ square root |/ 25.0 will give 5
    ||/ Cube root ||/ 27.0 will give 3
    ! factorial 5 ! will give 120
    !! factorial (prefix operator) !! 5 will give 120

    PostgreSQL Comparison Operators

    Assume variable a holds 10 and variable b holds 20, then −

    Operator Description Example
    = Checks if the values of two operands are equal or not, if yes then condition becomes true. (a = b) is not true.
    != Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (a != b) is true.
    <> Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (a <> b) is true.
    > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (a > b) is not true.
    < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (a < b) is true.
    >= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (a >= b) is not true.
    <= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (a <= b) is true.

    PostgreSQL Logical Operators

    Here is a list of all the logical operators available in PostgresSQL.

    S. No. Operator & Description
    1

    AND

    The AND operator allows the existence of multiple conditions in a PostgresSQL statement”s WHERE clause.

    2

    NOT

    The NOT operator reverses the meaning of the logical operator with which it is used. Eg. NOT EXISTS, NOT BETWEEN, NOT IN etc. This is negate operator.

    3

    OR

    The OR operator is used to combine multiple conditions in a PostgresSQL statement”s WHERE clause.

    PostgreSQL Bit String Operators

    Bitwise operator works on bits and performs bit-by-bit operation. The truth table for & and | is as follows −

    p q p & q p | q
    0 0 0 0
    0 1 0 1
    1 1 1 1
    1 0 0 1

    Assume if A = 60; and B = 13; now in binary format they will be as follows −

    A = 0011 1100

    B = 0000 1101

    —————–

    A&B = 0000 1100

    A|B = 0011 1101

    ~A  = 1100 0011

    The Bitwise operators supported by PostgreSQL are listed in the following table −

    Operator Description Example
    & Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100
    | Binary OR Operator copies a bit if it exists in either operand. (A | B) will give 61 which is 0011 1101
    ~ Binary Ones Complement Operator is unary and has the effect of ”flipping” bits. (~A ) will give -61 which is 1100 0011 in 2”s complement form due to a signed binary number.
    << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 will give 240 which is 1111 0000
    >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 will give 15 which is 0000 1111
    # bitwise XOR. A # B will give 49 which is 00110001

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

    PostgreSQL – Expressions



    An expression is a combination of one or more values, operators, and PostgresSQL functions that evaluate to a value.

    PostgreSQL EXPRESSIONS are like formulas and they are written in query language. You can also use to query the database for specific set of data.

    Syntax

    Consider the basic syntax of the SELECT statement as follows −

    SELECT column1, column2, columnN
    FROM table_name
    WHERE [CONDITION | EXPRESSION];
    

    There are different types of PostgreSQL expressions, which are mentioned below −

    PostgreSQL – Boolean Expressions

    PostgreSQL Boolean Expressions fetch the data on the basis of matching single value. Following is the syntax −

    SELECT column1, column2, columnN
    FROM table_name
    WHERE SINGLE VALUE MATCHTING EXPRESSION;
    

    Consider the table having records as follows −

    testdb# select * from COMPANY;
     id | name  | age | address   | salary
    ----+-------+-----+-----------+--------
      1 | Paul  |  32 | California|  20000
      2 | Allen |  25 | Texas     |  15000
      3 | Teddy |  23 | Norway    |  20000
      4 | Mark  |  25 | Rich-Mond |  65000
      5 | David |  27 | Texas     |  85000
      6 | Kim   |  22 | South-Hall|  45000
      7 | James |  24 | Houston   |  10000
    (7 rows)
    

    Here is the simple example showing usage of PostgreSQL Boolean Expressions −

    testdb=# SELECT * FROM COMPANY WHERE SALARY = 10000;
    

    The above given PostgreSQL statement will produce the following result −

     id | name  | age | address  | salary
    ----+-------+-----+----------+--------
      7 | James |  24 | Houston  |  10000
    (1 row)
    

    PostgreSQL – Numeric Expression

    These expressions are used to perform any mathematical operation in any query. Following is the syntax −

    SELECT numerical_expression as  OPERATION_NAME
    [FROM table_name WHERE CONDITION] ;
    

    Here numerical_expression is used for mathematical expression or any formula. Following is a simple example showing usage of SQL Numeric Expressions −

    testdb=# SELECT (15 + 6) AS ADDITION ;
    

    The above given PostgreSQL statement will produce the following result −

     addition
    ----------
           21
    (1 row)
    

    There are several built-in functions like avg(), sum(), count() to perform what is known as aggregate data calculations against a table or a specific table column.

    testdb=# SELECT COUNT(*) AS "RECORDS" FROM COMPANY;
    

    The above given PostgreSQL statement will produce the following result −

     RECORDS
    ---------
           7
    (1 row)
    

    PostgreSQL – Date Expressions

    Date Expressions return the current system date and time values and these expressions are used in various data manipulations.

    testdb=#  SELECT CURRENT_TIMESTAMP;
    

    The above given PostgreSQL statement will produce the following result −

                  now
    -------------------------------
     2013-05-06 14:38:28.078+05:30
    (1 row)
    

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

    PostgreSQL – INSERT Query



    The PostgreSQL INSERT INTO statement allows one to insert new rows into a table. One can insert a single row at a time or several rows as a result of a query.

    Syntax

    Basic syntax of INSERT INTO statement is as follows −

    INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
    VALUES (value1, value2, value3,...valueN);
    
    • Here, column1, column2,…columnN are the names of the columns in the table into which you want to insert data.

    • The target column names can be listed in any order. The values supplied by the VALUES clause or query are associated with the explicit or implicit column list left-to-right.

    You may not need to specify the column(s) name in the SQL query if you are adding values for all the columns of the table. However, make sure the order of the values is in the same order as the columns in the table. The SQL INSERT INTO syntax would be as follows −

    INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);
    

    Output

    The following table summarizes the output messages and their meaning −

    S. No. Output Message & Description
    1

    INSERT oid 1

    Message returned if only one row was inserted. oid is the numeric OID of the inserted row.

    2

    INSERT 0 #

    Message returned if more than one rows were inserted. # is the number of rows inserted.

    Examples

    Let us create COMPANY table in testdb as follows −

    CREATE TABLE COMPANY(
       ID INT PRIMARY KEY     NOT NULL,
       NAME           TEXT    NOT NULL,
       AGE            INT     NOT NULL,
       ADDRESS        CHAR(50),
       SALARY         REAL,
       JOIN_DATE	  DATE
    );
    

    The following example inserts a row into the COMPANY table −

    INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (1, ''Paul'', 32, ''California'', 20000.00,''2001-07-13'');
    

    The following example is to insert a row; here salary column is omitted and therefore it will have the default value −

    INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,JOIN_DATE) VALUES (2, ''Allen'', 25, ''Texas'', ''2007-12-13'');
    

    The following example uses the DEFAULT clause for the JOIN_DATE column rather than specifying a value −

    INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (3, ''Teddy'', 23, ''Norway'', 20000.00, DEFAULT );
    

    The following example inserts multiple rows using the multirow VALUES syntax −

    INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY,JOIN_DATE) VALUES (4, ''Mark'', 25, ''Rich-Mond '', 65000.00, ''2007-12-13'' ), (5, ''David'', 27, ''Texas'', 85000.00, ''2007-12-13'');
    

    All the above statements would create the following records in COMPANY table. The next chapter will teach you how to display all these records from a table.

    
    ID        NAME        AGE        ADDRESS     SALARY	  JOIN_DATE
    ----      ----------  -----      ----------  -------      --------
    1         Paul        32         California  20000.0      2001-07-13
    2         Allen       25         Texas                    2007-12-13
    3         Teddy       23         Norway      20000.0
    4         Mark        25         Rich-Mond   65000.0      2007-12-13
    5         David       27         Texas       85000.0      2007-12-13
    
    

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

    PostgreSQL – SELECT Query



    PostgreSQL SELECT statement is used to fetch the data from a database table, which returns data in the form of result table. These result tables are called result-sets.

    Syntax

    The basic syntax of SELECT statement is as follows −

    SELECT column1, column2, columnN FROM table_name;
    

    Here, column1, column2…are the fields of a table, whose values you want to fetch. If you want to fetch all the fields available in the field then you can use the following syntax −

    SELECT * FROM table_name;
    

    Example

    Consider the table having records as follows −

     id | name  | age | address   | salary
    ----+-------+-----+-----------+--------
      1 | Paul  |  32 | California|  20000
      2 | Allen |  25 | Texas     |  15000
      3 | Teddy |  23 | Norway    |  20000
      4 | Mark  |  25 | Rich-Mond |  65000
      5 | David |  27 | Texas     |  85000
      6 | Kim   |  22 | South-Hall|  45000
      7 | James |  24 | Houston   |  10000
    (7 rows)
    

    The following is an example, which would fetch ID, Name and Salary fields of the customers available in CUSTOMERS table −

    testdb=# SELECT ID, NAME, SALARY FROM COMPANY ;
    

    This would produce the following result −

      id | name  | salary
     ----+-------+--------
       1 | Paul  |  20000
       2 | Allen |  15000
       3 | Teddy |  20000
       4 | Mark  |  65000
       5 | David |  85000
       6 | Kim   |  45000
       7 | James |  10000
    (7 rows)
    

    If you want to fetch all the fields of CUSTOMERS table, then use the following query −

    testdb=# SELECT * FROM COMPANY;
    

    This would produce the following result −

     id | name  | age | address   | salary
    ----+-------+-----+-----------+--------
      1 | Paul  |  32 | California|  20000
      2 | Allen |  25 | Texas     |  15000
      3 | Teddy |  23 | Norway    |  20000
      4 | Mark  |  25 | Rich-Mond |  65000
      5 | David |  27 | Texas     |  85000
      6 | Kim   |  22 | South-Hall|  45000
      7 | James |  24 | Houston   |  10000
    (7 rows)
    

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

    PostgreSQL – Schema



    A schema is a named collection of tables. A schema can also contain views, indexes, sequences, data types, operators, and functions. Schemas are analogous to directories at the operating system level, except that schemas cannot be nested. PostgreSQL statement CREATE SCHEMA creates a schema.

    Syntax

    The basic syntax of CREATE SCHEMA is as follows −

    CREATE SCHEMA name;
    

    Where name is the name of the schema.

    Syntax to Create Table in Schema

    The basic syntax to create table in schema is as follows −

    CREATE TABLE myschema.mytable (
    ...
    );
    

    Example

    Let us see an example for creating a schema. Connect to the database testdb and create a schema myschema as follows −

    testdb=# create schema myschema;
    CREATE SCHEMA
    

    The message “CREATE SCHEMA” signifies that the schema is created successfully.

    Now, let us create a table in the above schema as follows −

    testdb=# create table myschema.company(
       ID   INT              NOT NULL,
       NAME VARCHAR (20)     NOT NULL,
       AGE  INT              NOT NULL,
       ADDRESS  CHAR (25),
       SALARY   DECIMAL (18, 2),
       PRIMARY KEY (ID)
    );
    

    This will create an empty table. You can verify the table created with the command given below −

    testdb=# select * from myschema.company;
    

    This would produce the following result −

     id | name | age | address | salary
    ----+------+-----+---------+--------
    (0 rows)
    

    Syntax to Drop Schema

    To drop a schema if it is empty (all objects in it have been dropped), use the command −

    DROP SCHEMA myschema;
    

    To drop a schema including all contained objects, use the command −

    DROP SCHEMA myschema CASCADE;
    

    Advantages of using a Schema

    • It allows many users to use one database without interfering with each other.

    • It organizes database objects into logical groups to make them more manageable.

    • Third-party applications can be put into separate schemas so they do not collide with the names of other objects.


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

    PostgreSQL – Syntax



    This chapter provides a list of the PostgreSQL SQL commands, followed by the precise syntax rules for each of these commands. This set of commands is taken from the psql command-line tool. Now that you have Postgres installed, open the psql as −

    Program Files → PostgreSQL 9.2 → SQL Shell(psql).

    Using psql, you can generate a complete list of commands by using the help command. For the syntax of a specific command, use the following command −

     postgres-# help <command_name>
    

    The SQL Statement

    An SQL statement is comprised of tokens where each token can represent either a keyword, identifier, quoted identifier, constant, or special character symbol. The table given below uses a simple SELECT statement to illustrate a basic, but complete, SQL statement and its components.

    SELECT id, name FROM states
    Token Type Keyword Identifiers Keyword Identifier
    Description Command Id and name columns Clause Table name

    PostgreSQL SQL commands

    ABORT

    Abort the current transaction.

    ABORT [ WORK | TRANSACTION ]
    

    ALTER AGGREGATE

    Change the definition of an aggregate function.

    ALTER AGGREGATE name ( type ) RENAME TO new_name
    ALTER AGGREGATE name ( type ) OWNER TO new_owner
    

    ALTER CONVERSION

    Change the definition of a conversion.

    ALTER CONVERSION name RENAME TO new_name
    ALTER CONVERSION name OWNER TO new_owner
    

    ALTER DATABASE

    Change a database specific parameter.

    ALTER DATABASE name SET parameter { TO | = } { value | DEFAULT }
    ALTER DATABASE name RESET parameter
    ALTER DATABASE name RENAME TO new_name
    ALTER DATABASE name OWNER TO new_owner
    

    ALTER DOMAIN

    Change the definition of a domain specific parameter.

    ALTER DOMAIN name { SET DEFAULT expression | DROP DEFAULT }
    ALTER DOMAIN name { SET | DROP } NOT NULL
    ALTER DOMAIN name ADD domain_constraint
    ALTER DOMAIN name DROP CONSTRAINT constraint_name [ RESTRICT | CASCADE ]
    ALTER DOMAIN name OWNER TO new_owner
    

    ALTER FUNCTION

    Change the definition of a function.

    ALTER FUNCTION name ( [ type [, ...] ] ) RENAME TO new_name
    ALTER FUNCTION name ( [ type [, ...] ] ) OWNER TO new_owner
    

    ALTER GROUP

    Change a user group.

    ALTER GROUP groupname ADD USER username [, ... ]
    ALTER GROUP groupname DROP USER username [, ... ]
    ALTER GROUP groupname RENAME TO new_name
    

    ALTER INDEX

    Change the definition of an index.

    ALTER INDEX name OWNER TO new_owner
    ALTER INDEX name SET TABLESPACE indexspace_name
    ALTER INDEX name RENAME TO new_name
    

    ALTER LANGUAGE

    Change the definition of a procedural language.

    ALTER LANGUAGE name RENAME TO new_name
    

    ALTER OPERATOR

    Change the definition of an operator.

    ALTER OPERATOR name ( { lefttype | NONE }, { righttype | NONE } )
    OWNER TO new_owner
    

    ALTER OPERATOR CLASS

    Change the definition of an operator class.

    ALTER OPERATOR CLASS name USING index_method RENAME TO new_name
    ALTER OPERATOR CLASS name USING index_method OWNER TO new_owner
    

    ALTER SCHEMA

    Change the definition of a schema.

    ALTER SCHEMA name RENAME TO new_name
    ALTER SCHEMA name OWNER TO new_owner
    

    ALTER SEQUENCE

    Change the definition of a sequence generator.

    ALTER SEQUENCE name [ INCREMENT [ BY ] increment ]
    [ MINVALUE minvalue | NO MINVALUE ]
    [ MAXVALUE maxvalue | NO MAXVALUE ]
    [ RESTART [ WITH ] start ] [ CACHE cache ] [ [ NO ] CYCLE ]
    

    ALTER TABLE

    Change the definition of a table.

    ALTER TABLE [ ONLY ] name [ * ]
    action [, ... ]
    ALTER TABLE [ ONLY ] name [ * ]
    RENAME [ COLUMN ] column TO new_column
    ALTER TABLE name
    RENAME TO new_name
    

    Where action is one of the following lines −

    ADD [ COLUMN ] column_type [ column_constraint [ ... ] ]
    DROP [ COLUMN ] column [ RESTRICT | CASCADE ]
    ALTER [ COLUMN ] column TYPE type [ USING expression ]
    ALTER [ COLUMN ] column SET DEFAULT expression
    ALTER [ COLUMN ] column DROP DEFAULT
    ALTER [ COLUMN ] column { SET | DROP } NOT NULL
    ALTER [ COLUMN ] column SET STATISTICS integer
    ALTER [ COLUMN ] column SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
    ADD table_constraint
    DROP CONSTRAINT constraint_name [ RESTRICT | CASCADE ]
    CLUSTER ON index_name
    SET WITHOUT CLUSTER
    SET WITHOUT OIDS
    OWNER TO new_owner
    SET TABLESPACE tablespace_name
    

    ALTER TABLESPACE

    Change the definition of a tablespace.

    ALTER TABLESPACE name RENAME TO new_name
    ALTER TABLESPACE name OWNER TO new_owner
    

    ALTER TRIGGER

    Change the definition of a trigger.

    ALTER TRIGGER name ON table RENAME TO new_name
    

    ALTER TYPE

    Change the definition of a type.

    ALTER TYPE name OWNER TO new_owner
    

    ALTER USER

    Change a database user account.

    ALTER USER name [ [ WITH ] option [ ... ] ]
    ALTER USER name RENAME TO new_name
    ALTER USER name SET parameter { TO | = } { value | DEFAULT }
    ALTER USER name RESET parameter
    

    Where option can be −

    [ ENCRYPTED | UNENCRYPTED ] PASSWORD ''password''
    | CREATEDB | NOCREATEDB
    | CREATEUSER | NOCREATEUSER
    | VALID UNTIL ''abstime''
    

    ANALYZE

    Collect statistics about a database.

    ANALYZE [ VERBOSE ] [ table [ (column [, ...] ) ] ]
    

    BEGIN

    Start a transaction block.

    BEGIN [ WORK | TRANSACTION ] [ transaction_mode [, ...] ]
    

    Where transaction_mode is one of −

    ISOLATION LEVEL {
       SERIALIZABLE | REPEATABLE READ | READ COMMITTED
       | READ UNCOMMITTED
    }
    READ WRITE | READ ONLY
    

    CHECKPOINT

    Force a transaction log checkpoint.

    CHECKPOINT
    

    CLOSE

    Close a cursor.

    CLOSE name
    

    CLUSTER

    Cluster a table according to an index.

    CLUSTER index_name ON table_name
    CLUSTER table_name
    CLUSTER
    

    COMMENT

    Define or change the comment of an object.

    COMMENT ON {
       TABLE object_name |
       COLUMN table_name.column_name |
       AGGREGATE agg_name (agg_type) |
       CAST (source_type AS target_type) |
       CONSTRAINT constraint_name ON table_name |
       CONVERSION object_name |
       DATABASE object_name |
       DOMAIN object_name |
       FUNCTION func_name (arg1_type, arg2_type, ...) |
       INDEX object_name |
       LARGE OBJECT large_object_oid |
       OPERATOR op (left_operand_type, right_operand_type) |
       OPERATOR CLASS object_name USING index_method |
       [ PROCEDURAL ] LANGUAGE object_name |
       RULE rule_name ON table_name |
       SCHEMA object_name |
       SEQUENCE object_name |
       TRIGGER trigger_name ON table_name |
       TYPE object_name |
       VIEW object_name
    }
    IS ''text''
    

    COMMIT

    Commit the current transaction.

    COMMIT [ WORK | TRANSACTION ]
    

    COPY

    Copy data between a file and a table.

    COPY table_name [ ( column [, ...] ) ]
    FROM { ''filename'' | STDIN }
    [ WITH ]
    [ BINARY ]
    [ OIDS ]
    [ DELIMITER [ AS ] ''delimiter'' ]
    [ NULL [ AS ] ''null string'' ]
    [ CSV [ QUOTE [ AS ] ''quote'' ]
    [ ESCAPE [ AS ] ''escape'' ]
    [ FORCE NOT NULL column [, ...] ]
    COPY table_name [ ( column [, ...] ) ]
    TO { ''filename'' | STDOUT }
    [ [ WITH ]
    [ BINARY ]
    [ OIDS ]
    [ DELIMITER [ AS ] ''delimiter'' ]
    [ NULL [ AS ] ''null string'' ]
    [ CSV [ QUOTE [ AS ] ''quote'' ]
    [ ESCAPE [ AS ] ''escape'' ]
    [ FORCE QUOTE column [, ...] ]
    

    CREATE AGGREGATE

    Define a new aggregate function.

    CREATE AGGREGATE name (
       BASETYPE = input_data_type,
       SFUNC = sfunc,
       STYPE = state_data_type
       [, FINALFUNC = ffunc ]
       [, INITCOND = initial_condition ]
    )
    

    CREATE CAST

    Define a new cast.

    CREATE CAST (source_type AS target_type)
    WITH FUNCTION func_name (arg_types)
    [ AS ASSIGNMENT | AS IMPLICIT ]
    CREATE CAST (source_type AS target_type)
    WITHOUT FUNCTION
    [ AS ASSIGNMENT | AS IMPLICIT ]
    

    CREATE CONSTRAINT TRIGGER

    Define a new constraint trigger.

    CREATE CONSTRAINT TRIGGER name
    AFTER events ON
    table_name constraint attributes
    FOR EACH ROW EXECUTE PROCEDURE func_name ( args )
    

    CREATE CONVERSION

    Define a new conversion.

    CREATE [DEFAULT] CONVERSION name
    FOR source_encoding TO dest_encoding FROM func_name
    

    CREATE DATABASE

    Create a new database.

    CREATE DATABASE name
    [ [ WITH ] [ OWNER [=] db_owner ]
       [ TEMPLATE [=] template ]
       [ ENCODING [=] encoding ]
       [ TABLESPACE [=] tablespace ]
    ]
    

    CREATE DOMAIN

    Define a new domain.

    CREATE DOMAIN name [AS] data_type
    [ DEFAULT expression ]
    [ constraint [ ... ] ]
    

    Where constraint is −

    [ CONSTRAINT constraint_name ]
    { NOT NULL | NULL | CHECK (expression) }
    

    CREATE FUNCTION

    Define a new function.

    CREATE [ OR REPLACE ] FUNCTION name ( [ [ arg_name ] arg_type [, ...] ] )
    RETURNS ret_type
    { LANGUAGE lang_name
       | IMMUTABLE | STABLE | VOLATILE
       | CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT
       | [ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER
       | AS ''definition''
       | AS ''obj_file'', ''link_symbol''
    } ...
    [ WITH ( attribute [, ...] ) ]
    

    CREATE GROUP

    Define a new user group.

    CREATE GROUP name [ [ WITH ] option [ ... ] ]
    Where option can be:
    SYSID gid
    | USER username [, ...]
    

    CREATE INDEX

    Define a new index.

    CREATE [ UNIQUE ] INDEX name ON table [ USING method ]
    ( { column | ( expression ) } [ opclass ] [, ...] )
    [ TABLESPACE tablespace ]
    [ WHERE predicate ]
    

    CREATE LANGUAGE

    Define a new procedural language.

    CREATE [ TRUSTED ] [ PROCEDURAL ] LANGUAGE name
    HANDLER call_handler [ VALIDATOR val_function ]
    

    CREATE OPERATOR

    Define a new operator.

    CREATE OPERATOR name (
       PROCEDURE = func_name
       [, LEFTARG = left_type ] [, RIGHTARG = right_type ]
       [, COMMUTATOR = com_op ] [, NEGATOR = neg_op ]
       [, RESTRICT = res_proc ] [, JOIN = join_proc ]
       [, HASHES ] [, MERGES ]
       [, SORT1 = left_sort_op ] [, SORT2 = right_sort_op ]
       [, LTCMP = less_than_op ] [, GTCMP = greater_than_op ]
    )
    

    CREATE OPERATOR CLASS

    Define a new operator class.

    CREATE OPERATOR CLASS name [ DEFAULT ] FOR TYPE data_type
    USING index_method AS
    { OPERATOR strategy_number operator_name [ ( op_type, op_type ) ] [ RECHECK ]
       | FUNCTION support_number func_name ( argument_type [, ...] )
       | STORAGE storage_type
    } [, ... ]
    

    CREATE RULE

    Define a new rewrite rule.

    CREATE [ OR REPLACE ] RULE name AS ON event
    TO table [ WHERE condition ]
    DO [ ALSO | INSTEAD ] { NOTHING | command | ( command ; command ... ) }
    

    CREATE SCHEMA

    Define a new schema.

    CREATE SCHEMA schema_name
    [ AUTHORIZATION username ] [ schema_element [ ... ] ]
    CREATE SCHEMA AUTHORIZATION username
    [ schema_element [ ... ] ]
    

    CREATE SEQUENCE

    Define a new sequence generator.

    CREATE [ TEMPORARY | TEMP ] SEQUENCE name
    [ INCREMENT [ BY ] increment ]
    [ MINVALUE minvalue | NO MINVALUE ]
    [ MAXVALUE maxvalue | NO MAXVALUE ]
    [ START [ WITH ] start ] [ CACHE cache ] [ [ NO ] CYCLE ]
    

    CREATE TABLE

    Define a new table.

    CREATE [ [ GLOBAL | LOCAL ] {
       TEMPORARY | TEMP } ] TABLE table_name ( {
          column_name data_type [ DEFAULT default_expr ] [ column_constraint [ ... ] ]
          | table_constraint
          | LIKE parent_table [ { INCLUDING | EXCLUDING } DEFAULTS ]
       } [, ... ]
    )
    [ INHERITS ( parent_table [, ... ] ) ]
    [ WITH OIDS | WITHOUT OIDS ]
    [ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP } ]
    [ TABLESPACE tablespace ]
    

    Where column_constraint is −

    [ CONSTRAINT constraint_name ] {
       NOT NULL |
       NULL |
       UNIQUE [ USING INDEX TABLESPACE tablespace ] |
       PRIMARY KEY [ USING INDEX TABLESPACE tablespace ] |
       CHECK (expression) |
       REFERENCES ref_table [ ( ref_column ) ]
       [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
       [ ON DELETE action ] [ ON UPDATE action ]
    }
    [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
    

    And table_constraint is −

    [ CONSTRAINT constraint_name ]
    { UNIQUE ( column_name [, ... ] ) [ USING INDEX TABLESPACE tablespace ] |
    PRIMARY KEY ( column_name [, ... ] ) [ USING INDEX TABLESPACE tablespace ] |
    CHECK ( expression ) |
    FOREIGN KEY ( column_name [, ... ] )
    REFERENCES ref_table [ ( ref_column [, ... ] ) ]
    [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
    [ ON DELETE action ] [ ON UPDATE action ] }
    [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
    

    CREATE TABLE AS

    Define a new table from the results of a query.

    CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE table_name
    [ (column_name [, ...] ) ] [ [ WITH | WITHOUT ] OIDS ]
    AS query
    

    CREATE TABLESPACE

    Define a new tablespace.

    CREATE TABLESPACE tablespace_name [ OWNER username ] LOCATION ''directory''
    

    CREATE TRIGGER

    Define a new trigger.

    CREATE TRIGGER name { BEFORE | AFTER } { event [ OR ... ] }
    ON table [ FOR [ EACH ] { ROW | STATEMENT } ]
    EXECUTE PROCEDURE func_name ( arguments )
    

    CREATE TYPE

    Define a new data type.

    CREATE TYPE name AS
    ( attribute_name data_type [, ... ] )
    CREATE TYPE name (
    INPUT = input_function,
    OUTPUT = output_function
    [, RECEIVE = receive_function ]
    [, SEND = send_function ]
    [, ANALYZE = analyze_function ]
    [, INTERNALLENGTH = { internal_length | VARIABLE } ]
    [, PASSEDBYVALUE ]
    [, ALIGNMENT = alignment ]
    [, STORAGE = storage ]
    [, DEFAULT = default ]
    [, ELEMENT = element ]
    [, DELIMITER = delimiter ]
    )
    

    CREATE USER

    Define a new database user account.

    CREATE USER name [ [ WITH ] option [ ... ] ]
    

    Where option can be −

    SYSID uid
    | [ ENCRYPTED | UNENCRYPTED ] PASSWORD ''password''
    | CREATEDB | NOCREATEDB
    | CREATEUSER | NOCREATEUSER
    | IN GROUP group_name [, ...]
    | VALID UNTIL ''abs_time''
    

    CREATE VIEW

    Define a new view.

    CREATE [ OR REPLACE ] VIEW name [ ( column_name [, ...] ) ] AS query
    

    DEALLOCATE

    Deallocate a prepared statement.

    DEALLOCATE [ PREPARE ] plan_name
    

    DECLARE

    Define a cursor.

    DECLARE name [ BINARY ] [ INSENSITIVE ] [ [ NO ] SCROLL ]
    CURSOR [ { WITH | WITHOUT } HOLD ] FOR query
    [ FOR { READ ONLY | UPDATE [ OF column [, ...] ] } ]
    

    DELETE

    Delete rows of a table.

    DELETE FROM [ ONLY ] table [ WHERE condition ]
    

    DROP AGGREGATE

    Remove an aggregate function.

    DROP AGGREGATE name ( type ) [ CASCADE | RESTRICT ]
    

    DROP CAST

    Remove a cast.

    DROP CAST (source_type AS target_type) [ CASCADE | RESTRICT ]
    

    DROP CONVERSION

    Remove a conversion.

    DROP CONVERSION name [ CASCADE | RESTRICT ]
    

    DROP DATABASE

    Remove a database.

    DROP DATABASE name
    

    DROP DOMAIN

    Remove a domain.

    DROP DOMAIN name [, ...] [ CASCADE | RESTRICT ]
    

    DROP FUNCTION

    Remove a function.

    DROP FUNCTION name ( [ type [, ...] ] ) [ CASCADE | RESTRICT ]
    

    DROP GROUP

    Remove a user group.

    DROP GROUP name
    

    DROP INDEX

    Remove an index.

    DROP INDEX name [, ...] [ CASCADE | RESTRICT ]
    

    DROP LANGUAGE

    Remove a procedural language.

    DROP [ PROCEDURAL ] LANGUAGE name [ CASCADE | RESTRICT ]
    

    DROP OPERATOR

    Remove an operator.

    DROP OPERATOR name ( { left_type | NONE }, { right_type | NONE } )
    [ CASCADE | RESTRICT ]
    

    DROP OPERATOR CLASS

    Remove an operator class.

    DROP OPERATOR CLASS name USING index_method [ CASCADE | RESTRICT ]
    

    DROP RULE

    Remove a rewrite rule.

    DROP RULE name ON relation [ CASCADE | RESTRICT ]
    

    DROP SCHEMA

    Remove a schema.

    DROP SCHEMA name [, ...] [ CASCADE | RESTRICT ]
    

    DROP SEQUENCE

    Remove a sequence.

    DROP SEQUENCE name [, ...] [ CASCADE | RESTRICT ]
    

    DROP TABLE

    Remove a table.

    DROP TABLE name [, ...] [ CASCADE | RESTRICT ]
    

    DROP TABLESPACE

    Remove a tablespace.

    DROP TABLESPACE tablespace_name
    

    DROP TRIGGER

    Remove a trigger.

    DROP TRIGGER name ON table [ CASCADE | RESTRICT ]
    

    DROP TYPE

    Remove a data type.

    DROP TYPE name [, ...] [ CASCADE | RESTRICT ]
    

    DROP USER

    Remove a database user account.

    DROP USER name
    

    DROP VIEW

    Remove a view.

    DROP VIEW name [, ...] [ CASCADE | RESTRICT ]
    

    END

    Commit the current transaction.

    END [ WORK | TRANSACTION ]
    

    EXECUTE

    Execute a prepared statement.

    EXECUTE plan_name [ (parameter [, ...] ) ]
    

    EXPLAIN

    Show the execution plan of a statement.

    EXPLAIN [ ANALYZE ] [ VERBOSE ] statement
    

    FETCH

    Retrieve rows from a query using a cursor.

    FETCH [ direction { FROM | IN } ] cursor_name
    

    Where direction can be empty or one of −

    NEXT
    PRIOR
    FIRST
    LAST
    ABSOLUTE count
    RELATIVE count
    count
    ALL
    FORWARD
    FORWARD count
    FORWARD ALL
    BACKWARD
    BACKWARD count
    BACKWARD ALL
    

    GRANT

    Define access privileges.

    GRANT { { SELECT | INSERT | UPDATE | DELETE | RULE | REFERENCES | TRIGGER }
    [,...] | ALL [ PRIVILEGES ] }
    ON [ TABLE ] table_name [, ...]
    TO { username | GROUP group_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]
    
    GRANT { { CREATE | TEMPORARY | TEMP } [,...] | ALL [ PRIVILEGES ] }
    ON DATABASE db_name [, ...]
    TO { username | GROUP group_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]
    
    GRANT { CREATE | ALL [ PRIVILEGES ] }
    ON TABLESPACE tablespace_name [, ...]
    TO { username | GROUP group_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]
    
    GRANT { EXECUTE | ALL [ PRIVILEGES ] }
    ON FUNCTION func_name ([type, ...]) [, ...]
    TO { username | GROUP group_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]
    
    GRANT { USAGE | ALL [ PRIVILEGES ] }
    ON LANGUAGE lang_name [, ...]
    TO { username | GROUP group_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]
    
    GRANT { { CREATE | USAGE } [,...] | ALL [ PRIVILEGES ] }
    ON SCHEMA schema_name [, ...]
    TO { username | GROUP group_name | PUBLIC } [, ...] [ WITH GRANT OPTION ]
    

    INSERT

    Create new rows in a table.

    INSERT INTO table [ ( column [, ...] ) ]
    { DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) | query }
    

    LISTEN

    Listen for a notification.

    LISTEN name
    

    LOAD

    Load or reload a shared library file.

    LOAD ''filename''
    

    LOCK

    Lock a table.

    LOCK [ TABLE ] name [, ...] [ IN lock_mode MODE ] [ NOWAIT ]
    

    Where lock_mode is one of −

    ACCESS SHARE | ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE EXCLUSIVE
    | SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE | ACCESS EXCLUSIVE
    

    MOVE

    Position a cursor.

    MOVE [ direction { FROM | IN } ] cursor_name
    

    NOTIFY

    Generate a notification.

    NOTIFY name
    

    PREPARE

    Prepare a statement for execution.

    PREPARE plan_name [ (data_type [, ...] ) ] AS statement
    

    REINDEX

    Rebuild indexes.

    REINDEX { DATABASE | TABLE | INDEX } name [ FORCE ]
    

    RELEASE SAVEPOINT

    Destroy a previously defined savepoint.

    RELEASE [ SAVEPOINT ] savepoint_name
    

    RESET

    Restore the value of a runtime parameter to the default value.

    RESET name
    RESET ALL
    

    REVOKE

    Remove access privileges.

    REVOKE [ GRANT OPTION FOR ]
    { { SELECT | INSERT | UPDATE | DELETE | RULE | REFERENCES | TRIGGER }
    [,...] | ALL [ PRIVILEGES ] }
    ON [ TABLE ] table_name [, ...]
    FROM { username | GROUP group_name | PUBLIC } [, ...]
    [ CASCADE | RESTRICT ]
    
    REVOKE [ GRANT OPTION FOR ]
    { { CREATE | TEMPORARY | TEMP } [,...] | ALL [ PRIVILEGES ] }
    ON DATABASE db_name [, ...]
    FROM { username | GROUP group_name | PUBLIC } [, ...]
    [ CASCADE | RESTRICT ]
    
    REVOKE [ GRANT OPTION FOR ]
    { CREATE | ALL [ PRIVILEGES ] }
    ON TABLESPACE tablespace_name [, ...]
    FROM { username | GROUP group_name | PUBLIC } [, ...]
    [ CASCADE | RESTRICT ]
    
    REVOKE [ GRANT OPTION FOR ]
    { EXECUTE | ALL [ PRIVILEGES ] }
    ON FUNCTION func_name ([type, ...]) [, ...]
    FROM { username | GROUP group_name | PUBLIC } [, ...]
    [ CASCADE | RESTRICT ]
    
    REVOKE [ GRANT OPTION FOR ]
    { USAGE | ALL [ PRIVILEGES ] }
    ON LANGUAGE lang_name [, ...]
    FROM { username | GROUP group_name | PUBLIC } [, ...]
    [ CASCADE | RESTRICT ]
    
    REVOKE [ GRANT OPTION FOR ]
    { { CREATE | USAGE } [,...] | ALL [ PRIVILEGES ] }
    ON SCHEMA schema_name [, ...]
    FROM { username | GROUP group_name | PUBLIC } [, ...]
    [ CASCADE | RESTRICT ]
    

    ROLLBACK

    Abort the current transaction.

    ROLLBACK [ WORK | TRANSACTION ]
    

    ROLLBACK TO SAVEPOINT

    Roll back to a savepoint.

    ROLLBACK [ WORK | TRANSACTION ] TO [ SAVEPOINT ] savepoint_name
    

    SAVEPOINT

    Define a new savepoint within the current transaction.

    SAVEPOINT savepoint_name
    

    SELECT

    Retrieve rows from a table or view.

    SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
    * | expression [ AS output_name ] [, ...]
    [ FROM from_item [, ...] ]
    [ WHERE condition ]
    [ GROUP BY expression [, ...] ]
    [ HAVING condition [, ...] ]
    [ { UNION | INTERSECT | EXCEPT } [ ALL ] select ]
    [ ORDER BY expression [ ASC | DESC | USING operator ] [, ...] ]
    [ LIMIT { count | ALL } ]
    [ OFFSET start ]
    [ FOR UPDATE [ OF table_name [, ...] ] ]
    
    Where from_item can be one of:
    [ ONLY ] table_name [ * ] [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
    ( select ) [ AS ] alias [ ( column_alias [, ...] ) ]
    function_name ( [ argument [, ...] ] )
    [ AS ] alias [ ( column_alias [, ...] | column_definition [, ...] ) ]
    function_name ( [ argument [, ...] ] ) AS ( column_definition [, ...] )
    from_item [ NATURAL ] join_type from_item
    [ ON join_condition | USING ( join_column [, ...] ) ]
    

    SELECT INTO

    Define a new table from the results of a query.

    SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
    * | expression [ AS output_name ] [, ...]
    INTO [ TEMPORARY | TEMP ] [ TABLE ] new_table
    [ FROM from_item [, ...] ]
    [ WHERE condition ]
    [ GROUP BY expression [, ...] ]
    [ HAVING condition [, ...] ]
    [ { UNION | INTERSECT | EXCEPT } [ ALL ] select ]
    [ ORDER BY expression [ ASC | DESC | USING operator ] [, ...] ]
    [ LIMIT { count | ALL } ]
    [ OFFSET start ]
    [ FOR UPDATE [ OF table_name [, ...] ] ]
    

    SET

    Change a runtime parameter.

    SET [ SESSION | LOCAL ] name { TO | = } { value | ''value'' | DEFAULT }
    SET [ SESSION | LOCAL ] TIME ZONE { time_zone | LOCAL | DEFAULT }
    

    SET CONSTRAINTS

    Set constraint checking modes for the current transaction.

    SET CONSTRAINTS { ALL | name [, ...] } { DEFERRED | IMMEDIATE }
    

    SET SESSION AUTHORIZATION

    Set the session user identifier and the current user identifier of the current session.

    SET [ SESSION | LOCAL ] SESSION AUTHORIZATION username
    SET [ SESSION | LOCAL ] SESSION AUTHORIZATION DEFAULT
    RESET SESSION AUTHORIZATION
    

    SET TRANSACTION

    Set the characteristics of the current transaction.

    SET TRANSACTION transaction_mode [, ...]
    SET SESSION CHARACTERISTICS AS TRANSACTION transaction_mode [, ...]
    

    Where transaction_mode is one of −

    ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED
    | READ UNCOMMITTED }
    READ WRITE | READ ONLY
    

    SHOW

    Show the value of a runtime parameter.

    SHOW name
    SHOW ALL
    

    START TRANSACTION

    Start a transaction block.

    START TRANSACTION [ transaction_mode [, ...] ]
    

    Where transaction_mode is one of −

    ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED
    | READ UNCOMMITTED }
    READ WRITE | READ ONLY
    

    TRUNCATE

    Empty a table.

    TRUNCATE [ TABLE ] name
    

    UNLISTEN

    Stop listening for a notification.

    UNLISTEN { name | * }
    

    UPDATE

    Update rows of a table.

    UPDATE [ ONLY ] table SET column = { expression | DEFAULT } [, ...]
    [ FROM from_list ]
    [ WHERE condition ]
    

    VACUUM

    Garbage-collect and optionally analyze a database.

    VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ table ]
    VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] ANALYZE [ table [ (column [, ...] ) ] ]
    

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

    PostgreSQL tutorial

    PostgreSQL Tutorial







    PostgreSQL is a powerful, open source object-relational database system. It has more than 15 years of active development and a proven architecture that has earned it a strong reputation for reliability, data integrity, and correctness. PostgreSQL runs on all major operating systems, including Linux, UNIX (AIX, BSD, HP-UX, SGI IRIX, Mac OS X, Solaris, Tru64), and Windows. This tutorial will give you quick start with PostgreSQL and make you comfortable with PostgreSQL programming.

    Audience

    This tutorial has been prepared for the beginners to help them understand the basic to advanced concepts related to PostgreSQL Database.

    Prerequisites

    Before you start practicing with various types of examples given in this reference, I”m making an assumption that you are already aware of what a database is, especially RDBMS and what a computer language is.

    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