Database SQL Commands

← Go back to Database Chapter



Syntax to create a table in MySql is as follows:
CREATE TABLE tablename (column name datatype (size)… );

Example:
CREATE TABLE authors (aid INT, aname CHAR(20), dod DATE, quali CHAR(20), email CHAR(20), phonenumber VARCHAR(12));

Syntax to insert value: Insert Into tablename (Col1, Col2……) values (Val1,Val2……);

Example:
In Mysql:
Insert into authors values (101, 'Kiran', '2001-10-10 ', 'M.Sc', 'kiran@fle.com', 123456);

In Oracle:
Insert into authors values (102, 'vas', '10-oct-2001', 'M.Sc', 'vas@fle.com', 123456);

Authors Table With Data

Aid Aname DOB Quail Email Phone
101 Kiran 2001-10-10 M.Sc kiran@fle.com 123456

  • Rectify It : IRectify It :nsert into authors values (103, 'sd', 'M.Sc' 12345);

  • Rectified : Insert Into authors (aid, aname, quali, phonenumber) VALUES (103, 'sd', 'M.Sc', 12345);


Notes:

  1. Char & date type values must be enclosed in single quotation marks.

  2. Whenever you are inserting date type values, you should make sure that the correct date format is used.

  3. Whenever you are not providing values for all the columns, you must specify the first column name and add that table name.

  4. Whenever you are not supplying values for a column, a null will be placed in that column.


Question: What will happen when you give the following command:
INSERT INTO authors (phonenumber, aname, aid) VALUES (1234, 'sd',104);

Answer: It will execute successfully.

Syntax to insert value: Update table-name set col1 = Val1, Col2 = val2 Where condition;

Example:

Single Update:
UPDATE authors SET email = 'kiran@fle.com', phonenumber = 999 WHERE aid = 101;

Multiple Update:
UPDATE authors SET phonenumber = 999;

Note:

  1. Using one update state, you can update zero or more records.

  2. ROLLBACK - insert, delete and update not for create and drop

Syntax to insert value: DELETE FROM tablename WHERE CONDITIONS;

Example:

Single Update:
DELETE FROM authors WHERE aid = 103;

Multiple Update:
DELETE FROM authors; (DELETES ALL DATA IN AUTHOR’S TABLE)

Note:

    Using one delete statement, you can delete zero or more records.

Syntax: DROP TABLE tablename;

Example: DROP TABLE authors;

Syntax: TRUNCATE TABLE tablename;

Example: TRUNCATE TABLE authors; DELETE FROM authors;

  1. Both the statements will give you an empty tab at the end.

  2. The Delete statement deletes all records one by one, whereas the Truncate state does the table simply and creates a new table.

  3. Delete takes more time than the Truncate state.

  4. Delete operation can be cancelled i.e. it can be called back, whereas Truncate operations cannot be callback.

  5. DELETE deletes the entire table with its structure whereas TRUNCATE deletes only the data that is inside that particular table.

Syntax :
Select (or) cal1, col2 _ _ _ _ _ _ _ _ _ tab1, tab2
Where condition Order by col-name Group by col-name Having condition;

Books Table

BookID Cost Isbn Pub Qty Edition Tob
                 

Question 1: Display complete information of all the books.
Answer: Select * from books;

Question 2: Display the bookID, bookname, cost and the edition of all the books.
Answer: Select bookID, bookname, cost, edition, from books;

Question 3: Display the books published by DLC.
Answer: Select * from books where pub= 'ONLYFORJAVA';

Question 4: Display the books written by Kiran.
Answer: Select * from books where author = 'kiran';

Question 5: Display the books written by Kiran in 2010.
Answer: Select * from books where author = 'kiran' and YOP = 2010;

Question 6: Display the book whose name you learnt in the beginning.
Answer: Select * from books where book name like 'learn %';

Question 7: Display the books which cost less than INR 200 and are written by an author whose name ends with Vas.
Answer: Select cost, author from book where cost <200 and author like '%Vas';

Question 8: Display the books published between 2005 and 2010.
Answer: Select * from books where YOP>=2005 and YOP<=2010;
or
Select * from books where YOP between 2005 and 2010;

Question 9: Display the books published by "TATA", "ONLYFORJAVA" and "P5".
Answer: Select * from books where pub='TATA' or pub='ONLYFORJAVA' or pube='P5';
or
Select * from books where pub in ('TATA', 'ONLYFORJAVA', 'P5');

Question 10: Display the books which cost from INR50 to 300 in ascending order of cost.
Answer: Select * from books where cost between 50 and 300 order by cost asc;

Question 11: Display the books published in 2009 in descending order of their costs.
Answer: Select * from books where YOP=200g order by cost describtion;

To Display the books which are not published by ONLYFORJAVA
Select * from books where PUB< >'ONLYFORJAVA'
or
Where PUB not in 'ONLYFORJAVA';

To Display the books written by the author whose name second character is S.
Select * from books where author like '-s%';