introduction to databases and sql. what is a database? a database is an organized way of holding...
TRANSCRIPT
TEC 319Introduction to databases and SQL
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
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
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
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
Connecting to MySQL
Using the command lineOpen Applications ->Terminal Emulator in LinuxType:mysql –uroot –p [Press Enter]Enter password: tec319 [Press Enter]
Connecting to MySQL
mysql –uusername –ppasswordORmysql --user=yourname --password
Examplemysql –user=root --passwordEnter password:tec319[Press Enter]
Executing commands
Executing commands
To use a specific databasemysql> \u databasename
mysql> select * from user;
Records are grouped togethermysql> select * from usermysql> \G
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.
GUI MySQL Editor
Go to http://localhost/phpMyAdmin in a browser
For user and password enter:root tec319
Basic SQL Syntax
The SELECT statement
Selecting specific columns Syntax Select columnName from Tablename;
egSelect firstName from students;
Selecting multiple columnsSelect firstName, lastName from students;
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;
Filtering and Sorting Data
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
Exact match
Syntax exampleSelect firstName, lastName from studentswhere lastName = ‘Smith’;
Select firstName, lastName from studentswhere lastName != ‘Smith’;
Select product_name, product_description from productswhere product_id=3443;
Filtering on a range of values Syntax exampleSelect * from products where price <=9.99;
Select last_namefrom customer_contactswhere last_name > ‘G’;
String values should be surrounded in single quotes
Ordering results – Order By Clause Sorting on a single columnSelect * from productsorder by price;
Select first_name, last_namefrom customer_contactswhere customer_code =‘DEFC’order by last_name;
Sorting on multiple columnsSelect order_date, customer_codefrom ordersorder by order_date, customer_code;
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 toSelect * 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;
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;