MySQL命令

    xiaoxiao2022-07-07  151

    一:创建

    进入数据库:mysql -u root -p

    退出数据库:exit;

    注意:数据库的命令,不区分大小写,建议用大写,我这里用小写,方便记录观看理解

    查看数据库:show databases;

    创建数据库:

    create database 数据库名; create database 数据库名 character set utf8; creata database if not exists 数据库名;(建议用它) creata database if not exists 数据库名 character set utf8;

    查询数据库字符集:show create database 数据库名;

    删除数据库:drop database if not exists 数据库名;

    进入数据库:use 数据库名;

    查询当前正在使用的数据库名称: select database();

    创建表:

    create table 表名(列名1 列1类型[约束],....,列名n 列n类型[约束])); cteate table student(id int,name varchar(25), age int,email varchar(255));

    查看数据库中表:show tables;

    查询表结构:desc 表名;

    添加新的字段(列):

    alter table 表名 add 列名 类型[约束]; alter table student add score int;

    修改字段(列):

    alter table 表名 modify 字段名 数据类[约束]; alter table students modify id bigint;

    删除字段(列):

    alter table 表名 drop 字段名; alter table student drop nums;

    修改表名:

    rename table 原表名 to 新表明; rename table student to students;

    查看创建表细节:

    show create table 表名; show create table students;

    修改字符集

    alter table 表名 character set 字符集名称; alter table students character set utf8;

    修改列名称:

    alter table 表名 change 原列名 新列名 类型[约束]; alter table students change name newName varchar(25);

    删除表:

    drop table if exists 表名; drop table if exists bnn;

    二.添加数据

    添加数据:

    insert into 表名 (列1,...,列n) values(值1,...,值n); 添加一条: insert into studnets(id,newName,age,eamil,score) values(1,'a',99,'99',99); 添加多条: insert into students(id,newName,age,eamil,score) values(2,'b',88,'88',88), (3,'c',66,'66',66),(4,'d',44,'44',44);

    删除数据

    delete from 表名 [where 条件]; truncate table 表名;(推荐使用,效率高,先删除,在创建表)

    注意:如果不加条件,则会删除表中所有记录

    修改数据

    update 表名 set 列名1 = 值1,...,列n=值n [where 条件]; uddate students set name = wyf where id = 3;

    注意:如果不加条件,则会将表中所有的记录全部修改

    查询数据 1.语法:

    select 字段列表 from 表名列表 where 条件列表 group by 分组字段 having 分组之后的条件 order by 排序 limit 分页限定

    2.基础查询:

    1.多个字段查询 select 字段1,...字段n from 表名; 注意:select * from 表名;(查询所有,一般不要用) 2.去除重复: distinct 3.计算列: ifnull(表达式1,表达式2):null参与的运算,计算结果都为null * 表达式1:哪个字段需要判断是否为null * 如果该字段为null后的替换值。 4.起别名: as: as也可以省略

    3.条件查询:

    运算符: * > 、< 、<= 、>= 、= 、<> * BETWEEN...AND * IN( 集合) * LIKE:模糊查询 * 占位符: * _:单个任意字符 * %:多个任意字符 * IS NULL * and 或 && * or 或 || * not 或 !

    示例:

    -- 查询年龄大于20岁 SELECT * FROM student WHERE age > 20; SELECT * FROM student WHERE age >= 20; -- 查询年龄等于20岁 SELECT * FROM student WHERE age = 20; -- 查询年龄不等于20岁 SELECT * FROM student WHERE age != 20; SELECT * FROM student WHERE age <> 20; -- 查询年龄大于等于20 小于等于30 SELECT * FROM student WHERE age >= 20 && age <=30; SELECT * FROM student WHERE age >= 20 AND age <=30; SELECT * FROM student WHERE age BETWEEN 20 AND 30; -- 查询年龄22岁,18岁,25岁的信息 SELECT * FROM student WHERE age = 22 OR age = 18 OR age = 25 SELECT * FROM student WHERE age IN (22,18,25); -- 查询英语成绩为null SELECT * FROM student WHERE english = NULL; -- 不对的。null值不能使用 = (!=) 判断 SELECT * FROM student WHERE english IS NULL; -- 查询英语成绩不为null SELECT * FROM student WHERE english IS NOT NULL;

    后续待添加???????????????????????????

    最新回复(0)