Вы находитесь на странице: 1из 14

WORKING WITH DATABASE OBJECTS CREATING TABLES Using the Object Browser

Using SQL

CREATING A COPY OF A TABLE Using the Object Browser

Using SQL

Command:

MODIFYING TABLES You can modify tables using the SQL ALTER TABLEstatement. You may need to change the table structure due to any of the following reasons: You omitted a column. Your column definition needs to be changed. You need to remove columns. The ALTER TABLE statement is used to: Add a new column Modify an existing column Define a default value for the new column Drop a column Manage constraints Using the Object Browser

Using SQL

MANAGING CONSTRAINTS Data integrity ensures the consistency and correctness of data stored in a database. Such integrity can be enforced by incorporating business rules. Constraints are the rules that are enforced on data stored in a table. You can use constraints to do the following: Enforce rules on the data in a table whenever a row is updated, inserted, or deleted from that table Prevent the deletion of a table if there are dependencies from other tables Types of Constraints: PRIMARY KEY FOREIGN KEY CHECK UNIQUE NOT NULL Constraints can be enforced at two levels: Column level Table level A constraint can be created with either of the following statements: CREATE TABLE ALTER TABLE With the ALTER TABLE statement, you can disable or enable the imposed constraint without dropping it or re-creating it: Disable a constraint by using the DISABLE clause. Enable a constraint by using the ENABLE clause. Using the Object Browser

Using SQL

REMOVING TABLES You can discard a table if you no longer find it useful. The DROP TABLE statement removes the definition of the table from the database. Deleting all of the records in a table is different from DROP TABLE. After deleting all the records, the column and constraint information still remains. But DROP TABLE results in the removal of the table definition along with the rows.

In Oracle Database XE, you can remove a table in either of the following ways: Select Object Browser > Browse > Tables. Click the table name, and then click DROP. Select SQL > SQL commands > Enter command. Type the SQL statement, and then click Run. Using the Object Browser

Using SQL

ACCESSING DATA The SQL SELECT statement is used to access and report data back from the XE tables. This is known as "querying" the data. In XE, you can either write SELECT statements using the SQL Workshop tool, or you can use the Query Builder tool to build queries with a GUI interface. All operations on information in an Oracle database are performed by using SQL statements. A SQL statement is a string of SQL text. The SQL SELECT statement is the command that retrieves data from the XE tables With the SELECT statement, you can choose to see all or some of the data in a table. SELECT statements have three capabilities: Selection: Identifying rows Projection: Identifying columns Join: Retrieving data from multiple tables

BUILDING QUERIES Using the Query Builder tool

Using the SQL Workshop tool

WRITING A QUERY Writing SELECT Statements Two mandatory clauses in the SELECT statement syntax are required: a SELECT clause and a FROM clause. The SELECT statement syntax is displayed. The SELECT clause specifies the columns that you want to display. The FROM clause specifies the tables that contain those columns. Syntax

Using the Query Builder tool

Using the SQL Workshop tool

SORTING DATA Using the Query Builder tool

Using the SQL Workshop tool

RETREIVING COLUMNS You can use the projection capability of SQL to choose the columns in a table that you want to retrieve. You can retrieve selected columns or all columns from a table. Using the Query Builder tool

Using the SQL Workshop tool

RETREIVING ROWS You can use the selection capability of SQL to choose the rows that you want to retrieve from a table. You can specify various criteria to select the rows that you want to see. You can restrict the number of rows that are retrieved from the database by using a WHERE clause in a SQL statement. By inserting a WHERE clause into a SQL statement, you can specify a condition that must be met, and only the rows that meet the condition are returned. When using a WHERE clause: The WHERE clause directly follows the FROM clause in the SQL statement syntax The WHERE clause consists of the WHERE keyword and a condition or conditions. The condition in a WHERE clause specifies a comparison of values that limits the rows that are returned by a query Using the Query Builder tool

