Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1

RDBMS structure enables you to work with that data, you are ready to learn
how to input and output data from the database. This is where SQL comes in.


Understanding SQL Basics


SQL (pronounced “S-Q-L” or “sequel,” depending on who is talking) is a
database query language understood by virtually all RDBMSs available today.
You use SQL statements to get data into and retrieve data from a database. As
with statements in any language, SQL statements have a defined structure that
determines their meanings and functions.


As a DBA, you should understand the basics of SQL, even if you will not be
doing any of the actual programming yourself. Fortunately, SQL is similar to
standard English, so learning the basics is simple.


Creating Tables


As mentioned previously, an RDBMS stores data in tables that look similar to
spreadsheets. Of course, before you can store any data in a database, you need
to create the necessary tables and columns to store the data. You do this by
using the CREATE statement.


For example, the cd_collection table from Figure 28.2 has five columns,
or fields: id, title, artist, year, and rating.


SQL provides several column types for data that define what kind of data will
be stored in the column. Some of the available types are INT, FLOAT, CHAR,
and VARCHAR. Both CHAR and VARCHAR hold text strings, with the
difference being that CHAR holds a fixed-length string, whereas VARCHAR
holds a variable-length string.


There are also special column types, such as DATE, that only take data in a
date format, and ENUM (enumeration), which can be used to specify that only
certain values are allowed. If, for example, you want to record the genre of
your CDs, you could use an ENUM column that accepts only the values POP,
ROCK, EASY_LISTENING, and so on. You learn more about ENUM later in
this chapter.


Looking at the cd_collection table, you can see that three of the
columns hold numeric data and the other two hold string data. In addition, the
character strings are of variable length. Based on this information, you can
discern that the best type to use for the text columns is type VARCHAR, and

Free download pdf