sql语句

进入mysql 命令行

Mysql -uroot -p123456

数据库都是以分号结尾

DDL语句 : CREATE DROP

DML语句 : SELECT,INSERT,UPDATE,DELETE

DCL语句 : 数据库权限

数据库

查看所有数据库;show database

创建数据库;Create database lanxia charset utf8

删除数据库;Drop database lanxia

选择数据库;Use lanxia

查看建库语句;Show database lanxia

#约束---自增长;auto_increment

#约束---不能为空;not null

#约束---默认值;defaurlt xxx

#约束---唯一,不能重复;unique

#约束---指定字符集,不能重复;charset utf8

#约束---主键(比如身份证(id_card)号不会重复,就代表这个人)不能为空;Primary key

#约束---外键;用来表示俩个表之间的关联关系

进入库里面的所有表;Show tables

查看表结构;desc 表名

查看建表语句;show creat table tablename

创建表(需要先use basexxx);Create tabale 表名(

Id int auto_increment

Stu_nmae vchar10not null

Sex varchar2defaurlt ‘男’,

Addr varchar10),

Phone int not null unique

id_card int unique Primary key

);

Create table source

Id int auto_increment

s_id int not null

Grade float not null

清除表里面的数据;trunkcate 表名#清空的表自增长ID1开始,速度比delete

删除表;Drop table xxx

删除整个表里的数据;Delete from 表名#自增长id会继续增长(如删除最后一个id10,那么再加数据会从11开始自增长)

删除字段;alter table drop addr

修改表名;alter table olename rename newname

修改字段数据类型;alter table newname modify name varchar20);

数据

insert into 表名(字段1,字段2values(值1,值2);#指定字段

insert into 表名 values(值1,值2);#如果不指定字段的话,必须写全所有字段的值

insert into 表名(字段1,字段2values(值1,值2),(值1,值2);多条用逗号隔开

update 表名set 字段1=新值1 #如果不加指定条件的话修改的是整个表的数据

update 表名set 字段1=新值1 where name= ‘蓝夏’;#加了筛选条件

update 表名set 字段1=新值1,字段2=新值2  where name= ‘蓝夏’;#加了筛选条件,修改多个字段

Select 字段 from 表名 where name=xxx’;

Select * from 表名;#查询所有数据

Select * from 表名 where  sex=‘男’and money>10#多个条件用and连接,同时满足

Select * from 表名 where  sex=‘男’or money>10#有一个条件满足即可

Select * from 表名 where  sex=‘男’#查询不等于男的数据

Select * from 表明 where addr like %东京%#模糊查询like%是通配符;如%东京就是模糊查询以东京结尾的数据

Select * from 表明 where addr like ‘东_#模糊查询like_是通配符;_类似占位符,东东和东东冬,只会显示东东;%号会显示所以以关于东的数据

Select a.name  from user  a  where a.sex=‘男’;给表起别名,在表名后边空格,一个类似变量的东西

Select * from user  a  where a.name in (‘张三’,‘李四’)and money>10#查询name包涵‘张三’,‘李四’并且money>10的数据

Select * from user  a  where a.id between 1 and 10#查询ID 1-10之间的数据

Select * from user order by id asc#order byID字段 升序 排序

Select * from user order by id desc#descID字段 降序 排序

Select * from user  a  where a.id =‘’ or a.id = is null#查空字符串

Select * from user  a  where a.id =‘’ or a.id = is not null#查不为空字符串

Select count*from user  where sex=女 ;#统计多少行

Select maxmoneyfrom user#统计钱最多

Select minmoneyfrom user#统计钱最少

Select agvmoneyfrom user#统计钱平均

Select summoneyfrom user#统计综合

Select *count*) 人数 from user group by sexclass;查看分组,人数是count的别名

Select *count*) 人数 from user group by sex having name = %;如果还要在group by后面加条件的话,必须用having

Select * from user limit  3#查询3条数据

Select * from user limit  13#查询1-3行数据,从0开始

多表查询

表一: user

     Id   Name     pwd      age

     1    Jmy      123456     18

     2    Jm       123456     22

表二: account

           Id           userid     money

           1              1        200

           2              2        300

Select * from user aaccount b where a.id = b.userid and name=’jmy’#user表里面的idaccount表里面的useriud 关联起来

左连接:select * from name a left join account b on a.id = b.userid 会把左边表有没有匹配的都能查到

右连接:select * from name a right join account b on a.id = b.userid 会把右边表有没有匹配的都能查到

内连接:select * from name a inner join account b on a.id = b.userid 会把两边表都匹配的都能查到

子查询

Select * from account where userid=select id from user where name =jmy’)#把一条sql的结果作为另一条sql的条件

原文地址:https://www.cnblogs.com/lanxia/p/7992460.html