Mysql—数据库管理与表管理
数据库管理
一、创建数据库
mysql> create database online; -- 方法一 mysql> create database online default charset utf8; -- 方法二,也可以使用:charset=utf8; mysql> create database online default character set utf8; -- 方法三,也可以使用:character set=utf8;
二、查看数据库
mysql> show databases; -- 查询所有的数据库 mysql> show create database online; -- 查看某个数据库的默认字符集 mysql> select database(); -- 查看当前所在数据库 mysql> use online; -- 选择进入某个数据库 mysql> show tables; -- 查看某个库的所有表
三、修改数据库
mysql> alter database online default character set gbk; -- 修改数据库的默认字符集
四、删除数据库
mysql> drop database online; -- 方法一 mysql> drop database if exists online; -- 方法二
数据表管理
一、创建表:create table 表名;
create table if not exists tb_student( id int(11) auto_increment, -- 自增约束 username varchar(32) unique, -- 唯一约束 password varchar(64) not null, -- 非空约束。varchar:必须给定一个宽度值,否则报错 phone char(11) default '15925638579', -- 默认约束。char:如果插入12个字节,只显示11个字节。输入12个汉字,也显示11个汉字。 email char(20) default '1548629568@qq.com', -- 默认约束 brief text, -- 文本类型:text(描述类文本)、longtext(文章类文本) age tinyint unsigned, -- 1个字节,无符号unsigned。默认是有符号signed score smallint unsigned, -- 2个字节,无符号unsigned。默认是有符号signed salary float(5,2), -- 最大值:999.99 lovely set("java", "c++", "python") default 'c++', -- 多选,最多只能包含64个成员。 gender enum("boy", "girl", "secret") default 'boy', -- 单选,最多可以有65535个不同的值。 _year year, -- YYYY _date date, -- YYYY-MM-DD _time time, -- HH:MM:SS birthday datetime, -- YYYY-MM-DD HH:MM:SS create_time timestamp not null default current_timestamp comment '创建时间', update_time timestamp not null default current_timestamp on update current_timestamp comment '更新时间', primary key (id), -- 主键约束 check(salary>0 and salary<1000) -- 检查性约束 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 auto_increment=100 comment="学生表";
二、删除表:drop table 表名;
mysql> drop table tb_student; mysql> drop table if exists tb_student;
三、查看表:desc 表名;
mysql> desc tb_student; -- 查看表的基本结构,方法一 mysql> describe tb_student; -- 查看表的基本结构,方法二 mysql> show columns from tb_student; -- 查看表的基本结构,方法三 mysql> show create table tb_student; -- 查看表的详细结构,除了字段名、字段的数据类型、约束条件外,还可以查看表的默认存储引擎和字符编码。
四、修改表:alter table 表名 执行动作
1.修改表名:alter table 旧表名 rename 新表名;
mysql> alter table student rename stu;
7.修改字段约束:alter table 表名 drop index 字段名
mysql> alter table student drop index title -- 删除唯一约束
8.修改字段索引:alter table 表名 drop index 字段名修改
https://www.cnblogs.com/linyujin/p/10876251.html
https://www.cnblogs.com/xinruyi/p/11192256.html
表内容管理
一、插入数据
INSERT INTO tb_student(username, password, age, score, salary, _year, _date, _time, birthday) values('jack', 'admin', 28, 96, 88.66, 1994, 19940506, 090000, 19940506090000); INSERT INTO tb_student(username, password, age, score, salary, lovely, gender, _year, _date, _time, birthday) values('jose', 'admin', 27, 96, 88.66, "java,c++", "boy", 1994, 19940506, 090000, 19940506090000);
二、删除数据
mysql> delete from tb_student; -- 方式一:删除内容、不释放空间,不删除定义。可以带条件(where)删除。 mysql> delete from tb_student where id=1; mysql> truncate table tb_student; -- 方式二:删除内容、会释放空间但不删除定义。不能带条件(where)删除。
三、修改数据
四、查询数据
1、NULL(空值) 与 ""( 空字符串) 的查询:空值,必须要用is 或者 is not 去匹配。空字符串,用 = 或者 != 去匹配
mysql> select * from tb_student Where brief = '' -- 查询简介内容为空字符串的学生 mysql> select * from tb_student Where brief != '' -- 查询简介内容不为空字符串的学生 mysql> select * from tb_student where phone is null; -- 查询手机号的值等于null的学生 mysql> select * from tb_student where phone is not null; -- 查询手机号的值不是null的学生