understanding queries and rdbms

17
By Arnel P. Colanggo

Upload: arnel-colanggo

Post on 04-Oct-2015

235 views

Category:

Documents


2 download

TRANSCRIPT

  • By Arnel P. Colanggo

  • Putting data into a database in only a half battle, the other half involves using it effectively.Database is the systematically arrange collection of data, structured so that it can automatically retrieve or manipulatedRelational Database Management System(RDBMS)-is a computer program devised to create, store and manipulate database.

    Structured Query Language (SQL) A database query and a programming language widely used for accessing, querying, updating and managing data in relational database management system.

  • is a basic concept with an RDBMS, and one that becomes important when designing a database with more than one table. When foreign keys are used to link one table to another, referential integrity by its nature, imposes constraints on inserting new records and updating existing records. For example, if a table only accepts certain types of values for a particular field, and other tables use that fields as their foreign key, these automatically imposes constraints on the dependent table.

  • An important part of designing a database is a process known as normalization. Normalization refers to the activity of streamlining a database design by eliminating redundancies and repeated values. Most often, redundancies are eliminated by placing repeating groups of values into separate tables and linking them through foreign keys. This not only makes the database more compact and reduces the disk space it occupies, but it also simplifies the task of making changes.

  • SQL INSERT STATEMENTINSERT INTO target [(field1[, field2[, ...]])]VALUES (value1[, value2[, ...])The INSERT INTO statement has these parts:

    targetThe name of the table or query to append records to.field1, field2Names of the fields to append data to, if following a target argument, or the names of fields to obtain data from, if following a source argument.value1, value2The values to insert into the specific fields of the new record. Each value is inserted into the field that corresponds to the value's position in the list: value1 is inserted into field1 of the new record, value2 into field2, and so on. You must separate values with a comma, and enclose text fields in quotation marks (' ').

  • SELECT STATEMENTInstructs the Microsoft Access database engine to return information from the database as a set of records.SyntaxSELECT [predicate] { * | table.* | [table.]field1 [AS alias1] [, [table.]field2 [AS alias2] [, ...]]}FROM tableexpression [, ...] [IN externaldatabase][WHERE... ][GROUP BY... ][HAVING... ][ORDER BY... ][WITH OWNERACCESS OPTION]The SELECT statement has these parts:predicateOne of the following predicates: ALL, DISTINCT, DISTINCTROW, or TOP. You use the predicate to restrict the number of records returned. If none is specified, the default is ALL.*Specifies that all fields from the specified table or tables are selected.

  • tableThe name of the table containing the fields from which records are selected.field1, field2The names of the fields containing the data you want to retrieve. If you include more than one field, they are retrieved in the order listed.alias1, alias2The names to use as column headers instead of the original column names in table.tableexpressionThe name of the table or tables containing the data you want to retrieve.externaldatabaseThe name of the database containing the tables in tableexpression if they are not in the current database.

  • UPDATE STATEMENTCreates an update query(update query: An action query (SQL statement) that changes a set of records according to criteria (search conditions) that you specify.) that changes values in fields in a specified table based on specified criteria.SyntaxUPDATE tableSET newvalueWHERE criteria;The UPDATE statement has these parts:tableThe name of the table containing the data you want to modify.newvalueAn expression(expression: Any combination of mathematical or logical operators, constants, functions, and names of fields, controls, and properties that evaluates to a single value. Expressions can perform calculations, manipulate characters, or test data.) that determines the value to be inserted into a particular field in the updated records.criteriaAn expression that determines which records will be updated. Only records that satisfy the expression are updated.

  • RemarksUPDATE is especially useful when you want to change many records or when the records that you want to change are in multiple tables.You can change several fields at the same time. The following example increases the Order Amount values by 10 percent and the Freight values by 3 percent for shippers in the United Kingdom:UPDATE OrdersSET OrderAmount = OrderAmount * 1.1,Freight = Freight * 1.03WHERE ShipCountryRegion = 'UK';

  • DELETE STATEMENTCreates a delete query(delete query: A query (SQL statement) that removes rows matching the criteria that you specify from one or more tables.) that removes records from one or more of the tables listed in the FROM clause that satisfy the WHERE clause.SyntaxDELETE [table.*]FROM tableWHERE criteriaThe DELETE statement has these parts:table.*The optional name of the table from which records are deleted.tableThe name of the table from which records are deleted.criteriaAn expression(expression: Any combination of mathematical or logical operators, constants, functions, and names of fields, controls, and properties that evaluates to a single value. Expressions can perform calculations, manipulate characters, or test data.) that determines which records to delete.

  • RemarksDELETE is especially useful when you want to delete many records.To drop an entire table from the database, you can use the Execute method with a DROP statement. If you delete the table, however, the structure is lost. In contrast, when you use DELETE, only the data is deleted; the table structure and all of the table properties, such as field attributes and indexes, remain intact.You can use DELETE to remove records from tables that are in a one-to-many relationship(one-to-many relationship: An association between two tables in which the primary key value of each record in the primary table corresponds to the value in the matching field or fields of many records in the related table.) with other tables. Cascading delete(cascading delete: For relationships that enforce referential integrity between tables, the deletion of all related records in the related table or tables when a record in the primary table is deleted.) operations cause the records in tables that are on the many side of the relationship to be deleted when the corresponding record in the one side of the relationship is deleted in the query. For example, in the relationship between the Customers and Orders tables, the Customers table is on the one side and the Orders table is on the many side of the relationship. Deleting a record from Customers results in the corresponding Orders records being deleted if the cascade delete option is specified.

  • A delete query deletes entire records, not just data in specific fields. If you want to delete values in a specific field, create an update query(update query: An action query (SQL statement) that changes a set of records according to criteria (search conditions) that you specify.) that changes the values to Null(Null: A value you can enter in a field or use in expressions or queries to indicate missing or unknown data. In Visual Basic, the Null keyword indicates a Null value. Some fields, such as primary key fields, can't contain Null.).

  • A criterion is similar to a formula it is a string that may consist of field references, operators(operator: A sign or symbol that specifies the type of calculation to perform within an expression. There are mathematical, comparison, logical, and reference operators.), and constants(constant: A value that is not calculated and, therefore, does not change. For example, the number 210, and the text "Quarterly Earnings" are constants. An expression, or a value resulting from an expression, is not a constant.). Query criteria are also referred to as expressions in Microsoft Office Access 2007.The following tables shows some sample criteria and explains how they work. CriteriaDescription>25 and
  • DateDiff ("yyyy", [BirthDate], Date()) > 30This criterion applies to a Date/Time field, such as BirthDate. Only records where the number of years between a person's birthdate and today's date is greater than 30 are included in the query result. Is NullThis criterion can be applied to any type of field to show records where the field value is null.As you can see, criteria can look very different from each other, depending on the data type of the field to which they apply and your specific requirements. Some criteria are simple, and use basic operators and constants. Others are complex, and use functions, special operators, and include field references.This topic lists several commonly used criteria by data type. If the examples given in this topic do not address your specific needs, you might need to write your own criteria. To do that, you must first familiarize yourself with the full list of functions, operators, special characters, and the syntax for expressions referring to fields and literals. For more information, see the articles listed in the See also section.

  • Here, you will see where and how you add the criteria. To add a criteria to a query, you must open the query in Design view. You then identify the fields for which you want to specify criteria. If the field is not already in the design grid, you add it by either dragging it from the query design window to the field grid, or by double-clicking the field (Double-clicking the field automatically adds it to the next empty column in the field grid.). Finally, you type the criteria in the Criteria rowCriteria that you specify for different fields in the Criteria row are combined by using the AND operator. In other words, the criteria specified in the City and BirthDate fields are interpreted like this:City = "Chicago" AND BirthDate < DateAdd("yyyy", -40, Date())

  • The City and BirthDate fields include criteria.

    2. Only records where the value of the City field is Chicago will satisfy this criterion.

    3. Only records of those who are at least 40 years old will satisfy this criterion.

    4. Only records that meet both criteria will be included in the result.