1、select
基本语法格式:select 列名 from 表名
示例:select ftp_port from cps_site_destination_tab
2、distinct
基本语法格式:select distinct 列名 from 表名
示例:select distinct FTP_PORT from cps_site_destination_tab
3、where
基本语法格式:select 列名 from 表名 where 条件
示例:select ftp_port from cps_site_destination_tab where ftp_port>'30'
4、and 或 or
基本语法格式:select 列名 from 表名 where 简单条件1 {[and | or] 简单条件2}+
{}+表示{}内的情况会发生一次或多次。语句块用()标识
示例:select ftp_port from cps_site_destination_tab where ftp_port>'30' or (ftp_port<'23' and ftp_port>'21')
5、in
基本语法格式:select 列名 from 表名 where 列名 in (‘数值1’,‘数值2’,.......)
如果只有一个数值就和”select 列名 from 表名 where 列名=‘数值1’“一样
示例:select ftp_port from cps_site_destination_tab where ftp_port in ('21','22','23','24','25')
6、between
基本语法格式:select 列名 from 表名 where 列名 between ‘数值1’ and ‘数值2’
和in类似,只是in代表的是散列的值,between表示一个连续范围
示例:select ftp_port from cps_site_destination_tab where ftp_port between '25' and '32'
7、like
基本语法格式:select 列名 from 表名 where 列名 like {模式}
示例:select ftp_user from cps_site_destination_tab where ftp_user like 's__f'
查询出以s开头,中间为任意两个字符,以f结尾的字符串
select ftp_user from cps_site_destination_tab where ftp_user like 'sd%'
查询出以sd开头的任意字符串
select ftp_user from cps_site_destination_tab where ftp_user like '%f'
查询出以f结尾的任意字符串
select ftp_user from cps_site_destination_tab where ftp_user like '%d%'
查询出中间包含d的任意字符串
8、order by
基本语法格式:select 列名 from 表名 [where 条件] order by [asc 或 desc]
[]中where语句非必须的,但是如果where语句被需要,必须放在order by之前。asc由小到大排列,desc为由大到小排列
示例:select ftp_port,ftp_user from cps_site_destination_tab order by 1 desc (1代表ftp_port,2代表ftp_user)
9、函数
基本函数:
AVG(求平均数)
COUNT(计数)
MAX(求最大值)
MIN(求最小值)
SUM(求和)
基本语法格式:select 函数名(列名) from 表名
示例:select avg(ftp_port) from cps_site_destination_tab
求平均数
select sum(ftp_port) from cps_site_destination_tab
求总数
select count(ftp_port) from cps_site_destination_tab
求符合条件行的计数
select min(ftp_port) from cps_site_destination_tab
求最小值
select max(ftp_port) from cps_site_destination_tab
求最大值
10、NULL
基本语法格式:select 列名 from 表名 where 列名 is [null 或 not null]
示例:select remark from cps_site_destination_tab where remark is null
查询remark列为空的所有行
select remark from cps_site_destination_tab where remark is not null
查询remark列不为空的所有行
11、group by
基本语法格式:select 列1,函数名(列2) from 表名 group by 列1
把列1分组,按照列1的分组,计算列2
示例:select ftp_user,sum(ftp_port) from cps_site_destination_tab group by ftp_user
12、having
基本语法格式:select [列1],函数名(列2) from 表名 [group by 列1] having 函数条件
如果select只有函数栏,那么group by也可不要。因为where语句不适合此用法,因此必须要用having处理
示例:select ftp_user,sum(ftp_port) from cps_site_destination_tab group by ftp_user having sum(ftp_port)='42'
13、alias
基本语法格式:select 表别名.列名 列别名 from 表名 表别名
示例:select csite.ftp_user fuser from cps_site_destination_tab csite
14、表格内部连接
基本语法格式:select 表1别名.列名,函数名(列名) from 表1 表1别名,表2 表2别名 where 表1别名.列名=表2别名.列名 group by 列名
利用表1和表2中相同的列作为连接通道,把两个表中的内容关联起来。显示两个表格中共有的行条目
示例:select a1.movies_name,count(drama_name) from h_movies a1, h_movies_drama a2 where a1.movies_id=a2.movies_id group by movies_name
15、表格外部连接
基本语法格式:select 表1别名.列名,函数名(列名) from 表1 表1别名,表2 表2别名 where 表1别名.列名=表2别名.列名(+) group by 列名
利用表1和表2中相同的列作为连接通道,把两个表中的内容关联起来。显示所有的条目
示例:select a1.movies_name,count(drama_name) from h_movies a1, h_movies_drama a2 where a1.movies_id=a2.movies_id(+) group by movies_name
16、concat基本语法格式:select concat(列1,列2) from 表1 where 条件 mysql用法
把列1和列2连接到一起,作为一个串
select 列1 || ‘ ’ || 列2 from 表1 where 条件 oracle用法
把列1和列2连接到一起,作为一个串
select 列1+‘ ’+列2 from 表1 where 条件 sql server用法
把列1和列2连接到一起,作为一个串
示例:select concat(movies_name,movies_director) from h_movies where movies_id='42' mysql
select ftp_ip||':'||ftp_port||' '||ftp_user from cps_site_destination_tab where ftp_user='admin' oracle
17、substr
基本语法格式:select substr(列名,位置,个数) from 表名 where 条件
选择符合条件的列,并从当前位置处截取指定个数的字符
示例:select substr(movies_name,2,1) from h_movies where movies_id='17'
18、trim
基本语法格式:select trim(‘字符串’),select rtrim('字符串'),select ltrim('字符串')
示例:select trim(' adfdsfsd ')
select ltrim(' adfdsfsd ')
select rtrim(' adfdsfsd ')
19、create table
基本语法格式:create table 表名
(
列1 类型,
列2 类型,
列3 类型,
)
示例:create table wcl_test
(
name char(20),
passwd char(20),
ip char(20),
port int(20)
)
20、表格限制
not null :限制表中指定列非空
基本语法格式:
create table 表名
(
列1 类型 not null,
列2 类型 not null,
列3 类型
)
示例:
create table test_NotNull
(
name char(20),
passwd char(20) not null,
ip char(20)
)
unique:限定表格中指定列唯一
基本语法格式:
create table 表名
(
列1 类型 unique,
列2 类型 not null,
列3 类型
)
示例:
create table test_Unique
(
name char(20) unique,
passwd char(20) not null,
ip char(20)
)
check:限制保证某列的所有数据符合某一条件
基本语法格式:
create table 表名
(
列1 类型 unique,
列2 类型 not null,
列3 类型 check(条件)
)
示例:
create table test_Check
(
name char(20) unique,
passwd char(20) not null,
port char(20) check(port > 1024)
)
主键:唯一标识一个表,当主键包含多个列时,称为组合主键
基本语法格式:
create table 表名
(
列1 类型 ,
列2 类型 not null,
列3 类型 check(条件),
primary key(列1)
) ---------------------------------mysql用法
create table 表名
(
列1 类型 primary key,
列2 类型 not null,
列3 类型 check(条件)
) ------------------------------------oracle用法
示例:
create table test_PrimaryKey
(
name char(20) unique,
passwd char(20) not null,
port char(20) check(port > 1024),
primary key (name)
)
外键:执行另外一个表格的主键。只有外连主键的表中存在时,才能录入外键存在的表
基本语法格式:
create table 表名
(
列1 类型 ,
列2 类型 not null,
列3 类型 check(条件),
primary key(列1),
foreign key(列3) references 表1(表1中的列)
) ---------------------------------mysql用法
create table 表名
(
列1 类型 primary key,
列2 类型 not null,
列3 类型 check(条件) references 表1(表1中的列)
) ------------------------------------oracle用法
示例:
create table test_foreignkey
(
name char(20) unique,
passwd char(20) not null,
port char(20) check(port > 1024),
primary key (name) ,
foreign key(port) references test_Unique(name)
)
21、create index
基本语法格式:create index 索引名称 on 表名(列1,列2)
把指定列作为索引,可以增加查找速度
示例:create index IDX_unique_name on test_Unique(name,ip)
22、alter table
基本语法格式:alter table 表名 add 列名 类型
alter table 表名 drop 列名
alter table 表名 change 原列名 新列名 新类型
alter table 表名 modify 列名 类型
示例:
alter table wcl_test add path char(200)
alter table wcl_test drop ip
alter table wcl_test change path ip char(50)
alter table wcl_test modify ip int(11)
23、drop table
基本语法格式:drop table 表名
删除表
示例:drop table wcl_test
24、truncate table
基本语法格式:truncate table 表名
清空表
示例:truncate table h_movies
25、insert into
基本语法格式:insert into 表名(列1,列2,列3,......) values ('值1','值2',‘值3’,......)
向表中插入数值
insert into 表1(列1,列2,列3,......) select 列a,列b,列c,...... from 表2
把表2中的数据插入到表1中
示例:insert into ftp (username,passwd,ip,port,path) values ('tester','123456','192.168.1.2.3','21','/home/tester')
insert into vftp(vusername,vpasswd,vip,vport,vpath) select username,passwd,ip,port,path from ftp
26、update
基本语法格式:update 表名 set 列=新值 where 条件
更新符合条件的值
示例:update ftp set ip='1.1.1.1' where username='tester'
修改指定子串:update [表名] set [字段名] = REPLACE ( [字段名] , '要替换掉的部分' , '替换成的字符串' ) where (条件)
示例:update `bms_movie_bitrat_tab` set play_url=REPLACE(play_url,'http://192.168.7.40:1935','http://192.168.7.229:1935') where play_url like '%192.168.7.40%';
27、delete
基本语法格式:delete from 表名 where 条件
删除符合条件的值
示例:delete from ftp where path='' /删除path列为空的数据
28、union
基本语法格式:select 列名 from 表1 union select 列名 from 表2
查询出表1和表2中不重复的所有数据
示例:select username from ftp union select vusername from vftp
29、union all
基本语法格式:select 列名 from 表1 union all select 列名 from 表2
查询出表1和表2中所有的数据
示例:select username from ftp union all select vip from vftp
39、use
基本语法格式:use 数据库名称
进入指定的数据库
示例:use mysql; /进入mysql的基础库,配置基础用户;刚安装的数据库需要用此命令添加用户
40、添加远程用户
1、连接登陆服务器后,输入:mysql -uroot -p 切换到mysql模式
2、输入:GRANT all on *.* to root@"%" identified by "123456";
3、输入:GRANT all on *.* to root@localhost identified by "123456";
4、输入:flush privileges;
5、exit
(注意:被连接的电脑要关闭防火墙)
# /etc/init.d/mysql stop
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
# mysql -u root mysql
mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
{SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpwd');}
mysql> FLUSH PRIVILEGES;
mysql> quit
# /etc/init.d/mysql restart
# mysql -uroot -p
Enter password: <输入新设的密码newpassword>
default-character-set = utf8
安装mysql
yum install mysql-server
删除mysql
yum remove mysql