Databases Flashcards
What’s the select statement used for?
The select statement is used to query the database and retrieve selected data that match the criteria that you specify.
Give an example of a select statement
select "column1" [,"column2",etc] from "tablename" [where "condition"]; [] = optional
What is some of the syntax around columns?
The column names that follow the select keyword determine which columns will be returned in the results. You can select as many column names that you’d like, or you can use a “*” to select all columns.
How do you select what table to draw results from?
The table name that follows the keyword from specifies the table that will be queried to retrieve the desired results.
select “column1”
[,”column2”,etc]
from “tablename” (
What does the where keyword do?
The where clause (optional) specifies which data values or rows will be returned or displayed, based on the criteria described after the keyword where. Conditional selections used in the where clause: = Equal > Greater than < Less than >= Greater than or equal <= Less than or equal <> Not equal to
What does the LIKE condition do when it follows the where keyword?
The LIKE pattern matching operator can also be used in the conditional selection of the where clause. Like is a very powerful operator that allows you to select only rows that are “like” what you specify. The percent sign “%” can be used as a wild card to match any possible character that might appear before or after the characters specified.
Give some examples of how to use the LIKE condition
select first, last, city
from empinfo
where first LIKE ‘Er%’;
This SQL statement will match any first names that start with 'Er'. Strings must be in single quotes. Or you can specify; select first, last from empinfo where last LIKE '%s';
This statement will match any last names that end in a ‘s’.
select * from empinfo
where first = ‘Eric’;
This will only select rows where the first name equals ‘Eric’ exactly.
How do you use the create table statement?
The create table statement is used to create a new table. Here is the format of a simple create table statement: create table "tablename" ("column1" "data type", "column2" "data type", "column3" "data type");
Format of create table if you were to use optional constraints: create table "tablename" ("column1" "data type" [constraint], "column2" "data type" [constraint], "column3" "data type" [constraint]); [ ] = optional
What are the most common data types used in tables?
char(size)-Fixed-length character string. Size is specified in parenthesis. Max 255 bytes.
varchar(size)-Variable-length character string. Max size is specified in parenthesis.
number(size) Number value with a max number of column digits specified in parenthesis.
date-Date value
number(size,d)-Number value with a maximum number of digits of “size” total, with a maximum number of “d” digits to the right of the decimal.
Give an example of how you’d create a table
create table employee (first varchar(15), last varchar(20), age number(3), address varchar(30), city varchar(20), state varchar(20));
To create a new table, enter the keywords create table followed by the table name, followed by an open parenthesis, followed by the first column name, followed by the data type for that column, followed by any optional constraints, and followed by a closing parenthesis. It is important to make sure you use an open parenthesis before the beginning table, and a closing parenthesis after the end of the last column definition. Make sure you separate each column definition with a comma. All SQL statements should end with a “;”.
What are constraints?
When tables are created, it is common for one or more columns to have constraints associated with them. A constraint is basically a rule associated with a column that the data entered into that column must follow. For example, a “unique” constraint specifies that no two records can have the same value in a particular column. They must all be unique. The other two most popular constraints are “not null” which specifies that a column can’t be left blank, and “primary key”. A “primary key” constraint defines a unique identification of each record (or row) in a table.
What is the insert statement used for?
The insert statement is used to insert or add a row of data into the table.
How do you use the insert statement?
To insert records into a table, enter the key words insert into followed by the table name, followed by an open parenthesis, followed by a list of column names separated by commas, followed by a closing parenthesis, followed by the keyword values, followed by the list of values enclosed in parenthesis. The values that you enter will be held in the rows and they will match up with the column names that you specify. Strings should be enclosed in single quotes, and numbers should not.
insert into “tablename”
(first_column,…last_column)
values (first_value,…last_value);
Give an example of how you’d use the insert statement
insert into employee
(first, last, age, address, city, state)
values (‘Luke’, ‘Duke’, 45, ‘2130 Boars Nest’,
‘Hazard Co’, ‘Georgia’);
Note: All strings should be enclosed between single quotes: ‘string’
What does the update statement do?
The update statement is used to update or change records that match a specified criteria. This is accomplished by carefully constructing a where clause. update "tablename" set "columnname" = "newvalue" [,"nextcolumn" = "newvalue2"...] where "columnname" OPERATOR "value" [and|or "column" OPERATOR "value"];
[] = optional