create database db1 charset utf8;
注:charset utf8(字符编码省略,使用默认的)
例:drop database db1;
首先切换文件夹:
use 数据库名;
use db1;
select database(); #查看当前所在的文件夹
最终的格式:
# create table 表名 (
# 列1 列属性 [是否为null 默认值],
# 列2 列属性 [是否为null 默认值],
# .....
# 列n 列属性 [是否为null 默认值]
# )engine = 存储引擎 charset = 字符集
#
# 最终的例子:
# create table t4 (
# id int auto_increment primary key,
# name char(32) not null default '',
# pwd char(32) not null default ''
# )engine=Innodb charset=utf8;
auto_increment : 自增
# primary key : 主键索引 (作用: 加快查找的速度)
# not null : 不能为空
# default : 默认值
#
# 注意: 后面一列写完之后, 不能加逗号 (*********),engine=Innodb charset=utf8;如果不写为默认的根据实际情况。
实例:
drop table t1;
注:连带着将数据表中的所有数据都会删掉
# create table t4 (
# id int auto_increment primary key,
# name char(32) not null default '',
# pwd char(32) not null default ''
# )engine=Innodb charset=utf8;
#
# 修改字段:
# alter table 表名(t3) change 原列名(name) 新列名(username varchar(32) not null default '');
#
# 新增字段:
# alter table 表名(t3) add 新列(pwd char(32) not null default '');
#
# 删除字段:
# alter table 表名(t3) drop 列名(pwd);
查看当前库下所有的表名
show tables;
查看t1表的详细信息
show create table t1;
查看表结构
desc t1;
列类型:
#
# a. 数值型
# create table t4 (
# id unsigned mediumint auto_increment primary key,
# name char(32) not null default '',
# pwd char(32) not null default ''
# )engine=Innodb charset=utf8;
#
# tinyint :
# 范围:
# 有符号: -128到127
# 无符号: 0 到 255 unsigned
# smallint
# 范围:
# 有符号: -32768到32767
# 无符号: 0 到 65535 unsigned
#
# mediumint
# 范围:
# 有符号: -8388608到8388607
# 无符号: 0 到 16777215 unsigned
# int
# bigint
#
# 区别:
# a. 取值范围不一样, 根据自己公司的业务来去选择
# b. 无符号和有符号的意思
#
# float(M,D) 浮点型
# decimal(M,D) 定点型 比float更加的精准
#
# 例如: 3.1415151519868789789
# float: 3.141515000000000000
# decimal : 3.1415151519868789789
#
# 126.35
#
# M:小数总共多少位 decimal(5, )
# D:小数点后面几位 decimal(5, 2)
#
# 使用场景:
# 比如 说存 salary 工资 : 6000.23 decimal(,2)
#
# b. 字符串类型
#
# char : 定长 char(32) 这一列的值就是32 优点: 速度快 缺点: 浪费
# varchar : 变长 varchar(32) 优点: 不浪费, 节省空间 缺点: 速度慢
#
# 根据自己公司的业务来去选择:
#
# create table userinfo (
# id unsigned mediumint auto_increment primary key,
# name varchar(128) not null default '',
# pwd char(32) not null default '',
# create_time datetime not null default '1970-01-01 00:00:00'
# )engine=Innodb charset=utf8;
#
# 一般情况下, 如果没有100%的把握, 都是用varchar()
#
#
# text: 文本 范围比较大, 如果存储大量字符的话, 可以使用这个字段
#
# c. 时间类型
# date 2019-6-12
#
# 推荐使用datetime
insert into t3 (id, name) values (1, '你好');
insert into db1.t3 values
(1,'egon'),
(2,'alex'),
(3,'lxx');
# delete from 表名(t3); 将表中的所有的 数据删除掉, 再次添加的时候, 继续会延续上一个 ID
#
# truncate 表名(t3); 将表中的所有的 数据删除掉, 再次添加的时候, ID 会重新开始
#
# truncate 速度快
#
# ps: 工作中, 线上数据库, 这个命令根本不会让你用到
#
# delete from 表名(t3) where name = 'xxxxx';
delete from db1.t1 where name = "SB" ;
update t3 set username='zekai';
#
# update t3 set username='xxxx' where id=3;
#
# update t3 set username='xxxx', pwd='xxxxx' where id=3;
# 缺点:
# 1. 数据重复
# 2. 如果 部门过长的话, 太占用空间
#
#
# 解决方法:
#
# 重新设计一张表, 这张表 中存放部门的相关信息
#
#
# 部门表:
#
# create table department (
# id int auto_increment primary key,
# depart_name varchar(32) not null default ''
# )engine=Innodb charset=utf8;
#
# insert into department (depart_name) values ('公关'), ('关关'),('关公');
#
# create table userinfo (
# id int auto_increment primary key,
# name varchar(32) not null default '',
# depart_id int not null default 1,
#
# # constraint 外键名(fk_userinfo_depart) foreign key (列名(depart_id)) references 表名(department)(关联的列名(id)),
# constraint fk_userinfo_depart foreign key (depart_id) references department(id)
#
# )engine=Innodb charset=utf8;
#
#
# insert into userinfo (name, depart_id) values ('root1', 1);
# insert into userinfo (name, depart_id) values ('root2', 2); 错误的
#
# 注意:
# 创建多个外键的时候, 名称不能一样