MySQL读书笔记(2)
这本书的第一章介绍了Mysql的下载、安装,由于我只有Windows环境,所以就一路Next的安装配置完成,并没有什么好说的。
第二章介绍了(My)SQL基础,主要讲了一些SQL语法和Mysql在SQL标准上的某些关键字扩展。
昨天主要学习了DDL语句:
- 创建数据库
- create database dbname;
- 显示所有数据库
- show databases;
- 删除数据库
- drop database dbname;
- 选择数据库
- use dbname;
- 显示所有表
- show tables;
- 创建表
- create table tablename ( col_name_1 col_type_1 constraints , col_name_2 colu_type_2 constraints,……,col_name_n col_type_n constraints)
- 查看表定义
- desc tablename;
- 查看更详细的表定义(查看创建表的SQL语句)
- show create table tablename (\G);
- \G:使得记录能够按照字段竖着排列,对于内容比较长的记录更容易显示
- 删除表
- drop table tablename;
- 修改表
- 修改表类型
- alter table tablename modify [column] col_definition [first|after col_name]
- e.g: alter table emp modify ename varchar(20);
- 增加表字段
- alter table tablename add [column] col_definition [first|after col_name]
- e.g: alter table emp add [column] age int(3);
- 删除表字段
- alter table tablename drop [column] col_definition [first|after col_name]
- e.g: alter table emp drop [column] age;
- 注意:change和modify都可以修改表定义,不同的是change后面需要写两次列名,不方便,但是change的有点是可以修改列名称,modify则不能。
- 字段改名
- alter table tablename change[column] old_col_name col_definition [first|after col_name]
- e.g: alter table emp change age age1 int(4);
- 修改字段排列顺序
- 在add/change/modify定义中都有first|after col_name,这个选项用于修改字段在表中的位置,默认add增加的新字段是加在表的最后位置,而change/modify默认不会改变字段位置
- e.g1: alter table emp add birth date after ename;
- e.g2: alter table emp modify age int(3) first; age字段将置于最前面
- 更改表名
- alter table tablename rename [to] new_table_name
- e.g: alter table emp rename emp1;
- 注意:chage/first|after column 这些关键字属于Mysql在标准SQL上的扩展,在其他数据库上不一定适用。
- 修改表类型


最新评论