连接数据库

1
mysql -h10.20.66.32 -uroot -p123456

-h后面是mysqlServer所在地址,-u后面是用户名,-p后面是密码

查看数据库

1
show databases;

使用数据库

1
use test;

查看表

1
show tables;

查看表结构

1
desc winton

建表

1
2
3
4
create table t1(
    id int not null primary key, 
    name char(20) not null
    );

语法 create table 表名称( 字段名 字段名类型 字段描述符,字段名 字段类型 字段描述符);

修改表

添加字段

1
alter table t1 add(score int not null);

语法:alter table 表明称 add(字段名 类型 描述符);

移除字段

1
alter table t1 drop column score;

语法:alter table 表名 drop colunm 字段名,drop colunm 字段名;

变更字段

1
alter table t1 change name score int not null;

语法:alter table 表名 change 旧字段名 新字段名 新字段描述符

插入

全字段插入

1
insert into winton values(001,'zww'),(002,'rs');

语法:insert into 表名 values(字段1值,字段2值,……),(字段1值,字段2值,……);

个别字段插入

1
insert into winton(id) values(004);

查看插如后的结果,如上图所示。 
语法:insert inton 表名(字段名) values(值一),(值二);

普通查询

单表全字段查询

1
select * from t1;

语法:select * from 表名;

单表个别字段查询

1
select id from t1;

语法:select 字段一,字段二 from 表名;

多表查询

1
select t1.id,t1.score,winton.name from t1,winton;

语法:select 表一字段,表二字段,表三字段,…… from 表一,表二,表三,……;

条件查询

单表条件查询

1
select * from t1 where socre>90;

语法:select 字段1,字段2 from 表名 where 条件;

多表条件查询

1
select t1.id,t1.score,winton.name from t1,winton where t1.id=winton.id;

语法:select 表一字段,表二字段 from 表一,表二 where 条件;

嵌套查询

1
select name from winton where id=(select id from t1 where score=90);

语法:select 字段一,字段二…… from 表名 where 条件(查询);

并查询

1
(select id from t1 )union(select id from winton);

交查询

1
select id from t1 where id in (select id from winton);

删除

1
delete from winton where id=4;

语法:delete from 表名 where 条件;

更新

1
update t1 set score=69 where id=2;

语法:update 表名 set 更改的字段名=值 where 条件;

常用函数

求和

1
select sum(score) from t1;

注:sum(字段) 对字符串和时间无效

求平均值

1
select avg(score) from t1;

注:avg(字段)对字符串和时间无效

计数

1
select count(*) from t1;

注:count(字段名)不包含NULL;

求最大值

1
select max(name) from winton;

注:max(colunm)返回字母序最大的,返回数值最大的

求最小值

1
select min(name) from winton;

注:min(colunm)返回字母序最小值,返回数值最小值

常用的修饰符

distinct 字段中值唯一

1
select distinct name from winton;

limit查询结果数限制

1
select * from winton limit 2;

order by 排序

1
select * from winton order by name;

注:默认是升序

desc 降序

1
slelect * from winton order by name desc;

asc 升序

1
select * from winton order by name asc;

group by 分组

1
select name from winton group by name;

结尾

恩,基本能想起来的就值么多了,都是最基础,最常用的一些。

原文出处:http://blog.csdn.net/wenwen091100304/article/details/49368019

SQL基础语句汇总的更多相关文章

  1. Oracle知识梳理(三)操作篇:SQL基础操作汇总

    Oracle知识梳理(三)操作篇:SQL基础操作汇总 一.表操作 1.表的创建(CREATE TABLE): 基本语句格式:       CREATE TABLE  table_name ( col_ ...

  2. SQL基础操作汇总

    SQL基础操作汇总 一.表操作 1.表的创建(CREATE TABLE): 基本语句格式:    CREATE TABLE  table_name ( col_name    datatype, -- ...

  3. T——SQL基础语句(定义变量,赋值,取值,分支,循环,存储过程)

    T--SQL基础语句 1.定义变量: declare @变量名 数据类型 ; declare @a int ; declare @b  nvarchar(10) ; 2.赋值: 法1:set @变量名 ...

  4. sql常用语句汇总

    --创建数据库 USE yuju CREATE database YuJu on primary ( name='YuJu', filename='B:\ceshi数据库\YuJu.mdf', max ...

  5. SQL基础语句(详解版)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/CZXY18ji/article/deta ...

  6. sql基础语句50条

    curdate() 获取当前日期 年月日 curtime() 获取当前时间 时分秒 sysdate() 获取当前日期+时间 年月日 时分秒 */ order by bonus desc limit ( ...

  7. sql 基础语句

    一.基础  2  31.说明:创建数据库  4Create DATABASE database-name  5  62.说明:删除数据库  7drop database dbname  8  93.说 ...

  8. PGSQL基础语句汇总

    一.pgsql里面的数据类型不再介绍:https://www.runoob.com/postgresql/postgresql-data-type.html 二.常用基本语句 2.1.CREATE D ...

  9. SQL基础语句入门

    SQL语句入门 起因 学校开设数据库相关的课程了,打算总结一篇关于基础SQL语句的文章. SQL介绍 SQL最早版本是由IBM开发的,一直发展到至今. SQL语言有如下几个部分: 数据定义语言DDL: ...

随机推荐

  1. ab压测工具的一些个人见解

    ab压测工具(linux版)由于网上教程一大把,今天也按照教程好好研究了一番,下面写一下对此工具的一些个人见解,如有不妥,希望一起探讨.   优点: 1.小巧. 2.理论支持655350并发数.实际3 ...

  2. pat1091. Acute Stroke (30)

    1091. Acute Stroke (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One impo ...

  3. HDU 3829——Cat VS Dog——————【最大独立集】

    Cat VS Dog Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit S ...

  4. 连接数据库报错:1130-Host 'xxx' is not allowed to connect to this MySQL server解决

    出现这个问题的同学都很奇怪,为啥用localhost就可以连接上,但是使用本地ip就不行.出现这个问题的原因就是mysql未开启mysql远程访问权限导致. 这时候我们就用cmd去访问下你的mysql ...

  5. 构建web应用

    一.web服务器示例 var http = require('http'); http.createServer(function(req, res){ res.writeHeader(200, {C ...

  6. 粗看ES6之面向对象写法

    标签: es6 在es6以前,js没有类的概念,虽然有构造函数原型的方式用来做面向对向开发,但是对于书法并不是十分友好,而且对于继承实现也不是十分友好. es6引入class constructor ...

  7. 粗看ES6之函数

    标签: es6 javascript 箭头函数 ES6为了书写方便引入了函数的全新简写方式-箭头函数 <!DOCTYPE html> <html> <head> & ...

  8. 一本通 1260:【例9.4】拦截导弹(Noip1999)

    拦截导弹(Noip1999) 经典dp题目,这个做法并非最优解,详细参考洛谷导弹拦截,想想200分的做法. #include <iostream> #include <cstdio& ...

  9. 【Android学习入门】Android studio基本设置

    1.背景设置 依次选择File->Settings-->Appearance & Behaviour->Apprearance,然后勾选 show line number. ...

  10. u-boot分析(八)----串口初始化

    u-boot分析(八) 上篇博文我们按照210的启动流程,分析到了内存初始化,今天我们继续按照u-boot的启动流程对串口的初始化进行分析. 今天我们会用到的文档: 1.        2440芯片手 ...