Using the SQL Workshop tool

JOINING TABLES Sometimes you need to display data from more than one table. To do this, you use SELECT statements. The FROM clause of your query contains the names of the tables from which you are retrieving data. Because information comes from more than one table, this is called a JOIN between the tables involved. For example, in the EMPLOYEES table, the DEPARTMENT_ID column represents the department number for an employee. In the DEPARTMENTS table, there is a DEPARTMENT_ID column as well as a DEPARTMENT_NAME column. You can join the EMPLOYEES and DEPARTMENTS tables by using the DEPARTMENT_ID column to produce a report that shows the employees' names and department names. Natural Joins A natural join enables you to display data from two tables when a value in one column of one table corresponds directly to a value in another column in the second table. In a natural join, the two tables include one or more columns that have the same name and data types. A natural join retrieves all rows from the two tables that have equal values in all matched columns. Frequently, this type of join involves primary key and foreign key columns.

Joining Two Tables with a USING Clause The USING clause enables you to specify the columns to be used for a join between two tables. The column names must be the same for both tables and must have compatible data types. Use the USING clause if your tables contain more than one column whose names match to explicitly identify the name of the columns that you want to join.

Using the Query Builder tool

Using the SQL Workshop tool

ON Clause You use the ON clause to specify the join condition when joining two tables or joining a table to itself. This enables you to separate the join condition from any search or filter conditions in the WHERE clause. The column names need not match between the tables; however, the data types must match.

Ex. Evaluating the hire dates and start dates of all employees. Hire dates are in the EMPLOYEES table and the start dates are in the JOB_HISTORY table. These two columns are named differently. TheON clause is as follows:

Using the SQL Workshop tool

Additional Conditions You can apply additional conditions to the join. For example, you may want to join the EMPLOYEES and DEPARTMENTS tables and, in addition, display only employees who have a manager ID of 149. To add additional conditions to theON clause, you can add AND clauses. Alternatively, you can use a WHERE clause to apply additional conditions.

Using the Query Builder tool

Using the SQL Workshop tool

Aliases In the examples shown, aliases are used to identify the table names. In theFROM clause, an abbreviation is provided after the table name. This is called an alias. After the alias is set up in the FROM clause, you can refer to it throughout the statement. Joining Multiple Tables A three-way join is a join of three tables. You can join as many tables as needed to retrieve information. For example, you might want to find employees, their dependents names, and the department names for those employees. This requires accessing three tables: EMPLOYEES, DEPENDENTS, and DEPARTMENTS.

Self-Joining Tables The ON clause can also be used to join columns that have different names (in the same table or in a different table). For example, you can perform a self-join of the EMPLOYEES table based on the EMPLOYEE_ID and MANAGER_ID columns.

Removing Tables You can discard a table if you no longer find it useful. The DROP TABLE statement removes the definition of the table from the database. Deleting all of the records in a table is different from DROP TABLE. After deleting all the records, the column and constraint information still remains. But DROP TABLE results in the removal of the table definition along with the rows.

In Oracle Database XE, you can remove a table in either of the following ways: Select Object Browser > Browse > Tables. Click the table name, and then click DROP. Select SQL > SQL commands > Enter command. Type the SQL statement, and then click Run.

USING FUNCTIONS TO CUSTOMISE REPORTS A SQL function is a program that performs an operation on data. SQL functions provide a powerful way to perform operations when retrieving data from a table. For example, you may need to display employee names in uppercase in a report. Or you may need to display employees' hire dates with the name of the month spelled out. Many functions work on specific data types. Each value manipulated by Oracle XE has a data type. Data types provide a way to define the behavior of data. When you create a table, you must specify a data type for each of its columns The three main types of data that functions work on are: Character Number Date Characteristics Single-row character functions accept character data as input and return either character or numeric values. Character functions that return character values return values of the same data type as their input argument. Character functions that return number values can take any character data type as their input argument.

Вам также может понравиться