Dbms Commands

Embed Size (px)

Citation preview

  • 8/3/2019 Dbms Commands

    1/6

    CREATE TABLE - SQL Command

    CREATE TABLE student (StudentID varchar(6), StudentName Character(20), Age int(2), Dept varchar(20));

    SQL Insert Query:

    INSERT INTO student (StudentID,StudentName,Age,Dept)VALUES('y7cs201','abcd',19,'cse');

    SQL Select Query:

    SELECT table_column1, table_column2, table_column3 FROM tablename;

    Select * from student;

    Select StudentID from student;

    SQL - Update

    The update clause updates column values of a table. Update requires a conditional statement to select the row

    be updated

    UPDATE student SET StudentName = 'Fletcher' WHERE StudentID = 'y7cs201';

    SQL - Alter Table

    The alter clause changes a table by adding, removing, or modifying an existing table column.

    ALTER TABLE student ADD dob varchar(10);

    SQL - Delete

    Entire rows can be deleted from a table using the delete clause.

    DELETE FROM student WHERE StudentID='y6cs203';

    SQL - Order By

    The order by statement allows for table column assortment. It allows for ascending or descending lists of yo

    table column values permitting SQL to reorder your table rows for the purpose of viewing.

    SELECT * FROM student ORDER BY StudentName;

    SELECT * FROM student ORDER BY Age,StudentName;

  • 8/3/2019 Dbms Commands

    2/6

    SQL - Distinct Usage

    Distinct is used to retrieve rows of your database that have unique values for a given column. For example say w

    have a table of employees and in this table of employees we have several job titles from janitors to CEOs. W

    would like to know just how many distinct job titles we have.

    SELECT DISTINCT job_titles FROM student;

    The AND & OR Operators

    The AND operator displays a record if both the first condition and the second condition is true.

    The OR operator displays a record if either the first condition or the second condition is true.

    SELECT * FROM Persons WHERE FirstName='Tove' AND LastName='Svendson'

    SELECT * FROM Persons WHERE FirstName='Tove' OR FirstName='Ola'

    SELECT * FROM Persons WHERE LastName='Svendson' AND (FirstName='Tove' OR FirstName='Ola

    The ORDER BY Keyword

    The ORDER BY keyword is used to sort the result-set by a specified column.

    The ORDER BY keyword sort the records in ascending order by default.

    If you want to sort the records in a descending order, you can use the DESC keyword.

    SQL ORDER BY Syntax

    SELECT column_name(s)

    FROM table_name

    ORDER BY column_name(s) ASC|DESC

    SELECT * FROM Persons ORDER BY LastName

    SELECT * FROM Persons ORDER BY LastName DESC

    The TOP Clause

    The TOP clause is used to specify the number of records to return.

    The TOP clause can be very useful on large tables with thousands of records. Returning a largnumber of records can impact on performance.

    Note: Not all database systems support the TOP clause.

  • 8/3/2019 Dbms Commands

    3/6

    SQL Server Syntax

    SELECT TOP number|percent column_name(s)

    FROM table_name

    MySQL Syntax

    SELECT column_name(s)

    FROM table_name

    LIMIT number

    SELECT * FROM Persons LIMIT 5SELECT column_name(s) FROM table_name WHERE ROWNUM

  • 8/3/2019 Dbms Commands

    4/6

    SELECT * FROM Persons WHERE City LIKE 's%'

    The "%" sign can be used to define wildcards (missing letters in the pattern) both before and aftthe pattern.

    SELECT * FROM Persons WHERE City LIKE '%s'

    SELECT * FROM Persons WHERE City LIKE '%tav%'

    SELECT * FROM Persons WHERE City NOT LIKE '%tav%'

    The IN Operator

    The IN operator allows you to specify multiple values in a WHERE clause.

    SQL IN Syntax

    SELECT column_name(s)

    FROM table_name

    WHERE column_name IN (value1,value2,...)

    SELECT * FROM Persons WHERE LastName IN ('Hansen','Pettersen')

    The BETWEEN operator is used in a WHERE clause to select a range of data between two values.

    The BETWEEN Operator

    The BETWEEN operator selects a range of data between two values. The values can be numbetext, or dates.

    SQL BETWEEN Syntax

    SELECT column_name(s)

    FROM table_name

    WHERE column_name

    BETWEEN value1 AND value2

    Now we want to select the persons with a last name alphabetically between "Hansen" a"Pettersen" from the table above.

    We use the following SELECT statement:

    SELECT * FROM Persons WHERE LastName BETWEEN 'Hansen' AND 'Pettersen'

  • 8/3/2019 Dbms Commands

    5/6

    MySQL Date Functions

    The following table lists the most important built-in date functions in MySQL:

    Function Description

    NOW() Returns the current date and time

    CURDATE() Returns the current date

    CURTIME() Returns the current time

    DATE() Extracts the date part of a date or date/time expression

    EXTRACT() Returns a single part of a date/time

    DATE_ADD() Adds a specified time interval to a date

    DATE_SUB() Subtracts a specified time interval from a date

    DATEDIFF() Returns the number of days between two datesDATE_FORMAT() Displays date/time data in different formats

    SQL Server Date Functions

    The following table lists the most important built-in date functions in SQL Server:

    Function Description

    GETDATE() Returns the current date and time

    DATEPART() Returns a single part of a date/time

    DATEADD() Adds or subtracts a specified time interval from a date

    DATEDIFF() Returns the time between two dates

    CONVERT() Displays date/time data in different formats

    SQL Date Data Types

    MySQL comes with the following data types for storing a date or a date/time value in the database:

    DATE - format YYYY-MM-DD

    DATETIME - format: YYYY-MM-DD HH:MM:SS

    TIMESTAMP - format: YYYY-MM-DD HH:MM:SS

    YEAR - format YYYY or YY

    SQL Server comes with the following data types for storing a date or a date/time value in thdatabase:

  • 8/3/2019 Dbms Commands

    6/6

    DATE - format YYYY-MM-DD

    DATETIME - format: YYYY-MM-DD HH:MM:SS

    SMALLDATETIME - format: YYYY-MM-DD HH:MM:SS

    TIMESTAMP - format: a unique number

    Now we want to select the records with an OrderDate of "2008-11-11" from the table above.

    We use the following SELECT statement:

    SELECT * FROM Orders WHERE OrderDate='2008-11-11'

    SELECT * FROM Orders WHERE OrderDate='2008-11-11'