2. SHOW DATABASE mysql> SHOW DATABASES; Use the SHOW statement to find out what databases currently exist on the server SQL command
3. CREATE DATABASE mysql> CREATE DATABASE newdb; Database name Create statement The new database will appear after successfully create the database
4. USE DATABASE mysql> USE newdb Creating a database does not select it for use; you must do that explicitly. Note that USE, like QUIT, does not require a semicolon. The USE statement is special in another way, too: it must be given on a single line. SQL command Database name
5. SHOW TABLES mysql> SHOW TABLES; To create one or more tables in the current database, you can use CREATE TABLE statement. SQL command It indicates that there is no table in the database.
6. CREATE TABLE mysql> CREATE TABLE Use a CREATE TABLE statement to specify the layout of your table. In this case, table pelajarwill have 4 attributes.
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 DESCRIBE SQL command Database name
8. ALTER TABLE statement ALTER TABLE is use to modify an existing column It is consists of ADD, MODIFY and DROP column ALTER TABLE statement: ADD column MODIFY column DROP column ALTER TABLE table ADD (column datatype [DEFAULT expr][column datatype]….); ALTER TABLE table MODIFY (column datatype [DEFAULT expr][column datatype]….); ALTER TABLE table DROP (column datatype [DEFAULT expr][column datatype]….);
29. DEFINE THE INSTRUCTION SELECT to query data in the database INSERT to insert data into a table UPDATE to update data in a table DELETE to delete data from a table
30.
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.
36. SELECT STATEMENT Select all columns Select specific columns Output SELECT * FROM dept; Output SELECT Deptno, loc FROM dept;
37.
38. It is used to restrict or limiting the rows selected
39. The WHERE clause follows the FROM clauseSELECT [DISTINCT] {*, column,…} FROM table WHERE condition(s);
40. SELECT STATEMENT Using where clause Select specific columns Output SELECT ename, job, deptno FROM dept WHERE job = ‘CLERK’ Output SELECT Deptno, loc FROM dept WHERE loc = ‘New York’;
41. SELECT STATEMENT Using BETWEEN operator to display rows based on a range of values Output SELECT ename, sal FROM emp WHERE sal BETWEEN 10000 AND 15000;
42. UPDATE UPDATE item SET Quantity =10,UnitPrice =1800.00 WHERE ItemNo = ‘123’; 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.
43. DELETE To remove one or more rows from a table. For example , to delete the ItemNo = ‘123’. DELETE FROM item WHERE ItemNo = ‘123’;
44. The syntax for inserting data into a table one row at a time is as follows: INSERT INTO "table_name" ("column1", "column2", ...)VALUES ("value1", "value2", ...) INSERT INTO
45. Assuming that we have a table that has the following structure : Table Store_Information Result : INSERT INTO Store_Information (store_name, Sales, Date)VALUES (‘Ipoh', 900, 'Jan-10-1999')
46. The second type of INSERT INTO allows us to insert multiple rows into a table. Unlike the previous example, we now use a SELECT statement to specify the data that we want to insert into the table. The syntax is as follows: INSERT INTO "table1" ("column1", "column2", ...)SELECT "column3", "column4", ...FROM "table2"
47. Before using INSERT INTO command Table Sales_Information Table Store_Information Table Sales_Information Table Store_Information
48. After using INSERT INTO command INSERT INTO Store_Information (store_name, Sales, Date)SELECT store_name, Sales, DateFROM Sales_InformationWHERE Year(Date) = 1998 New information that been added to the table Table Sales_Information Table Store_Information
49. CREATE VIEW Views can be considered as virtual tables. Generally speaking, a table has a set of definition, and it physically stores the data. 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. The syntax for creating a view is as follows: CREATE VIEW "VIEW_NAME" AS "SQL Statement"
50. We have the following table TABLE Customer 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, CREATE VIEW V_CustomerAS SELECT First_Name, Last_Name, CountryFROM Customer
51. Now we have a view called V_Customer with the following structure: View V_Customer(First_Name char(50),Last_Name char(50),Country char(25))
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 Table Store_Information Table Geography
53. We want to build a view that has sales by region information 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 This gives us a view, V_REGION_SALES, that has been defined to store sales by region records.
55. Exercises:According to the table given, write SQL query for each of the following questions. Add a column called “TelNum” to this table. Change the column name for “Reg_Num” to “RegistrationNum”. Modify the data type of “Year_Born” to date. Delete the column “TelNum”.
60. 1) NOT NULL By default, a column can hold NULL. 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.
61. Columns "SID" and "Last_Name" cannot include NULL, while "First_Name" can include NULL. Result :
62. 2) UNIQUE The UNIQUE constraint ensures that all values in a column are distinct. “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".
64. 3) PRIMARY KEY A primary key is used to uniquely identify each row in a table. It can either be part of the actual record itself , or it can be an artificial field. A primary key can consist of one or more fields on a table. When multiple fields are used as a primary key, they are called a composite key. Primary keys can be specified either when the table is created (using CREATE TABLE) or by changing the existing table structure (using ALTER TABLE).
65. Example : CREATE TABLE Customer (SID integer, Last_Name varchar(30), First_Name varchar(30), PRIMARY KEY (SID)); ALTER TABLE Customer ADD PRIMARY KEY (SID); Indicate that “SID” column has been choose to be the primary key.
67. 4) FOREIGN KEY A foreign key is a field (or fields) that points to the primary key of another table. The purpose of the foreign key is to ensure referential integrity of the data. In other words, only values that are supposed to appear in the database are permitted.
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)); ALTER TABLE ORDERS ADD FOREIGN KEY (customer_sid) REFERENCES CUSTOMER(SID); Indicates the Customer_SID column in the ORDERS table is a foreign key pointing to the SID column in the CUSTOMER table