Presentation is loading. Please wait.

Presentation is loading. Please wait.

MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.

Similar presentations


Presentation on theme: "MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more."— Presentation transcript:

1 MySQL Tutorial

2 Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more tables

3 Tables Tables are structure that are used to store information Each table consists of 1 or more fields For simplicity, each field name should be declared as a single word (ex.: username, user_nname, not User Name). This will avoid multiple errors and issues Each table should have a field called id that should be defined as the primary key (the primary key uniquely defines each record in the table)

4 Primary Keys The PRIMARY KEY constraint uniquely identifies each record in a database table. Primary keys must contain UNIQUE values. A primary key column cannot contain NULL values. Most (in our case, ALL) tables should have a primary key, and each table can have only ONE primary key.

5 NOT NULL The NOT NULL constraint enforces a column to NOT accept NULL values The NOT NULL constraint enforces a field to always contain a value This means that you cannot insert a new record, or update a record without adding a value to this field

6 UNIQUE The UNIQUE constraint uniquely identifies each record in a database table The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table

7 DEFAULT The DEFAULT constraint is used to insert a default value into a column The default value will be added to all new records, if no other value is specified

8 Creating a table In order to create a new table using SQL queries, the following rule applies to generating the basic query: CREATE TABLE table_name ( column_name1 data_type(size), column_name2 data_type(size), column_name3 data_type(size),.... );

9 Database Table Queries There are 4 basic operators that are used where working on database table queries: SELECT INSERT UPDATE DELETE

10 Database Table Queries (continued) There are 4 basic components when constructing a table query: The operator (insert, update,…) The field(s) The table The condition

11 Database Table Queries (SELECT query) SELECT column_name,column_name FROM table_name; Select * from table; (retrieves all entries in table) Select id, username from table; (retrieves id and username of each entry in table) Select id, username from table where username like ‘ab%cd’; (retrieves id and username of each entry in table where the username starts with ‘ab’ and ends with ‘cd’) Select id, username from table where username like ‘%cd’; (retrieves id and username of each entry in table where the username ends with ‘cd’) …

12 Database Table Queries (DELETE query) DELETE FROM table_name WHERE some_column=some_value; Delete from table where id = value; (deletes the table entry with id = value)

13 Database Table Queries (UPDATE query) UPDATE table_name SET column1=value1,column2=value2,... WHERE some_column=some_value; Update users set password=‘12345’; (updates all entries in the users table and sets the password to ‘12345’ for each entry) Update users set password=‘12345’ where id=1; (updates the entry in the users table where the id = 1 and sets its password to ‘12345’)

14 Database Table Queries (INSERT query) INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...); For the inserted entry: Column1 will have the value1 Column2 will have the value2 Column3 will have the value3

15 Database Table Queries (INSERT query continued) INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...), (value11,value12,value13,...); For the 1 st inserted entry: Column1 will have the value1 Column2 will have the value2 Column3 will have the value3 For the 2 nd inserted entry: Column1 will have the value11 Column2 will have the value12 Column3 will have the value13

16 Using multiples within a single query (JOIN) An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them SELECT table1.column_name1, table2.column_name2, table1.column_name3 FROM table1 JOIN table2 ON table1.key_column_name1=table2.key_column_name2;

17 Using multiples within a single query (JOIN continued) You can join as many tables as you need Each join clause MUST be clearly defined in the ON section of the clause Ex.: SELECT * FROM users JOIN persInfo ON users.id = persInfo.userId

18 Grouping the resultset from a query When selecting entries from a database table, it is sometimes necessary to group certain entries together. This can be accomplished by using the GROUP BY statement The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns

19 Grouping the resultset from a query (continued) Ex.: SELECT persInfo.first_name, SUM( sales.sale ) FROM sales JOIN persInfo on sales.userId=persInfo.userId GROUP BY userId; Returns the first name (from the persInfo table) and the total sum of sales (calculated from the sale table) for each different userId

20 Condition statements (AND & OR) The AND & OR operators are used to filter records based on more than one condition The AND operator displays a record if both the first condition AND the second condition are true The OR operator displays a record if either the first condition OR the second condition is true

21 Condition statements (LIKE) The LIKE operator is used in a WHERE clause to search for a specified pattern in a column In SQL, wildcard characters are used with the SQL LIKE operator A wildcard character can be used to substitute for any other character(s) in a string WildcardDescription %A substitute for zero or more characters

22 Condition statements (BETWEEN) The BETWEEN operator selects values within a range The values can be numbers, text, or dates


Download ppt "MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more."

Similar presentations


Ads by Google