Category: t Sql

  • Khóa học miễn phí T-SQL – Overview nhận dự án làm có lương

    T-SQL – Overview



    In 1970”s the product called ”SEQUEL”, structured English query language, developed by IBM and later SEQUEL was renamed to ”SQL” which stands for Structured Query Language.

    In 1986, SQL was approved by ANSI (American national Standards Institute) and in 1987, it was approved by ISO (International Standards Organization).

    SQL is a structure query language which is a common database language for all RDBMS products. Different RDBMS product vendors have developed their own database language by extending SQL for their own RDBMS products.

    T-SQL stands for Transact Structure Query Language which is a Microsoft product and is an extension of SQL Language.

    Example

    MS SQL Server – SQLT-SQL

    ORACLE – SQLPL-SQL


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

    T-SQL – Drop Tables



    The SQL Server DROP TABLE statement is used to remove a table definition and all data, indexes, triggers, constraints, and permission specifications for that table.

    Note − You have to be careful while using this command because once a table is deleted then all the information available in the table would also be lost forever.

    Syntax

    Following is the basic syntax of DROP TABLE statement −

    DROP TABLE table_name;
    

    Example

    Let us first verify CUSTOMERS table and then we will delete it from the database −

    Exec sp_columns CUSTOMERS;
    

    The above command shows the following table.

    TABLE_QUALIFIER   TABLE_OWNER   TABLE_NAME   COLUMN_NAME   DATA_TYPE   TYPE_NAME
       PRECISION   LENGTH SCALE   RADIX   NULLABLE   REMARKS   COLUMN_DEF   SQL_DATA_TYPE
       SQL_DATETIME_SUB   CHAR_OCTET_LENGTH   ORDINAL_POSITION   IS_NULLABLE   SS_DATA_TYPE
    
    TestDB    dbo    CUSTOMERS   ID        4   int        10   4    0      10     0
       NULL   NULL   4   NULL    NULL      1   NO         56
    
    TestDB    dbo    CUSTOMERS   NAME      12  varchar    20   20   NULL   NULL   0
       NULL   NULL   12   NULL   20        2   NO         39
    
    TestDB    dbo    CUSTOMERS   AGE       4   int        10   4    0      10     0
       NULL   NULL   4   NULL    NULL      3   NO         56
    
    TestDB    dbo    CUSTOMERS   ADDRESS   1   char       25   25   NULL   NULL   1
       NULL   NULL   1   NULL    25        4   YES        39
    
    TestDB    dbo    CUSTOMERS   SALARY   3   decimal     18   20   2      10     1
       NULL   NULL   3   NULL    NULL     5   YES         106
    

    CUSTOMERS table is available in the database, so let us drop it. Following is the command for the same.

    DROP TABLE CUSTOMERS;
    Command(s) completed successfully.
    

    With the above command, you will not get any rows.

    Exec sp_columns CUSTOMERS;
    No rowsdata will be displayed
    

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

    T-SQL – SELECT Statement



    SQL Server 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

    Following is the basic syntax of SELECT statement −

    SELECT column1, column2, columnN FROM table_name;
    

    Where, 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 CUSTOMERS table having the following records −

    ID  NAME       AGE       ADDRESS             SALARY
    1   Ramesh     32        Ahmedabad           2000.00
    2   Khilan     25        Delhi               1500.00
    3   kaushik    23        Kota                2000.00
    4   Chaitali   25        Mumbai              6500.00
    5   Hardik     27        Bhopal              8500.00
    6   Komal      22        MP                  4500.00
    7   Muffy      24        Indore              10000.00
    

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

    SELECT ID, NAME, SALARY FROM CUSTOMERS;
    

    The above command will produce the following output.

    ID  NAME          SALARY
    1   Ramesh        2000.00
    2   Khilan        1500.00
    3   kaushik       2000.00
    4   Chaitali      6500.00
    5   Hardik        8500.00
    6   Komal         4500.00
    7   Muffy         10000.00
    

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

    SELECT * FROM CUSTOMERS;
    

    The above will produce the following output.

    ID  NAME       AGE       ADDRESS              SALARY
    1   Ramesh     32        Ahmedabad            2000.00
    2   Khilan     25        Delhi                1500.00
    3   kaushik    23        Kota                 2000.00
    4   Chaitali   25        Mumbai               6500.00
    5   Hardik     27        Bhopal               8500.00
    6   Komal      22        MP                   4500.00
    7   Muffy      24        Indore               10000.00
    

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

    T-SQL – Create Tables



    Creating a basic table involves naming the table and defining its columns and each column”s data type.

    The SQL Server CREATE TABLE statement is used to create a new table.

    Syntax

    Following is the basic syntax of CREATE TABLE statement −

    CREATE TABLE table_name(
       column1 datatype,
       column2 datatype,
       column3 datatype,
       .....
       columnN datatype,
       PRIMARY KEY( one or more columns ));
    

    CREATE TABLE is the keyword telling the database system what you want to do. In this case, you want to create a new table. The unique name or identifier for the table follows the CREATE TABLE statement. Then in brackets comes the list defining each column in the table and what sort of data type it is. The syntax becomes clearer to understand with the following example.

    A copy of an existing table can be created using a combination of the CREATE TABLE statement and the SELECT statement. You can check complete details at Create Table Using another Table.

    Example

    In this example, let’s create a CUSTOMERS table with ID as primary key and NOT NULL are the constraints showing that these fields cannot be NULL while creating records in this table −

    CREATE TABLE CUSTOMERS(
       ID   INT              NOT NULL,
       NAME VARCHAR (20)     NOT NULL,
       AGE  INT              NOT NULL,
       ADDRESS  CHAR (25) ,
       SALARY   DECIMAL (18, 2),
       PRIMARY KEY (ID));
    

    You can verify if your table has been created successfully by looking at the message displayed by the SQL server, otherwise you can use the following command −

    exec sp_columns CUSTOMERS
    

    The above command produces the following output.

    TABLE_QUALIFIER   TABLE_OWNER   TABLE_NAME   COLUMN_NAME   DATA_TYPE   TYPE_NAME
       PRECISION   LENGTH SCALE   RADIX   NULLABLE   REMARKS   COLUMN_DEF   SQL_DATA_TYPE
       SQL_DATETIME_SUB   CHAR_OCTET_LENGTH   ORDINAL_POSITION   IS_NULLABLE   SS_DATA_TYPE
    
    TestDB    dbo    CUSTOMERS   ID        4    int      10   4    0      10     0
       NULL   NULL   4   NULL    NULL      1    NO       56
    
    TestDB    dbo    CUSTOMERS   NAME      12   varchar  20   20   NULL   NULL   0
       NULL   NULL   12   NULL   20        2    NO       39
    
    TestDB    dbo    CUSTOMERS   AGE       4    int      10   4    0      10     0
       NULL   NULL   4   NULL    NULL      3    NO       56
    
    TestDB    dbo    CUSTOMERS   ADDRESS   1    char     25   25   NULL   NULL   1
       NULL   NULL   1   NULL    25   4    YES  39
    
    TestDB    dbo    CUSTOMERS   SALARY    3    decimal  18   20   2      10     1
       NULL   NULL   3   NULL    NULL      5    YES      106
    

    You can now see that CUSTOMERS table is available in your database which you can use to store required information related to customers.


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

    T-SQL – INSERT Statement



    The SQL Server INSERT INTO statement is used to add new rows of data to a table in the database.

    Syntax

    Following are the two basic syntaxes of INSERT INTO statement.

    INSERT INTO TABLE_NAME [(column1, column2, column3,...columnN)]
    VALUES (value1, value2, value3,...valueN);
    

    Where column1, column2,…columnN are the names of the columns in the table into which you want to insert data.

    You need not specify the column(s) name in the SQL query if you are adding values for all the columns of the table. But make sure the order of the values is in the same order as the columns in the table. Following is the SQL INSERT INTO syntax −

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

    Example

    Following statements will create six records in CUSTOMERS table −

    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
    VALUES (1, ''Ramesh'', 32, ''Ahmedabad'', 2000.00 );
    
    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
    VALUES (2, ''Khilan'', 25, ''Delhi'', 1500.00 );
    
    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
    VALUES (3, ''kaushik'', 23, ''Kota'', 2000.00 );
    
    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
    VALUES (4, ''Chaitali'', 25, ''Mumbai'', 6500.00 );
    
    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
    VALUES (5, ''Hardik'', 27, ''Bhopal'', 8500.00 );
    
    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
    VALUES (6, ''Komal'', 22, ''MP'', 4500.00 );
    

    Syntax

    You can create a record in CUSTOMERS table using second syntax as follows −

    INSERT INTO CUSTOMERS VALUES (7, ''Muffy'', 24, ''Indore'', 10000.00 );
    

    All the above statements will produce the following records in CUSTOMERS table −

    ID  NAME       AGE         ADDRESS              SALARY
    1   Ramesh     32          Ahmedabad            2000.00
    2   Khilan     25          Delhi                1500.00
    3   kaushik    23          Kota                 2000.00
    4   Chaitali   25          Mumbai               6500.00
    5   Hardik     27          Bhopal               8500.00
    6   Komal      22          MP                   4500.00
    7   Muffy      24          Indore               10000.00
    

    Populate One Table Using Another Table

    You can populate data into a table through SELECT statement over another table provided another table has a set of fields, which are required to populate first table. Following is the syntax −

    INSERT INTO first_table_name
       SELECT column1, column2, ...columnN
          FROM second_table_name
          [WHERE condition];
    

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

    T-SQL – UPDATE Statement



    The SQL Server UPDATE Query is used to modify the existing records in a table.

    You can use WHERE clause with UPDATE query to update selected rows otherwise all the rows would be affected.

    Syntax

    Following is the basic syntax of UPDATE query with WHERE clause −

    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 CUSTOMERS table having the following records −

    ID  NAME       AGE       ADDRESS             SALARY
    1   Ramesh     32        Ahmedabad           2000.00
    2   Khilan     25        Delhi               1500.00
    3   kaushik    23        Kota                2000.00
    4   Chaitali   25        Mumbai              6500.00
    5   Hardik     27        Bhopal              8500.00
    6   Komal      22        MP                  4500.00
    7   Muffy      24        Indore              10000.00
    

    Following command is an example, which would update ADDRESS for a customer whose ID is 6 −

    UPDATE CUSTOMERS
    SET ADDRESS = ''Pune''
    WHERE ID = 6;
    

    CUSTOMERS table will now have the following records −

    ID  NAME       AGE       ADDRESS             SALARY
    1   Ramesh     32        Ahmedabad           2000.00
    2   Khilan     25        Delhi               1500.00
    3   kaushik    23        Kota                2000.00
    4   Chaitali   25        Mumbai              6500.00
    5   Hardik     27        Bhopal              8500.00
    6   Komal      22        Pune                4500.00
    7   Muffy      24        Indore              10000.00
    

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

    UPDATE CUSTOMERS
    SET ADDRESS = ''Pune'', SALARY = 1000.00;
    

    CUSTOMERS table will now have the following records.

    ID  NAME       AGE       ADDRESS          SALARY
    1   Ramesh     32        Pune             1000.00
    2   Khilan     25        Pune             1000.00
    3   kaushik    23        Pune             1000.00
    4   Chaitali   25        Pune             1000.00
    5   Hardik     27        Pune             1000.00
    6   Komal      22        Pune             1000.00
    7   Muffy      24        Pune             1000.00
    

    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