* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Database
Open Database Connectivity wikipedia , lookup
Relational algebra wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Concurrency control wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Ingres (database) wikipedia , lookup
Functional Database Model wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Clusterpoint wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Database - A database is a collection of information that is organized so that it can easily be accessed, managed, and updated. Databases typically contain aggregations of data records or files, such as sales transactions, product catalogs and inventories, and customer profiles. Typically, a database manager provides users the capabilities of controlling read/write access, specifying report generation, and analyzing usage. Databases are organized by columns, records, and tables. A column is a single piece of information; a record is one complete set of columns; and a table is a collection of records. For example, a telephone book is analogous to a table. It contains a list of records, each of which consists of three fields: name, address, and telephone number. To access information from a database, you need a Database Management System (DBMS). This is a collection of programs that enables you to enter, organize, and select data in a database. A table is defined as a set of rows that have the same attributes. A table is organized into rows and columns. A row usually represents an object and information about that object. Objects are typically physical objects or concepts. Creating the person table CREATE TABLE person ( num INT firstname VARCHAR(20) lastname VARCHAR(30) gender_code VARCHAR(1) birth_dttm DATETIME inactive_date DATETIME CONSTRAINT PK_Person PRIMARY KEY (num) ON [PRIMARY] ) Creating the phone table CREATE TABLE phone ( person_num INT NOT NULL, type_code CHAR(3) NOT NULL, area_code CHAR(3) NULL, exchange CHAR(3) NULL, extension CHAR(4) NULL, CONSTRAINT PK_phone PRIMARY KEY (person_num, type_code) ON [PRIMARY] ) NOT NULL, NULL, NULL, NULL, NULL, NULL, Creating the address table CREATE TABLE address ( person_num INT NOT NULL, type_code CHAR(4) NOT NULL, street1 CHAR(30) NULL, street2 CHAR(30) NULL, city CHAR(30) NULL, state CHAR(2) NULL, postal_code CHAR(10) NULL, CONSTRAINT PK_address PRIMARY KEY (person_num, type_code) ON [PRIMARY] ) Relational database - A relational database is a collection of data items organized as a set of tables from which data can be accessed or reassembled in many different ways without having to reorganize the database tables. The relational model specifies that the rows of a table have no specific order and that the rows, in turn, impose no order on the attributes. Applications access data by specifying queries, which use operations such as select to identify rows, project to identify attributes, and join to combine relations. Relations can be modified using the insert, delete, and update operators. In addition to being relatively easy to create and access, a relational database has the important advantage of being easy to extend. After the original database creation, a new data category can be added without requiring that all existing applications be modified. A relational database is a set of tables containing data organized into predefined categories. Each table (which is sometimes called a relation) contains one or more data categories in columns. Each row contains a unique instance of data for the categories defined by the columns. For example, a typical Investigator database would include a table (Investigator) that describes an investigator with columns (fields) for name, address, phone number, affiliation, degree, etc. Another table would describe a sample: id, number, type, lithology, date taken, etc. A user of the database could obtain a view of the database that fitted the user's needs. For example, a head of the Geological Survey might like a view or report on all geological quadrangle maps that were completed by a certain data. A geologist in the same survey could, from the same tables, obtain a report on collected samples which need chemical analysis. The standard user and application program interface to a relational database is the Structured Query Language (SQL). SQL statements are used both for interactive queries for information from a relational database and for gathering data for reports. Domain - When creating a relational database, you can define the domain of possible values in a data column and further constraints that may apply to that data value. For example, a domain of maps could allow up to ten possible map names but be constrained in one table to allowing only three of these map names to be specifiable Constraints - Constraints allow to further restrict the domain of an attribute. You can place constraints to limit the type of data that is stored in a table. For example, a constraint can restrict a given integer attribute to values between 1 and 5. A UNIQUE constraint ensures that all values in a column are distinct. CREATE TABLE Customer (ID integer Unique, Last_Name varchar (100), First_Name varchar(100)); Column "ID" has a unique constraint, and cannot include duplicate values. ID Last_Name First_Name 1 Johnson Gloria 2 Smith Gina 3 Spalding David Executing the following SQL statement, INSERT INTO Customer values ('3','Jones','Kaila'); will result in an error because '3' already exists in the ID column, thus trying to insert another row with that value violates the UNIQUE constraint. A DEFAULT constraint provides a default value for a column when the INSERT INTO statement does not provide a specific value. CREATE TABLE Student (ID integer Unique NOT NULL, Last_Name varchar (100), First_Name varchar (100), Score integer DEFAULT 75); INSERT INTO Student (ID, Last_Name, First_Name) values ('1','James','Alex'); The table will look like the following: ID Last_Name First_Name Score 1 James Alex 75 Even though we didn't specify a value for the "Score" column in the INSERT INTO statement, it does get assigned the default value of 75 since we had already set 75 as the default value for this column. A CHECK constraint ensures that all values in a column satisfy certain conditions. Once defined, the database will only insert a new row or update an existing row if the new value satisfies the CHECK constraint. The CHECK constraint is used to ensure data quality For example, in the following CREATE TABLE statement, CREATE TABLE Customer (ID integer CHECK (ID > 0), Last_Name varchar (30), First_Name varchar(30)); Column "ID" has a constraint -- its value must only include integers greater than 0. So, attempting to execute the following statement, INSERT INTO Customer values ('-5','Smith','Lynn'); will result in an error because the values for ID must be greater than 0. Primary Key - A primary key is used to uniquely identify each row in a table. 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. Each primary key value must be unique within the table Table CUSTOMER ID Primary Key Last_Name First_Name CREATE TABLE Customer (ID integer PRIMARY KEY, Last_Name varchar(100), First_Name varchar(100)); Foreign key – A foreign key is a reference to a key in another table. 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. Table ORDERS Order_ID Primary Key Order_Date Customer_ID Foreign Key Amount CREATE TABLE ORDERS (Order_ID integer primary key, Order_Date datetime, Customer_ID integer references CUSTOMER(ID), Amount double); Index - An index provides quick access to data. A database index speeds up the retrieval of data. Creating the proper index can drastically increase the performance of an application. A database index is used in much the same way a reader uses a book index. One way to find all references to UPDATE statements in a SQL book would be to begin on page one and scan each page of the book. We could mark each time we find the word UPDATE until we reach the end of the book. This approach is very time consuming and labor intensive. Alternately, we can also use the index in the back of the book to find a page number for each occurrence of the UPDATE statements. This approach produces the same results as above, but with tremendous savings in time. When a database has no index to use for searching, the result is similar to the reader who looks at every page in a book to find a word: the database engine needs to visit every row in a table. In database terminology this is called a table scan. Indices can be created on any combination of attributes on a table. CREATE TABLE Subscribers ( ID INT PRIMARY KEY, email_address VARCHAR(255), First_name VARCHAR(255), Last_name VARCHAR(255) ); If you want to quickly find an email address, create an index on the email_address field: CREATE INDEX Subscriber_email ON Subscribers(email_address); These SQL Statement allow to quickly find an email address: SELECT firstname, lastname FROM subscribers WHERE email_address='[email protected]'; Create Table Table1 ( Emp_ID Int, Emp_Name Varchar(200) ) When you first create a new table, there is no index created by default. Use the following statement to create an index for this table. CREATE INDEX EmpID_Idx ON Table1 (Emp_ID); Joins – table joins are used to query data from two or more tables, based on a relationship between certain columns in these tables. Tables in a database are often related to each other with keys. Table Store_Information store_name Sales Date Los Angeles $1500 Jan-05-1999 San Diego $250 Jan-07-1999 Los Angeles $300 Jan-08-1999 Boston $700 Jan-08-1999 Table Geography Region_name store_name East Boston East New York West Los Angeles West San Diego To get the sales information by region, we have to combine the information from the two tables. These two tables are linked by the common field, "store_name". The following SQL will retrieve the sales data by the region. SELECT A1.region_name REGION, SUM(A2.Sales) SALES FROM Geography A1 JOIN Store_Information A2 ON A1.store_name = A2.store_name GROUP BY A1.region_name Result: REGION SALES East $700 West $2050 JOIN: Return rows when there is at least one match in both tables LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table FULL JOIN: Return rows when there is a match in one of the tables Views - A Database View is a subset of the database sorted and displayed in a particular way. For each view, you can control which columns are displayed, what order they are displayed in, how the data is sorted, and what types of records to display. A view consists of rows and columns just like a table. The difference between a view and a table is that views are definitions built on top of other tables (or views), and do not hold data themselves. If a data item is changing in the underlying table, the same change is reflected in the view. A view can be built on top of a single table or multiple tables. It can also be built on top of another view. CREATE TABLE Customer (First_Name varchar(100), Last_Name varchar(100), Address varchar(50), City varchar(30), Country varchar(25), Birth_Date date NOT NULL) Use the following syntax to create a view called V_Customer that contains only the First_Name, Last_Name, and Country columns from this table. CREATE VIEW V_Customer AS SELECT First_Name, Last_Name, Country FROM Customer Now we have a view called V_Customer with the following structure: View V_Customer (First_Name char(100), Last_Name char(100), Country char(25)) Alter Table - Once a table is created in the database, you can use the Alter statement to change the structure of the table. For example: Add a column Drop a column Change a column name Change the data type for a column ALTER table Customer add Gender char(1) ALTER table Customer drop Country Drop Table - Sometimes we may need to delete a table in the database. This allows to get rid of tables not needed anymore and frees up database space. Use the following command to delete the Customer table. DROP TABLE Customer TRUNCATE TABLE - Sometimes we may need to delete all of the data in a table. One way of doing this is with DROP TABLE. But what if we wish to simply get rid of the data but not the table itself? For this, we can use the TRUNCATE TABLE command. Use the following command to delete all data in Customer table. TRUNCATE TABLE Customer INSERT INTO Statement – Insert statement is used to add one or more rows to a table INSERT INTO Customer values ('4','Adams','Joe') – adds one row INSERT INTO Store_Information (store_name, Sales, Date) SELECT store_name, Sales, Date FROM Sales_Information WHERE Year(Date) = 1998 UPDATE Statement - allows to update data in an existing table. UPDATE Store_Information SET Sales = 2000 WHERE store_name = "Los Angeles" AND Date = "Jan-08-1999" DELETE Statement – allows to remove rows from a table. DELETE FROM Store_Information store_name = "Los Angeles" Stored procedures - A stored procedure is executable code that is associated with, and generally stored in, the database. Stored procedures usually collect and customize common operations, like inserting a row into a table, gathering statistical information about usage patterns, or encapsulating complex business logic and calculations. Normalization - is the process of efficiently organizing data in a database. The goal of normalization is to eliminate the duplication of data, which in turn prevents data manipulation anomalies and loss of data integrity. It eliminates redundant data (storing the same data in more than one table) and ensuring data dependencies are logical (only storing related data in a table). Normalization reduces the amount of space a database consumes and ensures data is logically stored. First Normal Form (1NF) First normal form (1NF) sets the very basic rules for an organized database: Eliminate duplicative columns from the same table. Create separate tables for each group of related data and identify each row with a unique column or set of columns (the primary key). Second Normal Form (2NF) Second normal form (2NF) further addresses the concept of removing duplicative data: Meet all the requirements of the first normal form. Remove subsets of data that apply to multiple rows of a table and place them in separate tables. Create relationships between these new tables and their predecessors through the use of foreign keys. Third Normal Form (3NF) Third normal form (3NF) goes one large step further: Meet all the requirements of the second normal form. Remove columns that are not dependent upon the primary key. Fourth Normal Form (4NF) Finally, fourth normal form (4NF) has one additional requirement: Meet all the requirements of the third normal form. A relation is in 4NF if it has no multi-valued dependencies.