PHP 和 MySQL 编程/基本查询
外观
以下是一些基本查询,可以帮助你入门。有关更详细的信息,请查看 MySQL 网站上的在线文档 https://dev.mysqlserver.cn/doc/refman/5.1/en/index.html 。
SELECT 查询是用于从数据库中检索信息的标准方法。SELECT 查询的语法如下:
mysql> SELECT field1, field2, ... FROM table_name [WHERE condition1 AND condition2 ... [ORDER BY field1, field2, etc...] [LIMIT number]];
示例
mysql> SELECT * FROM books; mysql> SELECT Title, Author FROM books WHERE Year > 2001 AND Year < 2006 ORDER BY Author ASC LIMIT 100;
INSERT 语句用于将数据插入数据库的特定表中。语法如下:
mysql> INSERT INTO table_name (field1, field2, etc...) VALUES ('val1', 'val2', etc...);
示例
mysql> INSERT INTO books (ISBN, Title, Author, Year) VALUES ('1234567890', 'Programming PHP and MySQL', 'Poisson, R.W', 2006);
DELETE 语句用于从表中删除行。语法如下:
mysql> DELETE FROM table_name [WHERE condition1, condition2 etc...];
示例
mysql> DELETE FROM books WHERE ISBN = '1234567890';
ALTER 用于修改表的结构。ALTER 的语法如下:
ALTER [IGNORE] TABLE tbl_name alter_specification [, alter_specification] ... alter_specification: ADD [COLUMN] column_definition [FIRST | AFTER col_name ] | ADD [COLUMN] (column_definition,...) | ADD INDEX [index_name] [index_type] (index_col_name,...) | ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...) | ADD [CONSTRAINT [symbol]] UNIQUE [INDEX] [index_name] [index_type] (index_col_name,...) | ADD FULLTEXT [INDEX] [index_name] (index_col_name,...) [WITH PARSER parser_name] | ADD SPATIAL [INDEX] [index_name] (index_col_name,...) | ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,...) [reference_definition] | ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT} | CHANGE [COLUMN] old_col_name column_definition [FIRST|AFTER col_name] | MODIFY [COLUMN] column_definition [FIRST | AFTER col_name] | DROP [COLUMN] col_name | DROP PRIMARY KEY | DROP INDEX index_name | DROP FOREIGN KEY fk_symbol | DISABLE KEYS | ENABLE KEYS | RENAME [TO] new_tbl_name | ORDER BY col_name | CONVERT TO CHARACTER SET charset_name [COLLATE collation_name] | [DEFAULT] CHARACTER SET charset_name [COLLATE collation_name] | DISCARD TABLESPACE | IMPORT TABLESPACE | table_options | partition_options | ADD PARTITION (partition_definition) | DROP PARTITION partition_names | COALESCE PARTITION number | REORGANIZE PARTITION partition_names INTO (partition_definitions) | ANALYZE PARTITION partition_names | CHECK PARTITION partition_names | OPTIMIZE PARTITION partition_names | REBUILD PARTITION partition_names | REPAIR PARTITION partition_names | REMOVE PARTITIONING
DROP 用于完全删除表或数据库。语法如下:
mysql> DROP table_name;
TRUNCATE 用于删除数据,但不删除表的结构。语法如下:
mysql> TRUNCATE table_name;