create database 库名 [charset utf8]; # 创建库,charset指定编码格式.[]中为可选项
CREATE DATABASE User CHARSET UTF8; # 创建User
注意:创建数据库时指定字符编码,不能写utf-8
drop database 库名; # 删除库
DROP DATABASE User;
alter database 库名 charset 新编码方式; # 修改库的编码
ALTER DATABASE User charset gbk;
select database(); # 查看当前所在的库
show databases; # 显示所有的库
show create database 库名; # 查看该库的建库语句
要对库下的某个表操作时,就必须先进入库
use 库名[;] # 切换库,";"可加可不加
#!/bin/bash
# 假设将sakila数据库名改为new_sakila
# MyISAM直接更改数据库目录下的文件即可
mysql -uroot -p123456 -e 'create database if not exists new_sakila'
list_table=$(mysql -uroot -p123456 -Nse "select table_name from information_schema.TABLES where TABLE_SCHEMA='sakila'")
for table in $list_table
do
mysql -uroot -p123456 -e "rename table sakila.$table to new_sakila.$table"
done
# 这里用到了rename table,改表名的命令,但是如果新表名后面加数据库名,就会将老数据库的表移动到新的数据库
create table 表名(
字段1 字段1类型[(宽度) 约束],
[字段1 字段1类型[(宽度) 约束]],...
)[engine=InnoDB] [charset = utf8]; # engine指定数据库存储引擎,charset指定编码,[]为可选项
create table student(
id int primary key auto_increment,
name char(10),
age int,
birthday datetime
)engine innodb charset utf8; # 竟然发现可以不用写“=”
drop table 表名; # 删除表
drop table student;
rename table 原表名 to 新表名; # 修改表名
alter table 原表名 rename 新表名; # 修改表名
rename table student to s;
alter table s rename student;
--------------------------------------------------------------------------------------------------
alter table 表名 add 字段1 字段1类型[(宽度) 约束] [FIRST|after 字段2]; # 添加表字段,first指定添加到第一个字段,after指定在哪个字段后增加
alter table student add gender char(5); # 默认在后面添加
alter table student add g1 char(5) first; # 添加在第一个字段
alter table student add g2 char(5) after name; # 添加在字段name后面
--------------------------------------------------------------------------------------------------
alter table 表名 drop 字段; # 删除表字段
alter table 表名 modify 字段1 字段1类型[(宽度) 约束]; # 修改表字段类型
alter table student drop g2;
alter table student modify gender1 varchar(10);
show tables; # 查看当前库下有哪些表
show create table 表名; # 查看该表创建语句
desc 表名; # 查看表结构
create table 新表名 select * from 原表名; # 复制表,数据和结构一起拷贝
create table 新表名 select * from 原表名 where 1>5; # 复制表结构,数据不拷贝
create table 新表名 like 原表名; # 复制表结构,数据不拷贝
原文:https://www.cnblogs.com/863652104kai/p/11252997.html