1、数据库的操作
1.1 显示当前的数据库
show databases;
1.2 创建数据库
CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [,
create_specification] ...]
create_specification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
说明:
大写的表示关键字
[] 是可选项
CHARACTER SET: 指定数据库采用的字符集
COLLATE: 指定数据库字符集的校验规则
示例:
创建名为test的数据库
create database test;
如果没有test2,则创建,有则不创建
create database if not exists test2;
如果没有test的数据库,则创建一个使用utf8mb4字符集的test数据库,如果有则不创建
create database if not exists character set utf8mb4
1.3 使用数据库
注意:只有在使用指定数据库后,才能操作对应数据库中的表
use 数据库名;
1.4 删除数据库
语法:
DROP DATABASE [IF EXISTS] db_name;
示例:
drop database test1;
drop database if exists test2;
本次删除是测试的数据库,大家在工作中谨记,不要随意进行drop操作,如果要进行,也要及时备份数据库,防止出现不可逆的操作。
2、常用的数据类型
2.1 数值类型:分为整型和浮点型
2.2 字符串类型
2.3 日期类型
3、表的操作
操作表时要先使用数据库
3.1 查看表结构
desc 表名;
3.2 创建表
语法:
CREATE TABLE table_name (
field1 datatype,
field2 datatype,
field3 datatype
);
后面可以加comment增加字段的注释
示例:
create table test1(id int,
-> name varchar(20),
-> password varchar(50) comment '密码',
-> age int,
-> sex varchar(1),
-> birthday timestamp,
-> amout decimal(13,2),
-> resume text
-> );
3.3 删除表
语法格式:
DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ...
示例:
-- 删除 test1 表
drop table test1;
-- 如果存在test1 表,则删除 test1 表
drop table if exists test1 ;