This lesson describes all Ihe SQL statements that you need to perform these actions again. Tins lesson also covers the use of SQL*Plus commands to execute. SQL is a database computer language designed for the retrieval and This tutorial is prepared for beginners to help them understand the basic as well as the. SQL is a database computer language designed for the retrieval and This reference has been prepared for the beginners to help them understand the basic to.
|Language:||English, Spanish, Arabic|
|Genre:||Politics & Laws|
|PDF File Size:||13.41 MB|
|Distribution:||Free* [*Regsitration Required]|
To change some of the values of columns of a table, you can use the UPDATE command. Changes are provided as a comma-separated list of column/value. Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of. O'Reilly Media, Inc. Learning SQL, the image of an Andean. SQL · A Beginner's Guide. Third Edition. Andy Oppel. Robert Sheldon. New York Chicago San Francisco. Lisbon London Madrid Mexico City. Milan New Delhi.
A table is made up of columns or fields and rows records. For clarity, they are shown in uppercase. The names or identifiers database names, table names, column names, etc.
Hence, it is best to treat identifiers as case-sensitive. A " test " database is provided during installation for your testing.
Let us begin with a simple example - a product sales database. A product sales database typically consists of many tables, e. Let's call our database " southwind " inspired from Microsoft's Northwind Trader sample database. We shall begin with the first table called " products " with the following columns having data types as indicated and rows:. For example,.
For robustness, the SHOW command back-quotes all the identifiers, as illustrated in the above example. The command " USE databaseName " sets a particular database as the default or current database. You can reference a table in the default database using tableName directly. But you need to use the fully-qualified databaseName. In our example, we have a database named " southwind " with a table named " products ".
If we issue " USE southwind " to set southwind as the default database, we can simply call the table as " products ". Otherwise, we need to reference the table as " southwind. To create a table, you need to define all its columns, by providing the columns' name , type , and attributes. Let's create a table " products " in our database " southwind ". We define 5 columns in the table products: The types are:.
NULL is a special value indicating "no value", "unknown value" or "missing value". In our case, these columns shall have a proper value. We also set the default value of the columns.
The column will take on its default value, if no value is specified during the record creation. We set the column productID as the so-called primary key. Values of the primary-key column must be unique. Every table shall contain a primary key. This ensures that every row can be distinguished from other rows.
You can specify a single column or a set of columns e.
An index is build automatically on the primary-key column to facilitate fast search. Primary key is also used as reference by other tables.
Let's fill up our " products " table with rows. Take note that strings must be enclosed with a pair of single quotes or double quotes. The most common, important and complex task is to query a database for a subset of data that meets your needs - with the SELECT command.
The ordering of string depends on the so-called collation chosen. You can perform arithmetic operations on numeric fields using arithmetic operators, as tabulated below:. You can also invert a condition using operator NOT. For examples,. NULL is a special value, which represent "no value", "missing value" or "unknown value". If several rows have the same value in columnA , it will be ordered according to columnB , and so on. For strings, the ordering could be case-sensitive or case-insensitive, depending on the so-called character collating sequence used.
To continue to the following records , you could specify the number of rows to be skipped, followed by the number of rows to be displayed in the LIMIT clause, as follows:. You could use the keyword AS to define an alias for an identifier such as column name, table name. The alias will be used in displaying the name. It can also be used as reference.
Take note that the identifier " Unit Price " contains a blank and must be back-quoted. You can also concatenate a few columns as one e. There are several ways to add data into the database: The file extension of ".
Our example so far involves only one table " products ". A practical database contains many related tables.
Products have suppliers. If each product has one supplier, and each supplier supplies only one product known as one-to-one relationship , we can simply add the supplier's data name, address, phone number into the products table. Suppose that each product has one supplier, and a supplier may supply zero or more products known as one-to-many relationship.
Putting the supplier's data into the products table results in duplication of data. This is because one supplier may supply many products, hence, the same supplier's data appear in many rows.
This not only wastes the storage but also easily leads to inconsistency as all duplicate data must be updated simultaneously. The situation is even more complicated if one product has many suppliers, and each supplier can supply many products, in a many-to-many relationship. Suppose that each product has one supplier, and each supplier supplies one or more products.
We could create a table called suppliers to store suppliers' data e. We create a column with unique value called supplierID to identify every suppliers.
We set supplierID as the primary key for the table suppliers to ensure uniqueness and facilitate fast search. To relate the suppliers table to the products table, we add a new column into the products table - the supplierID. We then set the supplierID column of the products table as a foreign key references the supplierID column of the suppliers table to ensure the so-called referential integrity.
We need to first create the suppliers table, because the products table references the suppliers table. The suppliers table is known as the parent table; while the products table is known as the child table in this relationship.
Next, we shall add a foreign key constraint on the supplierID columns of the products child table to the suppliers parent table, to ensure that every supplierID in the products table always refers to a valid supplierID in the suppliers table - this is called referential integrity.
For example, to list the product's name in products table and supplier's name in suppliers table , we could join the two table via the two common supplierID columns:. In the above query result, two of the columns have the same heading " name ".
We could create aliases for heading s. The database diagram is as illustrated. The link indicates a one-to-many relationship between products and suppliers. Suppose that a product has many suppliers; and a supplier supplies many products in a so-called many-to-many relationship. The above solution breaks. You cannot include the supplierID in the products table, as you cannot determine the number of suppliers, and hence, the number of columns needed for the supplierID s.
Similarly, you cannot include the productID in the suppliers table, as you cannot determine the number of products. To resolve this problem, you need to create a new table, known as a junction table or joint table , to provide the linkage. The primary key of the table consists of two columns: This primary key is defined to ensure uniqueness. Two foreign keys are defined to set the constraint to the two parent tables.
Next, remove the supplierID column from the products table. This column was added to establish the one-to-many relationship.
It is no longer needed in the many-to-many relationship. Before this column can be removed, you need to remove the foreign key that builds on this column.
To remove a key in MySQL, you need to know its constraint name, which was generated by the system. The database diagram is as follows.
Both products and suppliers tables exhibit a one-to-many relationship to the junction table.
The many-to-many relationship is supported via the junction table. Suppose that some products have optional data e.
Before we conclude this example, let's run the mysqldump utility program to dump out backup the entire southwind database. You can restore from the backup by running the script either:. In the relational model, a table shall not contain duplicate rows, because that would create ambiguity in retrieval.
To ensure uniqueness, each table should have a column or a set of columns , called primary key , that uniquely identifies every record of the table. For example, an unique number customerID can be used as the primary key for the customers table; productCode for products table; isbn for books table. A primary key is called a simple key if it is a single column; it is called a composite key if it is made up of several columns. The primary key is often used to relate to other tables.
A foreign key of a child table is used to reference the parent table. Foreign key constraint can be imposed to ensure so-called referential integrity - values in the child table must be valid values in the parent table. We define the foreign key when defining the child table, which references a parent table, as follows:.
Indexes or Keys can be created on selected column s to facilitate fast search. SQL has been around for many years and it will be around for many more coming years. It is also one of the matured technology, hence there are a lot of free resources e. I have been sharing some free eBooks on different technologies e. Some of the books are available for free to read online others you can download in PDF format. These are the books which are made available free from publisher and authors for the benefit of the community.
You can either download their PDF version for offline reading or you can read them online. Use the Index, Luke! Markus Winand Download: This book is for developers; it avoids unnecessary details about database internals.
That's all in this list of Free SQL books for beginners and experienced developers.