mysql基本语句1

-- 操作数据库
show databases;
show create database dbname;
-- 创建数据库,创建前进行判断,并制定字符集
create database if not exists DBname character set utf8;

-- 修改
alter database DBname character set utf8;
-- delte
drop database db3;
drop database if exists db3;

-- 使用
-- 查询当前使用的数据库
select database();
use db1; -- 使用db1

-- 操作表
-- CRUD
-- 查询某个数据库中所有表名称
use db1;
show tables;
-- 查询表结构
desc db1;

-- 创建表
create table tableName(列名1 数据类型1, ..., ...);
create table stu like student; -- 复制表
-- 数据类型
int ,double(m,n), date[yyyy-MM-dd],datetime[yyyy-MM-dd HH:mm:ss],timestamp[yyyy-MM-dd HH:mm:ss,不给这个字段赋值,则自动使用系统时间赋值], varchar(20)[字符串类型,最大20个字符,一个汉字一个字符]
create table student(
    id int,
    name varchar(32),
    age int,
    score double(2,2),
    birthday date,
    create_time timestamp
);
-- 删除表
drop table if exists tableName;
create table stu like student; -- 复制表

-- 修改表结构
-- 修改表名称
alter table T1 rename to T2; -- T1 --> T2
-- 修改表字符集
show create table T2;
alter table T2 character set utf8;
-- 修改某列,添加列,删除列
alter table TableName add sex varchar(2);
alter table T2 change sex xingbie varchar(4);
alter table T2 modify sex varchar(8);
-- delete
alter table 表名 drop 列名;

-- DML 增删改表中数据
-- 添加数据
insert into tablename(列名1,...,...) values(值1,值2,..,...);

select * from tablename;
-- 删除数据
delete from 表名 where 条件;
delete from stu where id=1; 
truncate table 表名;
-- 修改数据
update 表名 set 列名1=值1, ...=... where 条件;


-- DQL 查询表中的记录(重点)
select 字段名列表 
from 表名列表
where 条件列表
group by 分组字段
having 分组之后的条件
order by 排序
limit 分页限定


-- 基础查询
-- 查询姓名和年龄
select name,age from stu;
-- 去除重复,结果集都一样的才可以去除
select distinct address from stu ;
-- 计算
select name,math+english from stu;
-- 如果有null参与计算结果都为null
select name 姓名, math + ifnull(english,0) as 总分 from stu;

-- 条件查询
select * from where 条件;
between .. and ..
like
isnull
and
or
not
-- 20-30岁之间
select * from stu where age >=20 and age <=30;


-- 


posted @ 2021-11-28 10:48  头上有多云  阅读(21)  评论(0编辑  收藏  举报