文章目录
1.数据库管理(文件夹)
- 查看已有数据库
sql
show databases;
- 创建数据库
sql
create database database_name default CHARSET utf8 collate utf8_general_ci;
- 删除数据库
sql
drop database database_name;
- 进入数据库
sql
use database_name;
- 查看数据库下所有的表
sql
show tables; # 要先选择数据库,即use
2.数据表的管理(文件)
- 创建表
sql
create table table_name{
column_name type,
column_name type
} default charset=utf8;
create table student( # 创建表:名称+类型
id int auto_increment primary key, # 主键(不允许为空、不允许重复) + 自增
name varchar(10), # str,()中填长度,做出限制,最大255
age int default 3, # 插入数据时,age列的值默认为3
gender varchar(10) not null # 不允许为空
) default charset=utf8;
- 删除表
sql
drop table table_name;
3.数据行操作
-
新增数据
sqlinsert into table_name(column_name, column_name) values(price, price); insert into table_name(column_name, column_name) values(price, price), (price, price), (price, price);
-
删除数据
sqldelete from table_name; delete from table_name where condition;
-
修改数据
sqlupdate table_name set column_ = price; update table_name set column_ = price, column_ = price; update table_name set column_ = price where condition;
-
查询数据
sqlselect * from table_name; select column_name, column_name from table_name; select column_name, column_name from table_name where condition;
4.常用数据类型:
- tinyint
- int
- bigint
- float
- double
- decimal
- char(定长字符串,速度快)
- varchar(变长字符串,节省空间)
- text(用于保存变长的大字符串)
- mediumtext
- longtext
- datetime(年月日时分秒)
- date(年月日)