MySql教程

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 


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


这篇关于MySQL常用语句的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!