creating database using sql commands

44
CREATING DATABASE USING SQL COMMANDS

Upload: belle-wx

Post on 20-Jan-2015

916 views

Category:

Documents


3 download

DESCRIPTION

 

TRANSCRIPT

  • 1. CREATING DATABASE USING SQL COMMANDS

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].);
9. ALTER TABLE statement

  • Example

10. Table before add new column 11. Execute ALTER TABLE statement to ADD: 12. Table after execute 13. the ALTER TABLE 14. statement to ADDALTER TABLE emp
ADD (job VARCHAR(9));
15. ALTER TABLE statement

  • Example

16. Table before MODIFY a column 17. Execute ALTER TABLE statement to MODIFY: 18. Table after execute 19. the ALTER TABLE 20. statement to 21. MODIFYALTER TABLE emp
MODIFY (ename VARCHAR(3));
22. ALTER TABLE statement

  • Example

23. Table before DROP column 24. Execute ALTER TABLE statement to DROP: 25. Table after execute 26. the ALTER TABLE 27. statement to DROP:ALTER TABLE emp
DROP COLUMN job;
28. Basic Structures in Query Design
QUERY WITH SQL
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. SELECT STATEMENT

  • Purpose to retrieve and display data from one or more database tables.

31. It is an extremely powerful command capable of performing the equivalentof the relational algebras Selection, Projection and Join operations in a single statement. 32. It is most frequently used SQL command. 33. Basic select statement: 34. SELECT identifies what columns 35. FROM identifies which tableSELECT [DISTINCT] {*, column,}
FROM table;
36. SELECT STATEMENT
Select all columns
Select specific columns
Output
SELECT * FROM dept;
Output
SELECT Deptno, loc FROM dept;
37. SELECT STATEMENT

  • Using WHERE clause.

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.
54. SELECT * FROM V_REGION_SALES
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.
56. TABLE CONSTRAINTS IN SQL

  • NOT NULL

57. UNIQUE 58. PRIMARY KEY 59. FOREIGN KEY