数据类型
- int:整数(默认有符号)
- decimal:小数,decimal(5,2)表示共存5位数,小数占2位,整数占3位
- varchar:字符串(一个中文占一个字符、两个字节-gbk编码、三个字节-utf-8编码;一个字母占一个字符、一个字节)
- datetime:日期时间,例‘2023-01-01 12:00:00’(给长度易报错)
primary key:主键
foreign key:外键
not null:非空
unique:唯一
default:默认值
数据库
创建数据库test:
CREATE database test charset=utf8 collate=utf8_general_ci;
查看数据库test:
show create database test;
使用数据库test:
use test;
查看当前所在数据库:
select database();
更改数据库test的编码:
alter database test default character set gb2312;
删除数据库test:
drop database test;
查看所有数据库:
show databases;
数据表
创建数据表stu,stu中有id和name字段(unsigned:无符号;primary key:主键;auto_increment:自增长):
create table test (
id int unsigned primary key auto_increment,
name varchar(5),
height decimal(5,2)
);
判断表stu是否存在,如果存在,先删除再创建:
drop table if exists stu;
create table stu (
id int key,
name varchar(5),
height decimal(5,2)
);
查看创建的表stu:
show create table stu;
查看stu表结构:
desc stu;
删除表stu:
drop table if exists stu;