Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Creating database using sql commands


Published on

Creating database using sql commands

  2. 2. SHOW DATABASE<br />mysql> SHOW DATABASES;<br />Use the SHOW statement to find out what databases currently exist on the server<br />SQL command<br />
  3. 3. CREATE DATABASE<br />mysql> CREATE DATABASE newdb;<br />Database name<br />Create statement<br />The new database will appear after successfully create the database<br />
  4. 4. USE DATABASE<br />mysql> USE newdb<br />Creating a database does not select it for use; you must do that explicitly.<br />Note that USE, like QUIT, does not require a semicolon. <br />The USE statement is special in another way, too: it must be given on a single line. <br />SQL command<br />Database name<br />
  5. 5. SHOW TABLES<br />mysql> SHOW TABLES;<br />To create one or more tables in the current database, you can use CREATE TABLE statement.<br />SQL command<br />It indicates that there is no table in the database.<br />
  6. 6. CREATE TABLE<br />mysql> CREATE TABLE <br />Use a CREATE TABLE statement to specify the layout of your table.<br />In this case, table pelajarwill have 4 attributes.<br />
  7. 7. If you want to find out about the structure of a table, the DESCRIBE command is useful; it displays information about each of a table's columns<br />DESCRIBE<br />SQL command<br />Database name<br />
  8. 8. ALTER TABLE statement<br />ALTER TABLE is use to modify an existing column<br />It is consists of ADD, MODIFY and DROP column<br />ALTER TABLE statement:<br />ADD column<br /> MODIFY column<br /> DROP column<br />ALTER TABLE table<br />ADD (column datatype [DEFAULT expr][column datatype]….);<br />ALTER TABLE table<br />MODIFY (column datatype [DEFAULT expr][column datatype]….);<br />ALTER TABLE table<br />DROP (column datatype [DEFAULT expr][column datatype]….);<br />
  9. 9. ALTER TABLE statement<br /><ul><li>Example
  10. 10. Table before add new column
  11. 11. Execute ALTER TABLE statement to ADD:
  12. 12. Table after execute
  13. 13. the ALTER TABLE
  14. 14. statement to ADD</li></ul>ALTER TABLE emp <br />ADD (job VARCHAR(9));<br />
  15. 15. ALTER TABLE statement<br /><ul><li>Example
  16. 16. Table before MODIFY a column
  17. 17. Execute ALTER TABLE statement to MODIFY:
  18. 18. Table after execute
  19. 19. the ALTER TABLE
  20. 20. statement to
  21. 21. MODIFY</li></ul>ALTER TABLE emp <br />MODIFY (ename VARCHAR(3));<br />
  22. 22. ALTER TABLE statement<br /><ul><li>Example
  23. 23. Table before DROP column
  24. 24. Execute ALTER TABLE statement to DROP:
  25. 25. Table after execute
  26. 26. the ALTER TABLE
  27. 27. statement to DROP:</li></ul>ALTER TABLE emp <br />DROP COLUMN job;<br />
  28. 28. Basic Structures in Query Design<br />QUERY WITH SQL<br />
  29. 29. DEFINE THE INSTRUCTION<br />SELECT  to query data in the database<br />INSERT  to insert data into a table<br />UPDATE  to update data in a table<br />DELETE  to delete data from a table<br />
  30. 30. SELECT STATEMENT<br /><ul><li>Purpose  to retrieve and display data from one or more database tables.
  31. 31. It is an extremely powerful command capable of performing the equivalentof the relational algebra’s Selection, Projection and Join operations in a single statement.
  32. 32. It is most frequently used SQL command.
  33. 33. Basic select statement:
  34. 34. SELECT identifies what columns
  35. 35. FROM identifies which table</li></ul>SELECT [DISTINCT] {*, column,…}<br />FROM table;<br />
  36. 36. SELECT STATEMENT<br />Select all columns<br />Select specific columns<br />Output<br />SELECT * FROM dept;<br />Output<br />SELECT Deptno, loc FROM dept;<br />
  37. 37. SELECT STATEMENT<br /><ul><li> Using WHERE clause.
  38. 38. It is used to restrict or limiting the rows selected
  39. 39. The WHERE clause follows the FROM clause</li></ul>SELECT [DISTINCT] {*, column,…}<br />FROM table<br />WHERE condition(s);<br />
  40. 40. SELECT STATEMENT<br />Using where clause<br />Select specific columns<br />Output<br />SELECT ename, job, deptno<br /> FROM dept<br />WHERE job = ‘CLERK’<br />Output<br />SELECT Deptno, loc <br />FROM dept<br />WHERE loc = ‘New York’;<br />
  41. 41. SELECT STATEMENT<br />Using BETWEEN operator to display rows based on a range of values <br />Output<br />SELECT ename, sal<br /> FROM emp<br />WHERE sal BETWEEN 10000 AND 15000;<br />
  42. 42. UPDATE<br />UPDATE item<br />SET Quantity =10,UnitPrice =1800.00<br />WHERE ItemNo = ‘123’;<br />In this statement , only one row will be updated since the condition is specified in the WHERE clause, if the WHERE clause is omitted, all rows in the item table will be update.<br />
  43. 43. DELETE<br />To remove one or more rows from a table. For example , to delete the ItemNo = ‘123’.<br />DELETE<br />FROM item<br />WHERE ItemNo = ‘123’;<br />
  44. 44. The syntax for inserting data into a table one row at a time is as follows:<br /> INSERT INTO "table_name" ("column1", "column2", ...)VALUES ("value1", "value2", ...)<br />INSERT INTO<br />
  45. 45. Assuming that we have a table that has the following structure :<br />Table Store_Information<br />Result :<br />INSERT INTO Store_Information (store_name, Sales, Date)VALUES (‘Ipoh', 900, 'Jan-10-1999')<br />
  46. 46. The second type of INSERT INTO allows us to insert multiple rows into a table. <br />Unlike the previous example, we now use a SELECT statement to specify the data that we want to insert into the table. <br />The syntax is as follows:<br /> INSERT INTO "table1" ("column1", "column2", ...)SELECT "column3", "column4", ...FROM "table2"<br />
  47. 47. Before using INSERT INTO command<br />Table Sales_Information<br />Table Store_Information<br />Table Sales_Information<br />Table Store_Information<br />
  48. 48. After using INSERT INTO command<br />INSERT INTO Store_Information (store_name, Sales, Date)SELECT store_name, Sales, DateFROM Sales_InformationWHERE Year(Date) = 1998<br />New information that been added to the table<br />Table Sales_Information<br />Table Store_Information<br />
  49. 49. CREATE VIEW<br />Views can be considered as virtual tables. <br />Generally speaking, a table has a set of definition, and it physically stores the data. <br />A view also has a set of definitions, which is build on top of table(s) or other view(s), and it does not physically store the data. <br />The syntax for creating a view is as follows:<br /> CREATE VIEW "VIEW_NAME" AS "SQL Statement"<br />
  50. 50. We have the following table<br />TABLE Customer<br />We want to create a view called V_Customer that contains only the First_Name, Last_Name, and Country columns from this table, we would type in,<br />CREATE VIEW V_CustomerAS SELECT First_Name, Last_Name, CountryFROM Customer<br />
  51. 51. Now we have a view called V_Customer with the following structure: <br />View V_Customer(First_Name char(50),Last_Name char(50),Country char(25))<br />
  52. 52. We can also use a view to apply joins to two tables. In this case, users only see one view rather than two tables, and the SQL statement users need to issue becomes much simpler. Let's say we have the following two tables<br />Table Store_Information<br />Table Geography<br />
  53. 53. We want to build a view that has sales by region information<br />CREATE VIEW V_REGION_SALESAS SELECT A1.region_name REGION, SUM(A2.Sales) SALESFROM Geography A1, Store_Information A2WHERE A1.store_name = A2.store_nameGROUP BY A1.region_name<br />This gives us a view, V_REGION_SALES, that has been defined to store sales by region records. <br />
  54. 54. SELECT * FROM V_REGION_SALES<br />
  55. 55. Exercises:According to the table given, write SQL query for each of the following questions.<br />Add a column called “TelNum” to this table.<br />Change the column name for “Reg_Num” to “RegistrationNum”.<br />Modify the data type of “Year_Born” to date.<br />Delete the column “TelNum”.<br />
  56. 56. TABLE CONSTRAINTS IN SQL<br /><ul><li>NOT NULL
  57. 57. UNIQUE
  58. 58. PRIMARY KEY
  59. 59. FOREIGN KEY</li></li></ul><li>TABLE CONSTRAINT<br />You can place constraints to limit the type of data that can go into a table. <br />Such constraints can be specified when the table when the table is first created via the CREATE TABLE statement, or after the table is already created via the ALTER TABLE statement.<br />
  60. 60. 1) NOT NULL<br />By default, a column can hold NULL. <br />If you not want to allow NULL value in a column, you will want to place a constraint on this column specifying that NULL is now not an allowable value.<br />
  61. 61. Columns "SID" and "Last_Name" cannot include NULL, while "First_Name" can include NULL.<br />Result :<br />
  62. 62. 2) UNIQUE<br />The UNIQUE constraint ensures that all values in a column are distinct.<br />“SID” column in customer table is modify so that "SID" column cannot include duplicate values, while such constraint does not hold for columns "Last_Name" and "First_Name".<br />
  63. 63. Result :<br />Indicates that “SID” column is unique<br />
  64. 64. 3) PRIMARY KEY<br />A primary key is used to uniquely identify each row in a table. <br />It can either be part of the actual record itself , or it can be an artificial field. <br />A primary key can consist of one or more fields on a table. <br />When multiple fields are used as a primary key, they are called a composite key. <br />Primary keys can be specified either when the table is created (using CREATE TABLE) or by changing the existing table structure (using ALTER TABLE). <br />
  65. 65. Example :<br />CREATE TABLE Customer (SID integer, Last_Name varchar(30), First_Name varchar(30), PRIMARY KEY (SID)); <br />ALTER TABLE Customer ADD PRIMARY KEY (SID); <br />Indicate that “SID” column has been choose to be the primary key.<br />
  66. 66. Result :<br />Primary key<br />
  67. 67. 4) FOREIGN KEY<br />A foreign key is a field (or fields) that points to the primary key of another table. <br />The purpose of the foreign key is to ensure referential integrity of the data. <br />In other words, only values that are supposed to appear in the database are permitted.<br />
  68. 68. Table customer<br />Table order<br />Example :<br />
  69. 69. CREATE TABLE ORDERS (Order_ID integer, Order_Date date, Customer_SID integer, Amount double, Primary Key (Order_ID), Foreign Key (Customer_SID) references CUSTOMER(SID)); <br /> <br />ALTER TABLE ORDERS ADD FOREIGN KEY (customer_sid) REFERENCES CUSTOMER(SID);<br />Indicates the Customer_SID column in the ORDERS table is a foreign key pointing to the SID column in the CUSTOMER table<br />