Comment: Collection of> Transactions: NO
XA: NO
Savepoints: NO
*************************** 3. row ***************************
Engine: BLACKHOLE
Support: YES
Comment: /dev/null storage engine (anything you write to it disappears)
Transactions: NO
XA: NO
Savepoints: NO
*************************** 4. row ***************************
Engine: CSV
Support: YES
Comment: CSV storage engine
Transactions: NO
XA: NO
Savepoints: NO
*************************** 5. row ***************************
Engine: MEMORY
Support: YES
Comment: Hash based, stored in memory, useful for temporary tables
Transactions: NO
XA: NO
Savepoints: NO
*************************** 6. row ***************************
Engine: FEDERATED
Support: NO
Comment: Federated MySQL storage engine
Transactions: NULL
XA: NULL
Savepoints: NULL
*************************** 7. row ***************************
Engine: ARCHIVE
Support: YES
Comment: Archive storage engine
Transactions: NO
XA: NO
Savepoints: NO
*************************** 8. row ***************************
Engine: MyISAM (默认引擎,不支持事务,表级别锁)
Support: DEFAULT
Comment: Default engine as of MySQL 3.23 with great performance
Transactions: NO
XA: NO
Savepoints: NO
8 rows in set (0.00 sec)
改变表的类型(表所使用的存储引擎的类型):
mysql>> 查看表的存储引擎:
mysql> use information_schema
Database changed
mysql> SELECT TABLE_SCHEMA,TABLE_NAME,TABLE_TYPE,ENGINE FROM tables WHERE TABLE_NAME='students2';
SQL语句的熟练应用:
mysql> create database mydb;
mysql> use mydb
mysql> create table stu (
-> name char(6) not null,
-> age tinyint unsigned not null,
-> gender char(1),
-> primary key (name,gender)
-> );
mysql>>
mysql>> mysql> CREATE TABLE course(
->> -> course VARCHAR(255) NOT NULL DEFAULT 'Chinese',
-> startdate DATE NOT NULL DEFAULT '2011-01-01'
-> );
定义外键,先改存储引擎:
mysql>>
mysql>>
mysql>> PS:如果创建一个已经存在的表会报错。使用 "if not exists" 关键字,在创建表的时候如果已经存在则不创建。
mysql> show warnings
显示警告信息的内容
checksum 1 通过已经存在的校验码来检查表中的数据是否有错误。
comment 添加注释信息
mysql>create table if not exists stu2 (......) comment 'Students'info.' ;
delay_key_write 跟索引相关联,延迟索引更新,可以提高数据库性能。
根据已经存在的表来创建一个新表:
通过选取一些字段来创建一个新表
mysql> create table course2 select * from course where>
创建一个跟原来表结构一摸一样的表:
mysql> create table course3 like course;
向表中添加数据:
mysql> insert into course(course,startdate) values ('Maths','2011-01-01');
批量插入:
mysql> insert into course(course) values ('English'),('Computer'),('Music');
清空表:
mysql> truncate table course;
添加唯一键约束:
mysql>>
通过select来添加数据:
mysql> insert into course3 select * from course;
使用set为某个字段设值:
mysql> insert into course set course='PE';
修改表中数据:
mysql> update course set>
删除一行数据:
mysql> delete from course where> 查询:
使用别名:
mysql> select course as COURSE from course;
MySQL操作符:
mysql> select * from course where>
mysql> select * from course where> mysql> select * from course where course like 'M%';
mysql> select * from course where course like 'M_sic';
去除重复行:
mysql> select distinct startdate from course;
排序,默认升序:
mysql> select * from course order by>
mysql> select * from course order by>
分组:
mysql> select * from course group by startdate;
对分组后的结果再进行过滤:
mysql> select startdate from course group by startdate having count(*)>1;
对结果集做条目限制:
mysql> select * from course order by>
mysql> select * from course order by>
多表查询:
mysql> (select * from course3 order by>
两个表:
内连接:即等值连接,根据两个表的对应列值相等的原则进行连接。连接条件的形式:“主键=外键”,即一个表的主键值与另一个表的外键值相等的原则进行连接。
mysql> select * from stu,course where stu.course=course.id;
外连接:分为左外连接和右外连接。外连接不仅包含满足条件的行,还包括其中某个表中不满足连接条件的行。
mysql> select * from stu left join course on stu.course=course.id;
mysql> select * from stu right join course on stu.course=course.id;
mysql> select * from stu right join course on stu.course=course.id where stu.gender='m' or course.id=4;
mysql> select s1.name as HOME ,s2.name as CUSTOMER from stu as s1 inner join stu as s2 where s1.name s2.name;
子查询:
非相关子查询:外查询的查询结果依赖于子查询的结果。
mysql> select * from stu where course =(select>
mysql> select s1.name from (select * from stu where gender='m') as s1 where s1.course=2;
mysql> select course from course where>
相关子查询:子查询的执行依赖于外查询。执行从外查询开始,只有外查询把值传给内查询后,内查询才能执行。
mysql> select name from stu where exists (select * from stu where age>20);
//** exists 条件为真,就将外查询的结果输出出来。
PS:EXISTS 的子查询只测试子查询的结果集是否为空,因此在子查询中指定列名是没有意义的,所以在有EXISTS的子查询中,其列名序列通常都用“*”表示。
EXISTS后的子查询中必须加入外查询所使用的表与内查询所用的表之间的连接条件。
mysql> select * from stu where exists (select * from course where course.id=stu.course and course.course='Maths');
视图:虚表
mysql> create view students as select * from stu;
视图可以更新。
mysql> create view test as select name,course from stu where course is not null with check option;
索引:
PS:MySQL将存储数据的文件划分为页面。
为stu创建索引:
mysql> create index stu_age_index on stu(age) using btree;
mysql> show create table stu;