此下都是一些基本操作 对于初学者用处还是很大的 里面所有代码建议都使用大写 本人小白一名 写小写方便他人观看不是一个好的习惯
数据库
增
create databases if not exists 数据库名称 character set 编码;
删
drop database exists 数据库名称;
改
alter database 数据库名称 character set 编码;
查
show databases;
表
增
create table 表名(列名1 类型,……….name varchar(24容纳字符串数));
删
drop table exists表名称;
改
a.改表名–>alter table 原名 rename to 新名;
b.改字符集–>alter table 表名 character set 编码;
c.改列名 –>alter table 表名 change 旧列名 新列名 新的数据类型;
d.增一列–>alter table 表名 add 列名 数据类型;
e.删一列–>alter table 表名 drop 列名;
查
show tables;
操作表
增
添加指定列 –>insert into 表名 (列名1,….) values (值1,….);
添加所有列 –>insert into 表名 values(值1,….);
删
truncate table 表名; –(先删表再建表效率高)
改
update 表名 set 列名1=值1,…..[where 条件]; – 后面的where条件不要就是改所有的
查
多字段查询–>select 列名1,…from 表名;
1.去重查询
select distinct 列名 from 表名称;
2.四则运算
select ifnull (列名1,0) + 列名2 from 表名称;
3.起别名
select 列名1 as 别名1,列名2 别名2 from表名称;
条件查询
A->and–>select * from 表名称 where 列名>条件1 and 列名<条件2;
and 或者&&
B->select * from 表名称where 列名 between 条件1 AND 条件2;
C.or–>select * from 表名称where 列名=条件1 or 列名=条件2;
or或者||
D.is null–>select * from 表名称 where 列名 is null;
E.is not null–>select * from 表名称 where 列名 is not null;
模糊查询(以王举例)
查姓王–>select*from hero where name like”王%”;– ‘%’表示多个字符
姓王名字两个字–>select*from hero where name like “王“; – ‘‘表示一个字符
第二个字是王–>select*from hero where name like “_王%”;
带王的–>select*from hero where name like “%王%”;