库的管理

常用命令

#创建库
create database if not exists 库名 【 character set 字符集名】;
create database if not exists books #查看当前所有数据库
show databases #使用某一个库
use 库名
use employees #修改库字符集
alter database 库名 character set 字符集名;
ALTER DATABASE books CHARACTER SET gbk; #删库(慎用)
drop database 【if exists】 库名;
drop database if exists books #查看某个数据库中的所有表
select table_name from information_schema.tables where table_schema='【myemployees】';

注意:

  • 若是修改库名则需要关闭数据库服务后在文件夹里修改
  • 保持字段名和类型的一致性,在命名字段并为其指定数据类型的时候一定要保证一致性。假如数据类型在一个表里是整数,那在另一个表里可就别变成字符型了

命名规则

  • 数据库名不得超过30个字符,变量名限制为29个

  • 必须只能包含 A–Z, a–z, 0–9, _共63个字符

  • 字符间不能留空格

  • 不能重名

  • 不能有保留字、数据库系统或常用方法冲突

表的管理

一、创建表 ★

create table 【if not exists】 表名(

字段名 字段类型 【约束】,

字段名 字段类型 【约束】,

。。。

字段名 字段类型 【约束】

)

创建表

CREATE TABLE book(
id INT, #编号
bName VARCHAR(20), #图书名
price DOUBLE, #价格
authorId INT, #作者编号
publishDate DATETIME #出版日期
);

修改表

alter table 表名 add|drop|modify|change column 列名 【列类型 约束】;

#1.添加列 ,first、after表示所添加列的位置
alter table 表名 add column 列名 类型 【first|after 字段名】; #2.修改列的类型或约束
alter table 表名 modify column 列名 新类型 【新约束】; #3.修改列名
alter table 表名 change 【column】 旧列名 新列名 类型; #4.删除列
alter table 表名 drop column 列名; #5.修改表名
alter table 表名 rename 【to】 新表名;

删除表

drop table【if exists】 表名;

复制表

#1.复制表的结构
create table 表名 like 旧表; #2.复制表的结构+数据
create table 表名 #注意此处不能加结束符分号;
select 查询列表 from 旧表【where 筛选】; #3.复制表的部分结构
create table 表名
select 列名1,列名2,...
from 原始表
where 0/0 //若想连数据也复制则为1,若只复制结构则为0
  • 可以跨库复制表,表名写成库名.表名

查看表

#1.查看表的结构
desc 表名 #2.查看表的数据:
select * from 表名

最新文章

  1. AbpKernelModule
  2. [转]10个有关RESTful API良好设计的最佳实践
  3. Execl 使用技巧
  4. 将一列包含多个ID拆分多行
  5. Hadoop入门进阶课程8--Hive介绍和安装部署
  6. linux极点五笔无法输入词组_ibus设置
  7. 在Spring中使用cache(EhCache的对象缓存和页面缓存)
  8. js 中cookie 使用
  9. Vulkan Tutorial 27 combined image sampler
  10. 2017 ECJTU ACM程序设计竞赛 矩阵快速幂+二分
  11. servlet之隐藏域
  12. Java NIO 概览
  13. [原创][Synth 8-2543] port connections cannot be mixed ordered and named ["*_Top.v":1151]
  14. [Swift]LeetCode282. 给表达式添加运算符 | Expression Add Operators
  15. 爬虫2 urllib3用法
  16. 优化Django ORM中的性能问题(含prefetch_related 和 select_related)
  17. Java之集合(二十一)LinkedTransferQueue
  18. 铁乐学python_Day42_线程-信号量事件条件
  19. Team Member Introduction and Division of Work
  20. windows上memecache添加多个端口命令

热门文章

  1. windows安装python2.7、python3.7和pycharm
  2. requests接口自动化-数据库参数化
  3. [转载]解决虚拟机中Centos7出现错误:Failed to start LSB: Bring up/down networking
  4. CF25E-Test【AC自动机,bfs】
  5. P2179-[NOI2012]骑行川藏【导数,二分】
  6. 做毕设的tricks
  7. Incorrect datetime value: 时间添加失败原因
  8. 带你读Paper丨分析ViT尚存问题和相对应的解决方案
  9. NER为什么那么难
  10. 教你 4 步搭建弹性可扩展的 WebAPI