数据库定义语言DDL
数据库的增删选查
查:show databases;
创建:create database 数据库名称;
选择使用:use 数据库名称;
数据表的增删改查
创建:create table 表名 (字段名 类型);
查看表:show tables;
查看表结构:desc 表名;
常用约束条件
PRIMARY KEY 主键约束
NOT NULL 非空约束
UNIQUE 唯一约束
AUTO_INCREMENT 自增字段
DEFAULT 默认约束
FOREIGN KEY 外键约束
修改表格结构
修改表名:alter table 原表名 rename 新表名;
修改字段名:alter table 表名 change 原字段名 新字段名 数据类型 [自增/非空/默认] [字段位置];
修改字段类型:alter table 表名 modify 字段名 新数据类型 [自增/非空/默认] [字段位置];
添加字段:alter table 表名 add 字段名 数据类型;
修改字段的排列位置:
alter table 表名 modify 字段名 数据类型 first;
alter table 表名 modify 字段名 数据类型 after 参照字段;
删除字段:alter table 表名 drop 字段名;
数据库操作语言DML
插入数据
指定字段名插入:insert into 表名(字段名,字段类型)values (字段值2,字段值2);
不指定字段插入:insert into 表名 values (字段值1,字段值2);
批量导入数据:安全路径、cmd命令
更新数据
update 表名 set 字段名1=字段值1 …… [where 更新条件];
删除数据
delect from 表名[where 条件];
truncate 表名;
两者的联系和区别:
联系:都是删除表中全部数据,保留表结构
区别:1.delete可以加条件删除部分数据,truncate只能删除全部(语法上)
2.delect from删除数据保留结构表结构,truncate全部删除再建一张新表(比delete执行速度快)








暂无数据