Head First SQL 總結

Chapter 1 data and tableapp

CREATE DATABASE:Use this statement to set up the database that will hold all your tableside

USE DATABASE:Gets you inside the database to set up all your tables.post

CREATE TABLE:Starts setting up your table, but you’ll also need to know your COLUMN NAMES and DATA TYPES. You should have worked these out by analyzing the kind of data you’ll be putting in your table.this

DROP TABLE:Lets you delete a table if you make a mistake, but you’ll need to do this before you start using INSERT statements, which let you add the values for each column.atom

NULL and NOT NULL:You’ll also need to have an idea which columns should not accept NULL values to help you sort and search your data. You’ll need to set the columns to NOT NULL when you create your table.idea

DEFAULT :Lets you specify a default value for a column, used if you don’t supply a value for the column when you insert a record.orm

 

Chapter 2 SELECTip

SELECT *:Use this to select all the columns in a table. Escape with ' and \ Escape out apostrophes in your text data with an extra apostrophe or backslash in front of it .ci

= <> < > = :You've got a whole bunch of equality and inequality operators at your disposal.rem

IS NULL :Use this to create a condition to test for that pesky NULL value.

AND and OR With AND and OR, you can combine your conditional statements in your WHERE clauses for more precision.

NOT: NOT lets you negate your results and get the opposite values.

BETWEEN :Lets you select ranges of values. LIKE with % and _ Use LIKE with the wildcards to search through parts of text strings.

Chapter 3  DELETE and UPDATE

DELETE :This is your tool for deleting rows of data from your table. Use it with a WHERE clause to precisely pinpoint the rows you want to remove.

UPDATE :This statement updates an existing column or columns with a new value. It also uses a WHERE clause.

SET :This keyword belongs in an UPDATE statement and is used to change the value of an existing column.

Chapter 4 table design 

SHOW CREATE TABLE:Use this command to see the correct syntax for creating an existing table.

ATOMIC DATA:Data in your columns is atomic if it’s been broken down into the smallest pieces that you need.

ATOMIC DATA RULE 1:bits of the same type of data in the same column

ATOMIC DATA RULE 2: Atomic data can’t have multiple columns with the same type of data.

PRIMARY KEY:A column or set of columns that uniquely identifies a row of data in a table

FIRST NORMAL FORM (1NF):Each row of data must contain atomic values, and each row of data must have a unique identifier.

AUTO_INCREMENT :When used in your column declaration, that column will automatically be given a unique integer value each time an INSERT command is performed.

Chapter 5  ALTER

ALTER TABLE :Lets you change the name of your table and its entire structure while retaining the data inside of it.

ALTER with ADD: Lets you add a column to your table in the order you choose. ALTER with DROP Lets you drop a column from your table.

ALTER with CHANGE :Lets you change both the name and data type of an existing column.

ALTER with MODIFY :Lets you change just the data type of an existing column.

String functions :Let you modify copies of the contents of string columns when they are returned from a query. The original values remain untouched.

SUBSTRING_INDEX() and SUBSTRING()

RIGHT() and LEFT()

REVERSE(),LENGTH()

LTRIM(your_string) andRTRIM(your_string)

UPPER(your_string) and LOWER(your_string)

Chapter 6  advanced SELECT

ORDER BY :Alphabetically orders your results based on a column you specify.

GROUP BY :Consolidates rows based on a common column.

COUNT: Can tell you how many rows match a SELECT query without you having to see the rows. COUNT returns a single integer value.

DISTINCT :Returns each unique value only once, with no duplicates

AVG :Returns the average value in a numeric column. MAX and MIN Return the largest value in a column with MAX, and the smallest with MIN.

SUM: Adds up a column of numeric values.

LIMIT :Lets you specify exactly how many rows to return, and which row to start with.

Chapter 7  multi-table database design

Schema: A description of the data in your database, along with any other related objects and the way they all connect.

One-to-One relationship :Exactly one row of a parent table is related to one row of a child table.

One-to-Many relationship :A row in one table can have many matching rows in a second table, but the second table may only have one matching row in the first.

