小组分享

操作数据库

create database 数据库名称 charset utf8;
命名规范:
可以由字母、数字、下划线、@、$
区分大小写
唯一性
不能使用关键字如 create select
不能单独使用数字
最长128位

drop database 数据库名称;

删除再添加
如果数据库中有数据的话,直接drop会导致数据库中的数据丢失
在线上环境,不能直接删除数据,在删除之前,需要进行备份
alter database db1 charset utf8;

show databases;有哪些数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| f                  |
| feng               |
| mysql              |
| performance_schema |
| test               |
+--------------------+

show create database 数据库名;
+----------+-----------------------------------------------------------------+
| Database | Create Database                                                 |
+----------+-----------------------------------------------------------------+
| feng     | CREATE DATABASE `feng` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-----------------------------------------------------------------+

select database(); # 查看当前数据库


操作表

create table 表名(
	字段名 列类型 【约束条件】,#记住加逗号
    字段名 列类型 【约束条件】,#记住加逗号
    字段名 列类型 【约束条件】 #最后一行不加逗号
    )charset=utf8;#加分号

列约束:

auto_increment:自增1
primary key: 主键索引,加快查询速度,列的值不能重复
not null:标识该字段不能为空
default :为该字段设置默认值
unsigned:取值范围为正数

drop table 表名;# 线上禁用
drop table t9;

1.修改表名

alter table 旧表名 rename 新表名;

alter table t8 rename t88;

2.增加字段

#添加的列永远是添加在最后一列之后
alter table 表名
add 字段名 列类型 【约束条件】,
add 字段名 列类型 【约束条件】;

alter table t88 
add name varchar(32) not null default '';

#添加的列添加在第一列
alter table 表名
add 字段名 列类型 【约束条件】 first;

#添加的列添加在某一列下面
alter table 表名
add 字段名 列类型 【约束条件】 after 字段名;

3.删除字段

alter table 表名 drop 字段名;

alter table t88 drop name4;

4.修改字段

alter table 表名 modify 字段名 数据类型 【约束条件】;

alter table t88 modify name2 char(20);

alter table 表名 change 旧字段名 新字段名 新数据类型 【约束条件】;

alter table t88 change name2 name22 varchar(32) not null default '';

show tables;
show create table t88;
原文地址:https://www.cnblogs.com/zqfzqf/p/11779010.html