之前很少使用,但是换了新的工作,虽然还在远程办公,但发现用到的比较多,所以花半天时间学习下基本语言,主要是从w3school中学习(后续会逐步补充)

1、sql主要分为两个部分:dml和ddl。DML主要是查询和更新,都是处理表中数据,包含select、update、insert、delete(删除表中数据);DDL更上一层,对表与表之间关系进行处理,如drop(删除表)、create、alter。

半天时间学习SQL语言

2、select:select Company from my_table从表中选取Company对应的列;结果:ibm、w3school、apple、w3school

                   select * from my_table从表中选取所有的列;结果:原表

                   select distinct Company from my_table从表中选取Company对应的列并去重;结果:ibm、w3school、apple

                   select a.Company from my_table as a:把table临时命名为a

                   select Company as "公司" from my_table:把字段名改变

                   select Company,OrderNumber into table2 from table1:从表1中选择某些列存入表2中;如果table2在别的数据库,table2后面加入“in 数据库名称”

                   group by:对元素进行整合;select Company,sum(orderNumber) from my_table group by Company

                   having:select Company,sum(orderNumber) from my_table group by Company having sum(orderNumber)>100

3、where:在select的时候添加限制条件。常用关系有<>(不等于)、between、=、>、<等,数值不用引号,字符串需要引号;

                    and和or就是条件之间的并列了。

4、order by:取出的数据按照顺序排序。select * from table order by Company desc,orderNumber asc:Company按降序排列,orderNumber按升序排列。

5、insert into:1、insert into my_table values(string1,string2),插入一行新的数据

                         2、insert into my_table(Company)values(string1),对指定列插入数据

6、update:1、update my_table set Company=新值 where Company=旧值(改变原来旧值)

                    2、update my_table set Company=新值,orderNumber=新值 where Company=旧值(改变某一行多列值)

7、delete:1、delete from my_table where Company=apple:删除apple这一行

                   2、delete * from my_table:不删除表,清空数据,或者delete from my_table

8、limit:限制选取多少行

9、rlike:匹配正则

      not rlike:不能匹配

      rlike ‘%b’以b结尾,‘b%’以b开头,‘%b%’中间包含b。%匹配一个或者多个字符

      _一个字符;[abc]中的任一个;[!abc]=[^abc]不能是abc中的任一个

10、in:select * from my_table where Company in (string1,string2):Company值是string1或者2都应该被选取

11、inner join:select a,b,c from table1

                         inner join table2

                         on a = table2.a

      left join:返回table1中所有行,即使table2中没有匹配到

      right join:与上面相反

      full join:只要其中一个表存在匹配就返回行(就是left和right的并集)

12、union:select string1 from table1 union string2 from table2:求并集去重;union all的话不去重;两者必须相同的列且每一列的字段名相同

13、create database my_db:创建名称为my_db的数据库

        drop database my_db删除库

        create table my_table:创建名称为my_table的表

        create table if not exists my_table lifecycle 33 as:创建名称为my_table的表,存在周期为33天

        create table my_table {

             列名称1 数据类型,(int、char、varchar(255)、date(yyyymmdd))(int not null unique check (列名称1>0)

             列名称1 数据类型(varchar(255)表示字符最大长度是255)

             }            

          drop table my_table删除表

          truncate table my_table只删除表中数据而不删除表,和delete * from my_table功能相似         

14、约束:用于限制加入表的数据类型

        not null:相当于不接受空值

        unique:约束唯一标示数据库表中的每条记录;

                      已建表时:alter table my_table add unique(列名称1) 

                      撤销时:alter table my_table drop unique ,貌似不对的

        primary key:和unique类似

        foreign key:不是很明白,以后用到了再细读

        check:对值进行约束,如列名称1的数据类型是int,check(列1 > 0),对值范围进行规定

        default:不是很明白,以后用到了再细读

15、create index:create index index_table1 on table1(string1):对string1建立索引,多列用逗号隔开,加上dec就是降序

           drop index index_table1 on table1删除索引,drop index table1.index_table1,alter table table1 drop index index_table1

16、drop:删除索引、表或者数据库

17、alter:在表中添加、修改或删除

             添加:alter table my_table add string1 datatype1(增加新一列,数据类型是1)

             删除:alter table my_table drop column string1(删除string1这列)

             修改:alter table my_table alter column string1 datatype2(数据类型1改成2)

下面的主要是函数:

avg(colmun):某列均值;select string2 from my_table where string1>(select avg(string1) from my_table)

count(colmun):某列行数(不包含null的行)

count(distinct colmun):某列行数(不包含null的行,且去重了)

count(*):被选行数

max(cloumn):指定列中最大值

min(cloumn):指定列中最小值

sum(column):指定列总和

ucase(column):变成大写;select ucase(string1) as string1, string2 from my_table

lcase(column):变成小写

mid(column,start,length):对该列元素进行截取

len(column):该列元素长度;select len(string1) as len_string1 from my_table

round(column,1):该列元素小数点保留1位;select round(string1) as unit_string1 from my_table

now():当前时间;select string1,string2,now() as date from my_table;原表中抽取两列并加入一列当前时间

format(column,格式):对该列数据转变为固定格式;select string1,format(now(),'YYYY-MM-DD') as date from my_table

 

 

 

 

相关文章: