SQL MySQL
SQL
结构化查询语言(英语:Structural Query Language,缩写:SQL),是一种特殊目的之编程语言,用于数据库中的标准数据查询语言.
各种通行的数据库系统在其实践过程中都对SQL规范作了某些编改和扩充。所以,实际上不同数据库系统之间的SQL不能完全相互通用。
SQL keywords are NOT case sensitive(对大小写不敏感): select is the same as SELECT
What Can SQL do?
- SQL can execute queries(查询) against a database
- SQL can retrieve(获取) data from a database
- SQL can insert(插入) records in a database
- SQL can update(改动更新) records in a database
- SQL can delete(删除) records from a database
- SQL can create new databases(生成新的数据库)
- SQL can create new tables in a database(生成新的数据表)
- SQL can create stored procedures in a database
- SQL can create views in a database
- SQL can set permissions on tables, procedures, and views
Some of The Most Important SQL Commands
- SELECT - extracts data from a database
- UPDATE - updates data in a database
- DELETE - deletes data from a database
- INSERT INTO - inserts new data into a database
- CREATE DATABASE - creates a new database (创建数据库)
- ALTER DATABASE - modifies a database
- CREATE TABLE - creates a new table (创建数据表)
- ALTER TABLE - modifies a table
- DROP TABLE - deletes a table
- CREATE INDEX - creates an index (search key)
- DROP INDEX - deletes an index
SQL 基本用法
SQL SELECT
SELECT column_name,column_name
FROM table_name;
and
SELECT * FROM table_name;
SQL WHERE Clause
The WHERE clause is used to filter records.
SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;
SQL AND & OR Operators
The AND & OR operators are used to filter records based on more than one condition.
example:
SELECT * FROM Customers
WHERE Country='Germany'
AND (City='Berlin' OR City='München');
SQL ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set(给结果集排序) by one or more columns.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in a descending order, you can use the DESC keyword.
SQL INSERT INTO Statement
The INSERT INTO statement is used to insert new records in a table.(数据表中插入几条新的数据)
1 The first form does not specify the column names where the data will be inserted, only their values:
INSERT INTO table_name
VALUES (value1,value2,value3,...);
2 The second form specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
SQL UPDATE Statement
The UPDATE statement is used to update existing records in a table.
SQL UPDATE Syntax
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!
SQL DELETE Statement
The DELETE statement is used to delete rows in a table.
SQL DELETE Syntax
DELETE FROM table_name
WHERE some_column=some_value;
The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!
与update一样,需要注意where,否则影响就是整个数据表
SQL SELECT TOP Clause
The SELECT TOP clause can be very useful on large tables with thousands of records. Returning a large number of records can impact on performance.(对性能产生影响)
SQL LIKE Operator
The LIKE operator is used to search for a specified pattern in a column.
SQL LIKE Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
MySQL
MySQL is an open-source relational database management system (RDBMS).
为多种編程语言提供了API。这些編程语言包括C、C++、C#、VB.NET、Delphi、Eiffel、Java、Perl、PHP、Python、Ruby和Tcl等。
PHP支持哪些数据库
PHP通过安装相应的扩展来实现数据库操作,现代应用程序的设计离不开数据库的应用,当前主流的数据库有MsSQL,MySQL,Sybase,Db2,Oracle,PostgreSQL,Access等.
数据库扩展
PHP中一个数据库可能有一个或者多个扩展,其中既有官方的,也有第三方提供的。
像Mysql常用的扩展有原生的mysql库,也可以使用增强版的mysqli扩展,还可以使用PDO进行连接与操作。
mysql扩展进行数据库连接的方法:
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password');
mysqli扩展:
$link = mysqli_connect('mysql_host', 'mysql_user', 'mysql_password');
PDO扩展
$dsn = 'mysql:dbname=testdb;host=127.0.0.1'; $user = 'dbuser'; $password = 'dbpass'; $dbh = new PDO($dsn, $user, $password);
连接MySQL数据库
通常我们使用mysql_connect函数进行数据库连接
当连接成功以后,我们需要选择一个操作的数据库,通过mysql_select_db函数来选择数据库。
关闭数据库
mysql_close()
选择数据库
mysql_select_db
执行SQL语句
比如插入 insert table_name( ) values ()
为了让 PHP 执行SQL语句,我们必须使用 mysql_query() 函数。该函数用于向 MySQL 连接发送查询或命令。
mysql_query(query,connection)
在数据库建立连接以后就可以进行查询,采用mysql_query加sql语句的形式向数据库发送查询指令。
mysql_error() 函数返回上一个 MySQL 操作产生的文本错误信息
mysql_insert_id() 函数返回上一步 INSERT 操作产生的 ID。
4个fetch 语句
mysql_query('set names utf8')
设置数据库字符集为utf8
$query = mysql_query('select * from test')
当mysql_query语句中的是select语句时,如果执行成功,返回的是资源标识符。
mysql_fetch_row()
每执行一次,都从资源也就是结果集里返回第一条数据,以数组的形式返回。
返回的数组是一个一维索引数组,下标值与数据库里的字段排序对应。
mysql_fetch_array()
mysql_fetch_array() 函数从结果集中取得一行作为关联数组,或数字数组,或二者兼有
默认状态下取一条数据产生一个索引数组和一个关联数组。
mysql_fetch_array(data,array_type)
array_type
- MYSQL_ASSOC - 关联数组
- MYSQL_NUM - 数字数组
- MYSQL_BOTH - 默认。同时产生关联和数字数组
mysql_fetch_assoc()
与上一个函数mysql_fetch_array(data,MYSQL_ASSOC) 效果一样
mysql_fetch_object()
返回的是一个对象
mysql_num_rows()
mysql_num_rows(data) 函数返回结果集中行的数目。
mysql_result()
mysql_result() 函数返回结果集中一个字段的值。
mysql_result(data,row,field)
data | 必需。规定要使用的结果标识符。该标识符是 mysql_query() 函数返回的。 |
row | 必需。规定行号。行号从 0 开始。 |
field |
可选。规定获取哪个字段。可以是字段偏移值,字段名或 table.fieldname。 如果该参数未规定,则该函数从指定的行获取第一个字段。 |
Order By 关键词
SELECT column_name(s) FROM table_name ORDER BY column_name
ORDER BY 关键词用于对记录集中的数据进行排序