【Mysql】基本语法(数据操作+表结构操作)

一:数据操作

1.数据库(库名)

create database 数据库名      //创建数据库
use 数据库名                  //选择数据库
drop database 数据库名        //删除数据库

[root@xibushuma ~]# mysql db2 -e 'show tables' 不登录mysql 情况下,查看数据库db2的所有表

2.创建数据表(表名)

create table 表名 (字段名  字段类型,...)

primary key(id):将id设置为主键
auto_increment :id为自增属性

create table biao_name(
    id int not null auto_increment,
    name varchar(10) not null,       
    primary key(id)
    )engine=Innodb default charset=utf8;

3.删除数据表(表名)

drop table (表名)

4.插入数据表(表名)

insert into 表名(字段名,字段名2values (字段值,字段值2

插入值为当前时间,用 now() 代替

5.查询数据表(表名)

select 字段名 from 表名 

6.更新/删除表数据

update 表名 set 字段名1=new-value1, 字段名2=new-value2 where id=1; //更新数据

delete from 表名 where id=1;    //删除id=1的数据

7.like :包含’.com’条件的数据

select 字段 from 表名 where yuming like '%.com';

union : 用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中(会删除重复的数据)。
distinct : 可选,删除结果集中重复的数据。
all: 可选,返回所有结果集,包含重复数据。
order by :按照什么字段排序 (ASC 升序 / DESC 降序)

select country, name from Websites  where country='CN'
union all   //返回所有结果,包含重复数据
select country, app_name from apps  where country='CN'
order by country ASC ;   //按照排序字段升序

8.group by 根据一个或多个列对结果集进行分组

select column_name, function(column_name)
from table_name
where column_name operator value   //根据字段的运算符值
group by column_name

with rollup 可以实现在分组统计数据基础上再进行相同的统计(SUM,AVG,COUNT…)总和、平均值、计数。

select coalesce(a,b,c);
select coalesce(name, '总数'), sum(signin) as signin_count from  employee_tbl group by name with rollup;

二:表结构操作

举例:数据表(newtable / oldtable) 、字段(newfield / oldfield)

1.修改表名

alter table 旧表  rename to 新表;

2.增加表字段

alter table  表名  add 新字段 类型;
alter table  表名  add 新字段 first;               //在第一位置插入字段
alter table  表名  add 新字段 after 原字段;         //在原字段后插入

3.删除表字段

alter table 表名 drop 字段;                   //删除字段
alter table 表名 alter 字段i drop default;     //删除字段i的默认值

4.修改表字段

alter table 表名 modify j bigint not null default 100;  //更改字段j类型和默认值
alter table 表名 alter i set default 1000;    //设置字段 i 的默认值为1000
alter table 表名 change i j bigint;     //重命名字段i 为 j,需要指定j的类型为bigint   注意:此命令均需要有两个字段

5.查看数据表结构类型

show table status  like ‘表名’\G;    //查看结构属性
show columns from 表名;              //查看表字段属性  等同于 desc 表名;
  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值