MySQL基础操作语法

Mysql基础操作语法:

以下是我本人看一些视频自学总结的一些关于数据库的一些最最基本操作语法,并附上操作截图以便于理解,都是举例,没有贴具体的语法,以便于大家理解,小白系列,请诸位同为菜鸟的小白迁就着看一下,相信对于新手系列还是有一定帮助的。

show databases;    //显示所有的数据库

create database gc;   //创建数据库gc

drop database gc;    //删除数据库gc

create database table2;  //创建表table2

use table2;       //使用表table2 ,然后可以进行表的增删查改

show tables;     //显示所有的表

create table account;

show tables;

describe account; //获取表结构信息

drop table account1;

 

 

 

 

//创建表的数据类型

CREATE TABLE account1 (

   id bigint(20),

   createTime datetime,create

   ip varchar(255),

   mobile varchar(255),

   nickname varchar(255),

   passwd varchar(255),

   username varchar(255),

   avatar varchar(255),

   brief text,

   job varchar(255),

   location varchar(255),

   qq varchar(255),

   gender int(11),

   city varchar(255),

   province varchar(255)

) ;

 

 

  //增删table列

alter table [table_name] add [column_name] [data_type] [not null] [default]

     alter table account add c1 int(11) not null default 1;

 alter table [table_name] drop [column_name]

     alter table account drop c1;

 

 

//修改table--修改列信息和表名

  alter table [table_name] change [old_column_name] [new_column_name] [data_type]

1,  只改列名;[old_column_name] !=[new_column_name]

alter table account change city country varchar(255);

2,只改数据类型;[old_column_name]== [new_column_name],data_type改变

alter table account change country country text;alte

3,列名和数据类型都改;

修改表名: alter table [table_name] rename [old_column_name]

alter table account rename newaccount;

 

//插入&查看表数据

insert into [table_name] values(值1,值2,.....)

insert into [table_name](列1,列2,......) values(值1,值2,.....)

    insert into book values

    insert into book(id) values(6)[其他的必须设定默认值]

select * from table_name;//查看全部表数据

     select * from account;

select col_name1,col_name2,......from table_name;//查看特定列*

 

//where条件查询

select * from table _name where col_name [运算符] 值

      select * from book where title = 't' ;[在book表中在title列下查询并显示值为t的数据行]

组合条件and,or;  

     where 后面可以通过and与or 运算符组合多个条件筛选;语法如下:

          select * from table_name where col1 = xxx and col2 = xx or col3>xx

             select * from book where id = 5 and title = 't2' or id<6;[除数字外其他的数据项内容都要加单引号]

 

运算符

//where 的null判断

select * from table_name where col_name is null;

select * from table_name where col_name is not  null;

//select distinct(精确的)

   select distinct col_name from table_name;

select distinct title from book;

 

//select 结果按order by排序【asc升序,desc降序排列】

按单一列名排序:select * from table_name [where 子句] order by col_name[asc/desc]

按多列排序:select * from table_name [where 子句] order by col1_name[asc/desc],col1_name[asc/desc]……

 

//select 结果按limit截取

 select * from table_name [where 子句] [order 子句] limit [offset,] rowCount;

offset:查询结果的起始位置,第一条记录的其实是0

rowCount:从offset位置开始,获取的记录条数

需注意的是  limit rowCount = limit 0,rowCount [行从0开始]

//insert into 与select组合【数据迁移】

insert into[表名1] select  列1,列2 from [表2]

    insert into book2 select * from book where id<4;

insert into[表名1](列1,列2 ) select  列3,列4 from [表2]

   insert into book2(title) select content from book where id=5 ;

//更新表数据

修改单列:update 表名 set 列名 = xxx[where 字句]

    update book set content = 'nice day' where id=3;

修改多列:update 表名 set 列名1 = xxx,列名2 = xxx……[where 字句]

 

 

//高阶where  的in操作符

select * from 表名 where 列名 in(values1,values2……)

   select * from book where title in('t','h');

select * from 表名 where 列名 in(select 列名 from表名)

   select * from book2 where title in(select title from book where id<4);

注解:列名 in(values,values2……)等同于  列名=values1 or 列名= values2;

 

//高阶between操作符

select* from 表名 where列名 between 值1 and 值2;

select* from 表名 where列名 not between 值1 and 值2;

 

 

//高阶where的like 操作符【字符串模糊匹配】

select * from 表名 where 列名 [not] like pattern【like 后面加单引号】

     select * from book where content like 'c%';

pattern:匹配模式,比如 ‘abc’仅仅匹配abc

                                       '%abc'[以abc结尾的字符串]

                                     ‘abc%’[以abc开头的字符串];

                                      ‘%abc%’[包含abc的字符串]

"%"是一个通配符,理解上可以把她当成任何字符串;

以上就是我总结的一些最基础的数据库语法,希望会对大家有所帮助,祝大家在敲键盘这条道路上越走越远。

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值