MySQL 基本语句

Posted by Surflyan on 2017-09-03

1. 连接数据库

# 启动数据库
1. net start mysql

# 连接数据库
2. mysql -u root -p

# 关闭数据库
3. net stop mysql

2. 数据库操作

# 创建数据库
1. creat database db_name;

# 删除数据库
2. mysql -u root -p drop db_name

# 选择数据库
3. use db_name;

# 创建数据表
4. create table table_name
( id BIGINT auto_increment,
  title varchar(20) not null,
  primary key(id)
  ) engine = InnoDB default charset = utf8;

  # 删除数据表
  5. drop table table_name;

3. CRUD

# 插入数据(完整可省略field)
1. insert into table_name ( field1, field2,...fieldN ) values ( value1, value2,...valueN );

# 查询数据
2. select * from table_name where (conditon);

# 更新数据
3. update table_name set field1 = value1, field2 = value2 where (condition);

# 删除数据
4. delete from table_name where (condition);

4. Alter 命令

# 删除字段
1. alter table table_name drop i;

# 增加字段(默认末尾)
2. alter talbe table_name add i int;

# 增加字段指定位置(first,after)
3. alter table table_name add i int after c;

# 修改字段类型
4. alter table table_name modify i
char(10);
5. alter table table_name change i j bigint;

# 修改字段默认值
6. alter table table_name modify j bigint not null default 100;

# 修改表名
7. alter table table_name rename to new_table_name;