加入收藏 | 设为首页 | 会员中心 | 我要投稿 吕梁站长网 (https://www.0358zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

数据库操作基础命令

发布时间:2022-12-08 13:33:43 所属栏目:MySql教程 来源:互联网
导读: 创建数据库(create database [if not exists]数据库名;)中括号里的可加可不加意思是判断这个数据库是否存在 如果不存在就创建
创表并且添加数据
create table 表名(列名 列属性,列名 列

创建数据库(create database [if not exists]数据库名;)中括号里的可加可不加意思是判断这个数据库是否存在 如果不存在就创建

创表并且添加数据

create table 表名(列名 列属性,列名 列属性);

create table 表名(主键int primary key, wname varchar(20))engine innodb charset utf8;

insert into 表名(列名,列名)values(数据);

增加:insert into stu values()

删除数据:delete from 表名 where 条件; 条件NULL的判断 is null is not NULL;

修改:update 表名 set 列名1=值1,列名2=值2 where 条件

查:select

1:登录(mysql -u root -p)

2:查看所有数据库(show databases;)

3:切换主要数据库(use 数据库名;)

4:创建表(create table [ if not exists] `表名字`)

4:查看表(show tables;)

5:查看表中的信息(desc 表名;)

6退出数据库(exit)

7:创建数据库(create database [if not exists]数据库名;)中括号里的可加可不加意思是判断这个数据库是否存在 如果不存在就创建

8:删除数据库(drop database [if exists] 数据库名;)如果存在就删除

9: 查询数据库表中的信息 select `name` from 要查询的表的名字;(如果数据库名是关键字用``)

10:修改表名(alter table 旧表名 rename as 新表名)

11:增加表的字段(alter table 表名 add 字段名 字段的列属性)

12:修改表的字段(alter table 表名 modify 字段名 列属性)修改列属性

13:字段的重命名(alter table 表名 change 旧字段名 新字段名 列属性 )

14:删除表的字段(alter table 表名 drop 字段名)

15: 删除表(drop table 表名)

16.查看表中的数据select * from 表名;

17.给表添加数据:insert into 表名(id,name,age)values(1,'张三',22);

18.修改表的数据:update 表名 set 列名1=值1,列名2=值2 where 条件;

19.查表的数据:select 列名,列名2 from 表名 where 条件;

20.输出 。select 语句;输出语句

21.拼接字符串:select concat('5a','6b','123');

22.内置函数:select now(); select abs();

23.删除数据:delete from 表名 where 条件; 条件NULL的判断 is null is not NULL;

24.查询表中信息:select*fromgoodswheregoods_id!=3andgoods_id!=11;

goods_id!=3andgoods_id!=11也等于goods_id in(3,11)

goods_id!=3 and goods_id!=11;也等于goods_id not in(3,11)

25.模糊查询 不能使用= 要用like %表示n任意字符 _表示一个任意字符

列如select *from goods where goods_name like '%耳机%';

查询模型:列可以当做变量进行运算

select *,(market_price-shop_price)from goods;

select *,(market_price-shop_price>=500) as '商品' from goods;

having 与where不同的是where只能用原表中的列名称

select *,(market_price*goods_number) as 'new_mony' from goods having new_mony>=3000;

子查询

select * from goods where goods_number = (select min(goods_number) as min_price from goods);

26.对一列的数据求和

select sum(列名)from 表名;

27.计算平均值

select avg(列名)from 表名;

28.查询多少行记录

select count(*)from 表名;

29.每一列

select concat('[双十一热销]',goods_name) from goods;

30.分组查询

select goods_id,avg(market_price) from goods group by goods_id;

31.排序

select * from 表名order by (列名) desc;

desc 降序 asc 升序

32.限制条目数

Limit n ;#表示显示几条数据

Limit m,n;m表示跳几条数据,n表示显示几条数据

面试题:

请问mysql的书写顺序和执行顺序

书写顺序:select,from,join on数据库查询操作,where,group by,having,order by,limit

执行顺序:from,on,join,where,group by,having,select,order by,limit

左连接:

开启事务:start transaction;

回滚: rollback;

提交事务: commit;

(编辑:吕梁站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!