1.MySQL 基本操作
1.1 数据库的基本操作
1.1.1. 创建数据库
create database 数据库名 character set utf-8;
例如:
create database mydb character set utf-8;
1.1.2. 删除数据库
drop database 数据库名;
例如:
drop database mydb;
1.1.3. 修改数据库
alter database 数据库名 character set utf-8;
例如:
alter database mydb character set utf-8;
1.2 数据表的基本操作
1.2.1. 创建数据表
create table 表名(
字段名1 数据类型,
字段名2 数据类型,
字段名3 数据类型,
......
);
例如:
create table Student(
id int(11),
stuname varchar(255),
sex char(20),
age int(11),
address varchar(255),
);
1.2.2. 查看表结构
1.用describe语句查看表定义
describe 表名;
或
desc 表名;
例如:
describe Student;
或
desc Student;
2.用"show create table"语句查看表详细定义
show create table 表名;
例如:
show create table Student;
1.2.3. 修改数据表
1.修改表名
alter table 旧表名 rename 新表名;
例如:
alter table Student rename Stu;
2.修改字段
alter table 表名 modify 字段名 数据类型;
例如:
alter table Student modify sex varchar(255);
alter table 表名 change 旧字段名 新字段名 旧数据类型/新数据类型;
例如:
alter table Student change stuname name varchar(255);
3.添加字段
alter table 表名 add 字段名 数据类型;
例如:
在Student表的最后一个位置添加字段为birthday,数据类型为datetime
alter table Student add birthday datetime;
alter table 表名 add 字段名 数据类型 first;
例如:
在Student表的第一个位置添加字段为birthday,数据类型为datetime
alter table Student add birthday datetime;
alter table 表名 add 字段名 数据类型 after 字段名;
例如:
在Student表的age字段后添加字段为birthday,数据类型为datetime
alter table Student add birthday datetime after age;
4.删除字段
alter table 表名 drop 字段名