Many-to-Many relationship: Two tables are connected by a junction table, allowing many rows in the first to match many rows in the second, and vice versa.

First normal form (1NF) :Columns contain only atomic values, and no repeating groups of data are permitted in a column.

Transitive functional dependency :This means any non-key column is related to any of the other non-key columns.

Second normal form (2NF) :Your table must be in 1NF and contain no partial functional dependencies to be in 2NF.

Third normal form (3NF) :Your table must be in 2NF and have no transitive dependencies.

Foreign key :A column in a table that references the primary key of another table.

Composite key :This is a primary key made up of multiple columns, which create a unique key value

Chapter 8  joins and multi-table operations

INNER JOIN :Any join that combines the records from two tables using some condition.

NATURAL JOIN :An inner join that leaves off the 「ON」 clause. It only works if you are joining two tables that have the same column name.

CROSS JOIN :Returns every row from one table crossed with every row from the second table. Known by many other names including CARTESIAN JOIN and NO JOIN.

COMMA JOIN :The same thing as a CROSS JOIN, except a comma is used instead of the keywords CROSS JOIN.

EQUIJOIN and NON-EQUIJOIN :Both are inner joins. The EQUIJOIN returns rows that are equal, and the NON-EQUIJOIN returns any rows that are not equal.

Chapter 9  subqueries

Outer query: A query which contains an inner query or subquery.

Inner query :A query inside another query. It’s also known as a subquery.

Subquery :A query that is wrapped within another query. It’s also known as an inner query.

Noncorrelated subquery :A subquery that stands alone and doesn’t reference anything from the outer query.

Correlated Subquery :A subquery that relies on values returned from the outer query

Chapter 10  outer joins, self-joins, and unions

SELF-REFERENCING FOREIGN KEY :This is a foreign key in the same table it is a primary key of, used for another purpose.

LEFT OUTER JOIN A LEFT OUTER JOIN :takes all the rows in the left table and matches them to rows in the RIGHT table.

SELF-JOIN :The SELF-JOIN allows you to query a single table as though there were two tables with exactly the same information in them.

RIGHT OUTER JOIN :A RIGHT OUTER JOIN all the rows in the right table takes and matches them to rows in the LEFT table

UNION and UNION ALL :A UNION combines the results of two or more queries into one table, based on what you specify in the column list of the SELECT. UNION hides the duplicate values, UNION ALL includes duplicate values.

CREATE TABLE AS: Use this command to create a table from the results of any SELECT statement.

INTERSECT: Use this keyword to return only values that are in the first query AND also in the second query

EXCEPT: Use this keyworld to return only values that are in the first query BUT NOT in the second query.

Chapter 11  constraints, views, and transactions

VIEWS :Use a view to treat the results of a query as a table. Great for turning complex queries into simple ones.

UPDATABLE VIEWS :These are views that allow you to change the data in the underlying tables. These views must contain all NOT NULL columns of the base table or tables.

NON-UPDATABLE VIEWS :Views that can’t be used to INSERT or UPDATE data in the base table.

CHECK CONSTRAINTS: Use these to only allow specific values to be inserted or updated in a table.

CHECK OPTION: Use this when creating an updatable view to force all inserts and updates to satisfy a WHERE clause in the view.

TRANSACTIONS :This is a group of queries that must be executed together as a unit. If they can’t all execute without interruption, then none of them can. fullfill ACID

START TRANSACTION :is used to tell the RDBMS to begin a transaction. Nothing is permament until COMMIT is issued. The transaction will continue until it is committed or a ROLLBACK command is issued, which returns the database to the state it was prior to the START TRANSACTION.

Chapter  12  security

CREATE USER :Used by some RDBMSs to let you create a user and give them a password

GRANT: Lets you control exactly what users can do to tables and columns based on the privileges you give them.

REVOKE: Use this statement to remove privileges from a user.

WITH GRANT OPTION :Allows users to give other users the same privileges they have

ROLE:A role is a group of privileges. Roles let you group together specific privileges and assign them to more than one user.

WITH ADMIN OPTION :Allows anyone with a role to grant that role to anyone else

相關文章
相關標籤/搜索