Download presentation
Presentation is loading. Please wait.
Published byAdela Randall Modified over 9 years ago
1
Introduction to databases and SQL
2
What is a database? A database is an organized way of holding together pieces of information A database refers to a collection of data and not the means by which it is stored Software that is used to manage data in a database is known as a database-management system (DBMS) In a relational database-management system (RDMS), data is stored as a number of different tables that are related to each other in some way
3
Flat file vs RDBMS Data can also be stored in a huge flat table Each row in a flat table contains all the information about a system for that record The difference between a relational database over a flat table system is that large databases can be constructed from different tables which contain only info relevant to that table
4
Why choose MySQL? MySQL is distributed as open source software under the GNU General Public License It is free to use There are commercial versions where you can purchase support Even with commercial support, total cost of ownership is much less than competitors (Oracle, SQL Server) MySQL is robust, powerful and scalable MySQL has replication and clustering to guarantee 100% availability Extensive user base and wide community support and adoption (Facebook, Google, Yahoo, Apple, Microsoft, even Oracle) Majority of website databases run on MySQL It is a desired skill by employers
5
What is SQL? Structured Query Language (SQL) It is a common way to retrieve and manage database information SQL uses descriptive English keywords so most queries are easy to understand MySQL implementation of SQL conforms to the ANSI SQL standards We will be learning the basics of SQL and enough to build simple web applications Extensive database design, transactions, stored procedures, triggers etc is beyond the scope of this course Entire courses and major parts of degrees are devoted to database topics
6
Connecting to MySQL Using the command line Open Applications ->Terminal Emulator in Linux Type: mysql –uroot –p [Press Enter] Enter password: tec319 [Press Enter]
7
Connecting to MySQL mysql –uusername –ppassword OR mysql --user=yourname --password Example mysql –user=root --password Enter password:tec319[Press Enter]
8
Executing commands
9
To use a specific database mysql> \u databasename mysql> select * from user; Records are grouped together mysql> select * from user mysql> \G
10
Executing commands Running a script mysql> mysql –user=yourname --password dbname < script.sql The above command will execute the sql code in the script.sql tag on the database called dbname.
11
GUI MySQL Editor Go to http://localhost/phpMyAdmin in a browserhttp://localhost/phpMyAdmin For user and password enter: roottec319
12
Basic SQL Syntax
13
The SELECT statement Selecting specific columns Syntax Select columnName from Tablename; eg Select firstName from students; Selecting multiple columns Select firstName, lastName from students;
14
Selecting all records In order to retrieve all column values from a table, the * character is used Select * from students; Remember that the semicolon (;) is needed to terminate a sql query Select * from courses;
15
Filtering and Sorting Data
16
Where Clause Where is added to select statements to tell MySQL to filter the query results based on a given rule Rules in a WHERE clause refer to data values returned by the query Only rows that have values which meet the criteria in the rule are returned
17
Exact match Syntax example Select firstName, lastName from students where lastName = ‘Smith’; Select firstName, lastName from students where lastName != ‘Smith’; Select product_name, product_description from products where product_id=3443;
18
Filtering on a range of values Syntax example Select * from products where price <=9.99; Select last_name from customer_contacts where last_name > ‘G’; String values should be surrounded in single quotes
19
Ordering results – Order By Clause Sorting on a single column Select * from products order by price; Select first_name, last_name from customer_contacts where customer_code =‘DEFC’ order by last_name;
20
Sorting on multiple columns Select order_date, customer_code from orders order by order_date, customer_code;
21
Specifying sort order The default sort order is ascending depending on the data type of column e.g Select * from products order by weight; is equivalent to Select * from products order by weight asc; If you would like to sort the weight in descending order you can use: Select * from products order by weight desc;
22
Retrieving Database information Retrieve a list of Databases Show databases; Retrieve a list of tables Show tables; Show tables from company; Here company is the database name Retrieving a List of Columns Show columns from products;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.