一、数据库、表的建立/删除
新建数据库:
create database 数据库名;
#create database students; #新建一个students的数据库

新建表:
在建表之前,需要指定在哪个数据库下建表,使用use 数据库名;

接下来就可以建表了:
CREATE TABLE table_name (
field1 datatype,
field2 datatype,
field3 datatype
) character set 字符集 collate 校验规则 engine 存储引擎;
说明:
field 表示列名
datatype 表示列的类型
character set 字符集,如果没有指定字符集,则以所在数据库的字符集为准
collate 校验规则,如果没有指定校验规则,则以所在数据库的校验规则为准
create table student
(
id int auto_increment primary key,
sno varchar(20) not null,
name varchar(20)
);


表的删除-drop
sql
drop table 表名; #这是对整个表结构的删除,不是对数据的删除

二、数据插入-insert
插入语句:
insert into 表名(列1,列2,...) values(内容1,内容2,...);
将values内的内容插入到对应的列里面。
若未指定列,则默认插入一整行内容。
指定全部列:
insert into student(id, sno, name) values(1,2021001,'张三');

不指定列:
mysql> insert into student values(2,2021002,'李四');

指定部分列,会向指定的那列插入数据:
insert into student(id,sno) values(3,2021003); //id插入3,sno插入2021003,其余列默认NULL

插入多行数据,不同行数据用,
分开:
insert into student(sno, name) values(2021004, 'zhangsan'), (2021005, 'lisi');

三、数据修改-update
修改语句:
update 表名 set 列名1 = 值 WHERE 列名2 = 值;
修改表中满足where条件的那一行,将列名1进行修改,如果需要修改多列数据,每一列数据用,
分割开。
注:若为加where条件筛选,则是将该列的数据全部进行修改。
示例:
将张三的学号sno修改为2021007:
update student set sno = '2021007' where name = '张三';

四、数据删除-delete
delete from 表名 where 条件;
删除满足条件的对应那一行的数据。
注:如果没有where条件,则是删除所有行的数据***
示例:
删除姓名"张三"的那一行数据:
delete from student where name = '张三';

五、数据查找-select
SELECT [DISTINCT] {* | {column [, column] ...}
[FROM table_name]
[WHERE ...]
[ORDER BY column [ASC | DESC], ...]
LIMIT ...
执行顺序:from-->where->group by->select->having
全列查询:
sql
select * from student;
通常情况下不建议使用 * 进行全列查询 1. 查询的列越多,意味着需要传输的数据量越大; 2. 可能会影响到索引的使用。
**指定列查询:**指定列的顺序不需要按定义表的顺序来
sql
select id, name from student;

六、表结构修改-alter
为表添加字段
ALTER TABLE tablename ADD (column datatype [DEFAULT expr][,column datatype]...);
修改表字段属性
ALTER TABLE tablename MODIfy (column datatype [DEFAULT expr][,column datatype]...);
删除某一字段
ALTER TABLE tablename DROP (column) ;
添加字段
为student表添加math字段,设置默认值为0
alter table student add (math int default 0);

修改字段属性
修改student表中chinese字段的属性为int:
alter table student modify chinese int;

删除字段
删除student表中English字段:
alter table student drop English;

修改表名
修改student表表名为users:
alter table student rename users;

修改字段
修改student表中math字段修改为highMath int:
alter table student change math highMath int; // 新字段需要完整定义属性

简单总结一句,只有对表进行操作的时候才会加上table关键字+表名;对数据进行操作时,直接加的是表名。