mysql,sql语句大全_mysql数据库sql语句大全_第1页
mysql,sql语句大全_mysql数据库sql语句大全_第2页
mysql,sql语句大全_mysql数据库sql语句大全_第3页
mysql,sql语句大全_mysql数据库sql语句大全_第4页
已阅读5页,还剩6页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、mysql,sql语句大全_mysql数据库sql语句大全 mysql sql语句大全 mysql 是一个关系型数据库,由瑞典 mysql ab 公司开发,目前属于 oracle 旗下公司。 mysql默认超级管理员是root,一般在安装mysql的时候会提示设置root密码。 前期的mysql不支持存储过程,在mysql5.0之后,设置存储引擎engine=innodb才能支持存储过程。 ddl数据定义语言(create,alter,drop,declare) dml数据操纵语言(select,delete,update,insert) dcl数据掌握语言(grant,revoke,comm

2、it,rollback) 常见sql语句 1、创建用户创建用户一般都是登录超级管理员root,进行创建之后安排权限。 create user usernamehost identified by password; username - 你将创建的用户名。host - 指定该用户在哪个主机上可以登陆,假如是本地用户可用localhost,假如想让该用户可以从任意远程主机登陆,可以用法通配符%。password - 该用户的登陆密码,密码可以为空,假如为空则该用户可以不需要密码登陆服务器。 2、授权 grant privileges on databasename.tablename to us

3、ernamehost; privileges - 用户的操作权限,如select , insert , update,delete 等.假如要授予所的权限则用法all。databasename - 数据库名,tablename-表名,假如要授予该用户对全部数据库和表的相应操作权限则可用表示, 如.(对全部数据库全部表相应权限),datebasename.(对指定数据库全部表相应权限)。 留意:用以上指令授权的用户不能给其它用户授权,假如想让该用户可以授权,用以下指令: grant privileges on databasename.tablename to usernamehost with

4、 grant option; 3、设置与更改用户密码 set password for usernamehost = password(newpassword); 假如是当前登录用户用: set password = password("newpassword"); 4、撤销用户权限 revoke privilege on databasename.tablename from usernamehost; 留意: 假如你在给用户username%授权的时候是这样的(或类似的):grant select on testdb.user to username%, 则在用法rev

5、oke select on . from username%;指令并不能撤销该用户对testdb数据库中user表的select 操作。相反,假如授权用法的是grant select on . to username%;则revoke select on testdb.user from username%;指令也不能撤销该用户对testdb数据库中user表的select 权限。 详细信息可以用指令show grants for username%; 查看。 5、删除用户 drop user usernamehost; 6、创建数据库-databasename - 数据库名 create d

6、atabase databasename; 7、删除数据库-databasename - 数据库名 drop database databasename; 8、修改数据库名 alter database 旧名称 modify name = 新名称 9、备份sql server 创建 备份数据的deviceuse master exec sp_addumpdevice disk, testback, e:mysqltest.dat开头 备份backup database databasename to testback语句1:打开系统数据库master,或者说在系统数据库下操作。 语句2:定义备

7、份规律设备名testback,类型disk,路径为e:mysqltest.dat。 语句3:备份数据库databasename到设备testback上。 10、创建新表 create table tablename(col1 type1 not null primary key,col2 type2 not null,.); 依据已有的表创建新表: a:create table tab_new like tab_old (用法旧表创建新表) b:create table tab_new as select col1,col2 from tab_old definition only #用户表

8、create table if not exists t_user( u_id bigint unsigned auto_increment not null comment 编号, u_phone bigint not null comment 账号-手机号, u_passwd varchar(100) not null comment 密码,4-20字符,md5加密, u_regtime bigint unsigned not null comment 注册时间,时间戳, u_check_uid bigint unsigned comment 用户类型审核人员id, primary key

9、(u_id) comment 主键, unique key(u_phone) comment 唯一键, foreign key(u_check_uid) references t_user(u_id) on delete cascade, key index_regtime(u_regtime) comment 注册时间-索引 )engine=innodb default charset=utf8 comment 用户表; 11、删除新表 drop table tablename 12、增加一个列 alter table tablename add column col type 留意:列增加

10、后将不能删除。db2中列加上后数据类型也不能转变,唯一能转变的是增加varchar类型的长度。 13、添加主键 alter table tablename add primary key(col) 14、删除主键 alter table tablename drop primary key(col) 15、创建索引 create unique index idxname on tablename(col.) 16、删除索引 drop index idxname 17、创建视图 create view viewname as select statement 18、删除视图 drop view

11、viewname 19、几个简洁的基本的sql语句 查询: select * from tablename where 范围 插入: insert into tablename(field1,field2) values(value1,value2) 删除: delete from tablename where 范围 更新: update tablename set field1=value1 where 范围 查找: select * from tablename where field1 like %value1% 排序: select * from tablename order by

12、 field1,field2 desc 总数: select count(field1) as totalcount from tablename 求和: select sum(field1) as sumvalue from tablename 平均: select avg(field1) as avgvalue from tablename 最大: select max(field1) as maxvalue from tablename 最小: select min(field1) as minvalue from tablename 20、几个高级查询运算词a: union 运算符 u

13、nion运算符通过组合其他两个结果表(例如 table1 和 table2)并消去表中任何重复行而派生出一个结果表。当 all 随 union 一起用法时(即 union all),不消退重复行。两种状况下,派生表的每一行不是来自 table1 就是来自 table2。 b: except 运算符 except 运算符通过包括全部在 table1 中但不在 table2 中的行并消退全部重复行而派生出一个结果表。当 all 随 except 一起用法时 (except all),不消退重复行。 c: intersect 运算符 intersect 运算符通过只包括 table1 和 table

14、2 中都有的行并消退全部重复行而派生出一个结果表。当 all 随 intersect 一起用法时 (intersect all),不消退重复行。 注:用法运算词的几个查询结果行必需是全都的。 21、用法外连接 a、left outer join: 左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的全部行。 b:right outer join: 右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的全部行。 c:full outer join: 全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的全部记录。22、复制表(只复制结构,源表名:a 新表名:b) (

15、access可用) 方法一: select * into b from a where 11 方法二: select top 0 * into b from a 23、拷贝表(拷贝数据,源表名:a 目标表名:b) (access可用) insert into b(a, b, c) select d,e,f from b; 24、跨数据库之间表的拷贝(详细数据用法肯定路径) (access可用) insert into b(a, b, c) select d,e,f from b in 详细数据库 where 条件 25、子查询(表名1:a 表名2:b) select a,b,c from a

16、where a in (select d from b ) 或者: select a,b,c from a where a in (1,2,3) 26、between的用法,between限制查询数据范围时包括了边界值,not between不包括 select * from table1 where time between time1 and time2 select a,b,c, from table1 where a not between 数值1 and 数值2 27、in 的用法方法 select * from table1 where a not in (值1,值2,值4,值6)

17、 28、两张关联表,删除主表中已经在副表中没有的信息 delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1) 29、四表联查问题 select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where . 30、日程支配提前五分钟提示 select * from 日程支配 where datediff(minute,f开头时间,

18、 getdate()5 31、一条sql 语句搞定数据库分页(不推举) select top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段 32、前10条记录 select top 10 * form table1 where 范围 33、随机取出10条数据 select top 10 * from tablename order by newid() 34、随机选择记录 select newid() 35、删除重复记录 de

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论