数据库:(SQL语句)

基本操作:


mysql -h 127.0.0.1 -u root -p    # 登录mysql,-u后面是用户名,-p后是登录密码,若不填回车后会提示输入密码

select now();      显示当前时间

exit/quit/ctr+d   # 退出数据库

show databases;  // 展示当前有哪些数据库

use 数据库名;      # 使用某一个数据库

select  database();     # 查看当前使用的是那个数据库

show  tables;    # 展示或查询当前数据库有哪些表

desc 表名;  / desc 数据库.表名;             -----  查看表结构

select * from 表名;         # 查表,返回表的所有内容

create database 数据库名 charset=utf8;      # 创建数据库

drop database 数据库名;      # 删除数据库  慎用!

创建表

语法:create table 表名(字段1 字段1属性,....);

示例:

create table students(

id int unsigned primary key auto_increment  not null,      # id 整型 无符号 主键 自增 非空

name varchar(20) not null,              # name 字符串(长度20) 非空

age tinyint unsigned default 0,         #  age 小整型 无符号 默认为0

height decimal(5,2),         

gender enum('男','女','人妖','保密')  defalut '男'        # enum 枚举类型,默认‘男’             

);

drop table 表名;     # 删除表

(对表的操作)增、删、改、查

(增)insert into 表名 values(字段1,字段2....);

          insert into 表名(id,name,age) values(1,“张三”,20);

          (一次增加多条数据)insert into 表名(id,name,age) values(1,“张三”,20),(2,  “李四”,30),(3,“梦三”,120);

(删)delete 字段 from 表名;

truncate table 表名;(清空)

delete 字段 from 表名 where 条件1 and 条件2 and ...;

(改)update 表名 set 字段名1=值1,字段名2=值2 where 条件;

          alter table 表名 rename 新表名;

          alter table 表名 add 新字段 新字段属性;

          alter table 表名 drop 字段名;

          alter table 表名 change 字段名 新字段属性;

          alter table 表名 add primary key(字段名如:id);

          alter table 表名 drop primary key;

(查)select 字段 from 表名;

          select 字段1,字段2,字段3... from 表名 where 条件;

属性:

int(),char(),varchar(),year,date,datetime,....

not null;  不为空

primary key;  主键(只有一个主键)

default ~;  默认值为?

auto_increment;自动增量

条件:

and //并且

or    (或者)

like “王%”  (像、含什么的字)

where 字段名 in()

is null    (为空)

order by 字段    (升序)

order by 字段 desc    (降序)

order by 字段1,字段2 desc    (按字段1升序,再按字段2降序)

group by 字段    (分组,“每个”)

as 别名/“别名”  或者用空格,再加别名

having //如果计算函数被当做条件的时候,我们要用having,注意不能直接跟在where后面

having avg(?)>90

between and    (在什么之间)

in()  包含什么

union          (联合,结合,可用于把两张表的结合)

all()

any()

limit n  前n列

计算函数:

sum(字段名)  求和

avg(字段名)    求平均

count(字段名)    计数

min(字段名)

max(字段名)

distinct  字段名      (有区别的,不重复,作用相当于group by )

year(now())-year(birthday)-------知道生日,计算年龄公式

数学表达式:

+ 、- 、 *、 / 、>、 < 、 = 、!= 、 <>、>=、<=、%

求和:字段1+字段2+字段3....

求平均:(字段1+字段2+字段3....字段n)/n

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 50个常用的sql语句Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname...
    哈哈海阅读 1,256评论 0 7
  • 注:1.mysql是一种关系型数据库 2.大小写不敏感 3.字符串用单引号,若字符串里有单引号,则...
    孙浩j阅读 1,351评论 0 2
  • 哈喽!我是一个充满阳光的美少女欧阳曦悦。 我是新来的。 我爸妈经常换地方工作,我也经常转校,还好我反对经常转...
    我带你飞阅读 380评论 0 0
  • 工作不是任务 以前的观念中,工作是一种任务。任务,首先要很重视,时刻放在心上;其次以完成它为目的。 重视是没有异议...
    叁一叁阅读 276评论 3 3
  • 纯皮出口商品,批量发货,真正的物美价廉,有意着联系!
    荷霖阁阅读 183评论 0 0