SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database. According to ANSI, it is the standard language for relational database management systems. SQL statements are used to perform tasks such as update data on a database, or retrieve data from a database. Some common relational database management systems that use SQL are: Oracle, Sybase, Microsoft SQL Server, Access, Ingres, etc. Although most database systems use SQL, most of them also have their own additional proprietary extensions that are usually only used on their system. However, the standard SQL commands such as "Select", "Insert", "Update", "Delete", "Create", and "Drop" can be used to accomplish almost everything that one needs to do with a database.
SQL代表结构化查询语言。SQL用于数据库会话。按照ANSI的标准,它是关系数据库管理系统的标准语言。SQL 语句用于更新或者检索数据库中的数据,使用SQL的常见的关系数据库管理系统有:Oracle、Sybase、Microsoft SQL Server、Access、Ingres 等等。尽管大多数的数据库系统都使用SQL,但是他们大多都有针对自己系统的额外扩展。然而,标准的 SQL 命令,例如“Select”、“Insert”、“Update”, “Delete”、“Create”和 “Drop”,可以用来完成数据库中所需的任何任务。
A relational database system contains one or more objects called tables. The data or information for the database are stored in these tables. Tables are uniquely identified by their names and are comprised of columns and rows. Columns contain the column name, data type, and any other attributes for the column. Rows contain the records or data for the columns. Here is a sample table called "weather".City, state, high, and low are the columns. The rows contain the data for this table:
一个关系数据库系统包含一个或者多个被称为“表”的对象。数据库中的数据或信息存储在这些表中。表由表名称来惟一标记,由列和行组成。列包含列名、数据类型和其他的全部列的属性。行包含列的数据或记录。下面是一个“天气”表的例子。
The select statement is used to query the database and retrieve selected data that match the criteria that you specify. Here is the format of a simple select statement:
选择讲句用来查询数据,检索根据你设定的条件所检索到的数据。下面是一个选择语句的格式:
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.
关键字后而的列名决定了哪些列将在结果中出现。你可以根据需要选择任意的列名,或者使用“*”来选择全部列。
The table name that follows the keyword from specifies the table that will be queried to retrieve the desired results.
关键字from后面的表名确定了为得到结果所要检索的表。
The where clause (optional) specifies which data values or rows will be returned or displayed, based on the criteria described after the keyword where.
关键字where后面的从句基于标准的描述,where从句(可选项)限定了哪些数据或者行将会被查到或者显示。
The create table statement is used to create a new table. Here is the format of a simple create table statement:
生成表的语句用来生成一个新表,这是一个简单的生成表语句的格式:create table "tablename"
Note: You may have as many columns as you'd like, and the constraints are optional.
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 ";".
生成一个新表时,输入关键字create table,后而输入表名,接着是左括弧、第一列的列名、数据类型、其他可选的约束条件、右括弧。在表开始的地方使用左括弧,最后一列定义结束的地方使用右括弧很重要。必须用逗号隔开每一个列的定义。所有SQL语句结束时都有个分号。
The table and column names must start with a letter and can be followed by letters, numbers or underscores ê not to exceed a total of 30 characters in length. Do not use any SQL reserved keywords as names for tables or column names (such as "select", "create", "insert", etc).
表名、列名的第一个字符必须用字母,后面可以用字母、数字或者下划线——长度不要超过30个字符。不要用SQL的保留字(例如select、create、insert等等)作表名和列名。
Data types specify what the type of data can be for that particular column. If a column called "Last_ Name", is to be used to hold names, then that particular column should have a "varchar" (variable-length character) data type.
数据类型定义了指定列的数据的类型。如果一个列名为“姓用来存储名字,那这个列的数据类型为varchar (可变长度的字符)。
Note: Here are the most common data types:
char(size) Fixed-length character string. Size is specified in parenthesis. Max 255 bytes.
char(size) 定长字符串,括号里面设定长度,最长255字节
varchar(size) Variable-length character string. Max size is specified in parenthesis.
varchar(size) 不定长字符串,括弧里面限定了最大的长度
number(size) Number value with a max number of column digits specified in parenthesis.
number(size) 数位(括弧里面限定最大位数)
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.
number(size,d) 数值(括弧里的size限定最大总位数,限定小数点以后的位数)
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.
什么是约束条件?表建立以后,不同的列通常有相关的约束条件。列的相关约束条件是一个规则,所有输入本列的数据必须通守这一规则。例如,unique约束条件限定了这一列中不能有两个相同列值的记录。它们必须是惟一的。另外两个常见的约束条件是no null 和primary key,not null限制了列不能有空数据,primary key约束为每一条记录(或者行)定义了一个惟一的标志。
The insert statement is used to insert or add a row of data into the table.
插入语句用来在表中插入或增加一行数据。
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后面依次是表名、左括弧、用逗号分开的不同列名、右括弧、关键字values,括弧里而的数值列表。输入的数值将按照指定的列名存储到一行中。字符串要用单引号,数字不用。
In the example below, the column name first will match up with the value 'Luke', and the column name state will match up with the value 'Georgia'.
在下面的例子中,列名为first的将被输入数值luke,列state将被输入Georgia。
例子:
Example:
Note: All strings should be enclosed between single quotes: 'string'
注意:所有的字符串都要放在引号之间:“string”。
Updating Records
The update statement is used to update or change records that match a specified criteria. This is accomplished by carefully constructing a where clause.
更新语句用来按照某一标准更新或修改记录。这需要通过仔细构造一个where从句来实现。
Examples:
Deleting Records
The delete statement is used to delete records or rows from the table.
Examples:
To delete an entire record/row from a table, enter "delete from" followed by the table name, followed by the where clause which contains the conditions to delete. If you leave off the where clause, all records will be deleted.
要从表中删除一条完整的记录或行,输入“delete from"。后面接着是表名,包含删除条件的where从句。如果没有where从句,表中所备记录都将被删掉。
The drop table command is used to delete a table and all rows in the table.
删除表的命令用来删除表和表中的全部记录。
To delete an entire table including all of its rows, issue the drop table command followed by the table name. Drop table is different from deleting all of the records in the table. Deleting all of the records in the table leaves the table including column and constraint information. Dropping the table removes the table definition as well as all of its rows.
要删除一个完整的表和里面所有的记录,输入drop table命令,后面接着是表名。刪除表与删除所有记录并不相同,删掉所存记录以后还剩下包含列和约束条件的表。刪除表则将记录和表的定义一起删掉了。
Example: