创建数据表
-- 创建表语法
-- create table 表名(
-- 字段名1 类型 约束,
-- 字段名2 类型 约束,
-- ...,
-- 字段名n 类型 约束
-- );
CREATE TABLE students(
id int unsigned primary key auto_increment,
name VARCHAR(10),
age INT UNSIGNED,
height DECIMAL(5,2)
);
添加表数据
-- insert into 表名 (字段1,字段2,...) values (值1,值2,...)
--多行插入
insert into scores values (null,'王文永','1班','数据结构',70.9),(null,'万有波普','1班','数据库',89.3),(null,'柳州','2班','文学思想',88.9);
修改表数据
-- 数据的修改操作
-- update 表名 set 字段名1=字段值1,字段名2=字段值1,... where 条件
update scores set class='2班',score=80.5 where name='王文永';
查询表数据
-- 查询表中数据
-- 简单查询 select * from 表名 where 条件;
select * from scores;
select * from scores where class='1班';
删除表数据
1、delete删除数据
-- delete from 表名 where 条件
-- 此方法为物理删除,工作中大部分使用逻辑删除。
-- 逻辑删除是指通过设定一个字段来标识当前记录已经删除。
delete from scores where id=1;
2、drop删除数据
-- drop删除表,删除数据和表结构
-- drop table 表名
drop table scores;
3、truncate删除数据
-- 删除数据
-- Truncate删除数据,删除数据,保留表结构
-- Truncate table 表名
truncate table scores;