一、数据库操作
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec) mysql>
mysql> CREATE DATABASE test; ERROR 1007 (HY000): Can‘t create database ‘test‘; database exists mysql>
mysql> CREATE DATABASE IF NOT EXISTS test; Query OK, 1 row affected, 1 warning (0.00 sec) mysql>
mysql> SHOW WARNINGS; +-------+------+-----------------------------------------------+ | Level | Code | Message | +-------+------+-----------------------------------------------+ | Note | 1007 | Can‘t create database ‘test‘; database exists | +-------+------+-----------------------------------------------+ 1 row in set (0.00 sec) mysql>
mysql> CREATE DATABASE mydb; Query OK, 1 row affected (0.00 sec) mysql> SHOW DATABASES;; +--------------------+ | Database | +--------------------+ | information_schema | | mydb | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) ERROR: No query specified mysql>
mysql> DROP DATABASE testdb; ERROR 1008 (HY000): Can‘t drop database ‘testdb‘; database doesn‘t exist
mysql> DROP DATABASE IF EXISTS testdb; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> SHOW WARNINGS; +-------+------+------------------------------------------------------+ | Level | Code | Message | +-------+------+------------------------------------------------------+ | Note | 1008 | Can‘t drop database ‘testdb‘; database doesn‘t exist | +-------+------+------------------------------------------------------+ 1 row in set (0.00 sec) mysql>
ALTER {DATABASE | SCHEMA} [db_name] alter_specification ... ALTER {DATABASE | SCHEMA} db_name UPGRADE DATA DIRECTORY NAME alter_specification: [DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name (create_definition,......) [table_options] (create_definition,......)
字段的定义:字段名、类型和类型修饰符
键、约束或索引
PRIMARY KEY, UNIQUE KEY, FROEIGN KEY, CHECK {INDEX|KEY}
ENGINE [=] engine_name AUTO_INCREMENT [=] value [DEFAULT] COMMONT [=] DELAY_KEY_WRITE [=] {0 | 1} 对提高性能有所帮助 INDEX_DIRECTORY [=] ‘PATH‘ 索引位置 DATA DIRECTORY [=] ‘PATH‘ ROW_FORMAT
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name (create_definition,......) [table_options] select_statement
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name {LIKE old_tbl_name | (LIKE old_tbl_name)}
DROP [TEMPORARY] TABLE [IF EXTSTS] tbl_name [,tbl_name...]
ALTER TABLE tbl_name [alter_specification [, alter_specification]...]
mysql>RENAME TABLE old_tbl_name TO new_tbl_name
1.{INSERT|REPLACE} INTO tb_name [(col1, col2,....)] {VAULES|VALUE}(val, val2,...) [,(val21, val22,....),.....] 2.{INSERT|REPLACE} INTO tb_naem SET col1=val1, clo2=val2,..... 3.INSERT INTO tb_name SELECT clause
UPDATE [LOW_PRIORITY] [IGNORE] table_reference SET col_name1=val1 [, col_name2=val2] ... [WHERE where_condition] [ORDER BY ...] [LIMIT row_count]
--safe-updates DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name [WHERE where_condition] [ORDER BY ...] [LIMIT row_count]
SELECT常用结合使用方式
SELECT ... SELECT ... FROM ... ORDER BY ... SELECT ... FROM ... GROUP BY ... HAVING ... SELECT ... FROM ... WHERE ... SELECT ... FROM ... WHERE ... GROUP BY ... LIMIT SELECT ... FROM ... HAVING
交叉联结 (笛卡尔乘积) 避免使用
内联结(Inner Jion):通过匹配两个表之间的等值关系,建立的联结
例如:
SELECT * FROM student,cources WHERE students.CID1=cources.CID;
外联结(Outer Jion) 两个表之间不是等值关系,一个表中可能没有值。
左外 SELECT...FROM...LEFT JION...ON...
以左表为基准,右表没有就显示为空。 (不需要where)
例如:
SELECT name,cname FROM student LEFT JION courses ON student.CID1=courses.CID
右外 SELECT...FROM...RIGHT JION...ON... 以右表为基准,左表可能为空。(不需要where)
完全外
自联结 (SELF JION): 同一个表的两个字段建立联系。
例如:
SELECT s1.name,s2.name FROM students AS s1,students AS s2 WHERE s1.TID=s2.SID
联合(UNION):将两个字段的查询结果联结在一起(也就是将两个查询语句用UNION联合起来)
1.用于比较表达式中的子查询 子查询的返回值只能有一个 2.用于EXISTS中的子查询 判断存在与否,可以返回多个值 3.用于IN中的子查询 判断存在指定列表中,可以返回多个值
SELECT alias.col,....from (SELECT clause) AS alias WHERE condition
MySQL的联合查询:
创建视图:CREATE VIEW view_name AS select_statemen; select_statement表示SELECT语句,将SELECT语句查询得到的表建立为一个视图。
删除视图:DROP VIEW view_name
显示创建view的命令:SHOW CREATE VIEW view_name
虚表中插入数据时查看基表是否允许,但mysql中不允许向视图中插入数据。
对视图的查询就是一种子查询,在实际操作中应避免子查询。
ID Name Age Gender Course 1 Ling Huchong 24 Male Hamogong 2 Huang Rong 19 Female Chilian Shenzhang 3 Lu Wushaung 18 Female Jiuyang Shenggong 4 Zhu Ziliu 52 Male Pixie Jianfa 5 Chen Jialuo 22 Male Xianglong Shiba Zhang 6 Ou Yangfeng 70 Male Shenxiang Bannuo Gong操作步骤:
#创建库 mysql> CREATE DATABASE mydb Query OK, 1 row affected (0.00 sec) mysql> USE mydb Database changed #创建表 mysql> CREATE TABLE students ( -> ID INT AUTO_INCREMENT UNIQUE KEY, -> Name CHAR(30), -> Age TINYINT UNSIGNED NOT NULL, -> GENDER ENUM(‘Male‘,‘Female‘) NOT NULL, -> Corse VARCHAR(50) NOT NULL); Query OK, 0 rows affected (0.02 sec) #查看表 mysql> SHOW TABLES ; +----------------+ | Tables_in_mydb | +----------------+ | students | +----------------+ 1 row in set (0.00 sec) #查看表结构 mysql> DESC students; +--------+-----------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+-----------------------+------+-----+---------+----------------+ | ID | int(11) | NO | PRI | NULL | auto_increment | | Name | char(30) | YES | | NULL | | | Age | tinyint(3) unsigned | NO | | NULL | | | GENDER | enum(‘Male‘,‘Female‘) | NO | | NULL | | | Corse | varchar(50) | NO | | NULL | | +--------+-----------------------+------+-----+---------+----------------+ 5 rows in set (0.00 sec) #添加表内容 mysql> INSERT INTO students (ID,Name,Age,Gender,Course) VALUES -> (1,‘Ling Huchong‘,24,‘Male‘,‘Hamogong‘), -> (2,‘Huang Rong‘,19,‘Female‘,‘Chilian Shenzhang‘), -> (3,‘Lu Wushaung‘,18,‘Female‘,‘Jiuyang Shenggong‘), -> (4,‘Zhu Ziliu‘,52,‘Male‘,‘Pixie Jianfa‘), -> (5,‘Chen Jialuo‘,22,‘Male‘,‘Xianglong Shiba Zhang‘), -> (6,‘Ou Yangfeng‘,70,‘Male‘,‘Shenxiang Bannuo Gong‘); Query OK, 6 rows affected (0.01 sec) Records: 6 Duplicates: 0 Warnings: 0 #查看表内容: mysql> SELECT * FROM students; +----+--------------+-----+--------+-----------------------+ | ID | Name | Age | GENDER | Course | +----+--------------+-----+--------+-----------------------+ | 1 | Ling Huchong | 24 | Male | Hamogong | | 2 | Huang Rong | 19 | Female | Chilian Shenzhang | | 3 | Lu Wushaung | 18 | Female | Jiuyang Shenggong | | 4 | Zhu Ziliu | 52 | Male | Pixie Jianfa | | 5 | Chen Jialuo | 22 | Male | Xianglong Shiba Zhang | | 6 | Ou Yangfeng | 70 | Male | Shenxiang Bannuo Gong | +----+--------------+-----+--------+-----------------------+ 6 rows in set (0.00 sec) mysql>
mysql> ALTER TABLE students ADD Class INT NULL AFTER Name; Query OK, 6 rows affected (0.02 sec) Records: 6 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM students; +----+--------------+-------+-----+--------+-----------------------+ | ID | Name | Class | Age | GENDER | Course | +----+--------------+-------+-----+--------+-----------------------+ | 1 | Ling Huchong | NULL | 24 | Male | Hamogong | | 2 | Huang Rong | NULL | 19 | Female | Chilian Shenzhang | | 3 | Lu Wushaung | NULL | 18 | Female | Jiuyang Shenggong | | 4 | Zhu Ziliu | NULL | 52 | Male | Pixie Jianfa | | 5 | Chen Jialuo | NULL | 22 | Male | Xianglong Shiba Zhang | | 6 | Ou Yangfeng | NULL | 70 | Male | Shenxiang Bannuo Gong | +----+--------------+-------+-----+--------+-----------------------+ 6 rows in set (0.00 sec) mysql>2、将ID字段名称修改为TID;
mysql> ALTER TABLE students CHANGE ID TID INT AUTO_INCREMENT UNIQUE KEY; Query OK, 6 rows affected (0.02 sec) Records: 6 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM students; +-----+--------------+-------+--------+-----------------------+-----+ | TID | Name | Class | GENDER | Course | Age | +-----+--------------+-------+--------+-----------------------+-----+ | 1 | Ling Huchong | NULL | Male | Hamogong | 24 | | 2 | Huang Rong | NULL | Female | Chilian Shenzhang | 19 | | 3 | Lu Wushaung | NULL | Female | Jiuyang Shenggong | 18 | | 4 | Zhu Ziliu | NULL | Male | Pixie Jianfa | 52 | | 5 | Chen Jialuo | NULL | Male | Xianglong Shiba Zhang | 22 | | 6 | Ou Yangfeng | NULL | Male | Shenxiang Bannuo Gong | 70 | +-----+--------------+-------+--------+-----------------------+-----+ 6 rows in set (0.00 sec) mysql>
mysql> ALTER TABLE students MODIFY Age TINYINT UNSIGNED NOT NULL AFTER Course; Query OK, 6 rows affected (0.02 sec) Records: 6 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM students; +----+--------------+-------+--------+-----------------------+-----+ | ID | Name | Class | GENDER | Course | Age | +----+--------------+-------+--------+-----------------------+-----+ | 1 | Ling Huchong | NULL | Male | Hamogong | 24 | | 2 | Huang Rong | NULL | Female | Chilian Shenzhang | 19 | | 3 | Lu Wushaung | NULL | Female | Jiuyang Shenggong | 18 | | 4 | Zhu Ziliu | NULL | Male | Pixie Jianfa | 52 | | 5 | Chen Jialuo | NULL | Male | Xianglong Shiba Zhang | 22 | | 6 | Ou Yangfeng | NULL | Male | Shenxiang Bannuo Gong | 70 | +----+--------------+-------+--------+-----------------------+-----+ 6 rows in set (0.00 sec) mysql>
本文出自 “一叶知秋” 博客,请务必保留此出处http://maoqiu.blog.51cto.com/8570467/1392818
Mysql学习之基础知识总结(二),布布扣,bubuko.com
原文:http://maoqiu.blog.51cto.com/8570467/1392818