阅读 257

MySQL常用语句(mysql常用语句大全)

1、创建数据库: create database  数据库名;

2、查看数据库: show databases;

3、使用数据库:use 数据库名;

4、创建表:create table if not exists 表名(字段名1 字段类型 primary key   auto_increment not null ,字段名2 字段类型);

例如:create table if not exists  ww(id int primary key   auto_increment not null,name varchar(11));

解析:primary key:主键;auto_increment:自动增加;not null:不为空;comment'':注释;

5、查看表:show tables;

6、插入一条数据:insert into 表名 (字段名1,字段名2) values ('字段名1要插入的值','字段名2要插入的值');

例如:insert into ww (id,name)  values ('1','aa');

       insert into ww(name) values ('bb');

注意:当id是自动增长时,插入数据时可以不用管他

7、插入多条数据:insert into 表名 (字段名1,字段名2) values ('要传入的值1','值1'),('值2','值2'),.....,('值3','值3');

例如: insert into ww(name) values ('cc'),('dd');

8、查看表的全部内容:select * from 表名;

例如:select * from ww;

9、更新语句:update 表名 set 字段1=值1 ,字段2=值2,....,字段名n=值n  where 条件表达式;

例如:update ww  set name='vv' where id=1;

10、删除语句:delete from 表名 where 条件语句;

例如:delete from ww where id=1;

11、查看表的结构:desc 表名;

12、查看表的sql语句:show create table 表名;

13、修改表名:alter table 旧表名 rename  新表明; 

例如:alter table ww rename xx;

14、添加字段:alter table 表名 add 字段名 字段类型;

例如: alter table xx add book varchar(11);

15、修改字段类型: alter table 表名 modify 字段名  字段类型;

例如:alter table xx modify book int;

16、修改字段名:alter table 表名 change  旧字段名  新字段名  字段类型;

例如:alter table xx change book look  int;

17、删除字段名:alter table 表名 drop 字段名;

例如:alter table xx drop look;

18、删除数据表:drop table 表名;

19、删除数据库:drop database 数据库名;

20、查询表中的最后一条数据:

select * from 表名 order by id desc limit 1;

21、查询数据表的第一条信息:

select * from 表名  order by id  asc limit 1;

22、mysql数据库实现多表查询:

 select  字段名1,字段名2,字段名3,......,字段名n from 表名
UNION all
select 字段名1,字段名2,字段名3,......,字段名n from 表名
例如:
select id,subject,questiontype,question,score,difficulty ,createtime from single
UNION all
select id,subject,questiontype,question,score,difficulty,createtime from mcq


23mysql数据库实现多表查询,并按照其中一个字段进行排序
 select  字段1,字段2,字段3,......,字段n from 表名
UNION
select 字段1,字段2,字段3,......,字段n from 表名
 order by  字段1/字段2/..../字段n  desc asc


标签:语句,常用,MySQL,字段,select,表名,table,alter,字段名
来源: https://www.cnblogs.com/wyxjava/p/15582723.html


文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