Introduction to databases and SQL. What is a database? A database is an organized way of holding...
-
Upload
adela-randall -
Category
Documents
-
view
228 -
download
2
Transcript of Introduction to databases and SQL. What is a database? A database is an organized way of holding...
![Page 1: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/1.jpg)
TEC 319Introduction to databases and SQL
![Page 2: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/2.jpg)
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
![Page 3: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/3.jpg)
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
![Page 4: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/4.jpg)
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
![Page 5: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/5.jpg)
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
![Page 6: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/6.jpg)
Connecting to MySQL
Using the command lineOpen Applications ->Terminal Emulator in LinuxType:mysql –uroot –p [Press Enter]Enter password: tec319 [Press Enter]
![Page 7: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/7.jpg)
Connecting to MySQL
mysql –uusername –ppasswordORmysql --user=yourname --password
Examplemysql –user=root --passwordEnter password:tec319[Press Enter]
![Page 8: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/8.jpg)
Executing commands
![Page 9: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/9.jpg)
Executing commands
To use a specific databasemysql> \u databasename
mysql> select * from user;
Records are grouped togethermysql> select * from usermysql> \G
![Page 10: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/10.jpg)
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.
![Page 11: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/11.jpg)
GUI MySQL Editor
Go to http://localhost/phpMyAdmin in a browser
For user and password enter:root tec319
![Page 12: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/12.jpg)
Basic SQL Syntax
![Page 13: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/13.jpg)
The SELECT statement
Selecting specific columns Syntax Select columnName from Tablename;
egSelect firstName from students;
Selecting multiple columnsSelect firstName, lastName from students;
![Page 14: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/14.jpg)
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;
![Page 15: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/15.jpg)
Filtering and Sorting Data
![Page 16: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/16.jpg)
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
![Page 17: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/17.jpg)
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;
![Page 18: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/18.jpg)
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
![Page 19: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/19.jpg)
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;
![Page 20: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/20.jpg)
Sorting on multiple columnsSelect order_date, customer_codefrom ordersorder by order_date, customer_code;
![Page 21: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/21.jpg)
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;
![Page 22: Introduction to databases and SQL. What is a database? A database is an organized way of holding together pieces of information A database refers.](https://reader035.fdocuments.us/reader035/viewer/2022062321/56649e905503460f94b94b4b/html5/thumbnails/22.jpg)
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;