hin your database tables. When processing and displaying data from your database, the primary key is likely the field that your relational database will depend on to operate. A foreign key is used when tying together two related tables. A foreign key is used when a second table references a piece of information within the first table. Foreign keys can begin to get a little complicated. Foreign keys are a more advanced feature of SQL and it is likely you won't be using them in your first databases.
Adding data with SQL
When inserting data into SQL, you'll be using the INSERT INTO statement.
"INSERT INTO tablename VALUES (value1, value2, value3...);
When inserting data into a database it is important to make sure that you are inserting the data into the correct fields. In the first example the data is not told which columns to fill, only their values. It is usually best to specify the column names to ensure the data is being stored correctly. This second example will specify the column name.
"INSERT INTO tablename (column1, column2, column3) VALUES (value1, value2, value3);"
Deleting data with SQL
When deleting data with SQL it is absolutely vital that you are deleting the correct data. It is best if you know what the primary key value is for the data you want to delete. Because the primary key is unique, when you delete data matching the primary key it should only match the one row of data. It is also a good idea to put a limit of 1 on the query just in case. A limit of 1 will ensure that your query will only delete one row of data. An example is:
"DELETE * FROM tablename WHERE id=1 LIMIT 1;"
Emptying an entire table can be done with
"DELETE * FROM tablename;"
Updating Data with SQL
The update query is used when you want to change existing data within the database. Again you should use the primary key to limit your query to only one row of data. An example of an update query is:
"UPDATE tablename SET column1=value1, column2=value2... WHERE id=1"
Retrieving Data with SQL
The query used to withdraw data from the database is called the select query and is where the magic of SQL happens. Select statements can be incredibly simple or deviously complex. To select information from a database you should use the following query:
"SELECT * FROM tablename
You can select specific columns by substituting column names for the * in the example query.
PHP and SQL
SQL is a great language, however it doesn't interact with the web at all. In order to build a web page that uses a SQL database you need to use a language that interacts with the SQL and can print out HTML. The most commonly used language for this purpose is PHP. Unfortunately this guide is not long enough to teach you how to interact with your SQL server, however the following links should help you.