mysql的基本语句(一)

来源:互联网 发布:假面骑士数据排行 编辑:程序博客网 时间:2024/06/06 05:24

一    登录到服务器

1、 mysql –hlocalhost –uroot –p口令

2、 直接利用开始菜单!开始----程序---mysql5.0


二   命令的简单使用

下面的操作一般就是使用标准SQL命令:也就是说你在SQL SERVER里面怎么用,这里就怎么用,但是有的命令也不是完全一样的。有一点点细微的差别!下面咱们就把这个命令简单的过一遍:
 my sql 命令的使用:以分号或\g结束,\c取消一行命令,\h帮助

显示数据库
show databases;建数据库 
create database  [if not exists] 数据库名;建数据表create table [if not exists] 表名 (字段名1   类型。。。。。。。。) create table student (编号 int auto_increment primary key, 姓名 varchar(10));显示数据表:show tables;删除库drop database [if exists] 库名; 删除表:
drop table   [if exists] 表名;显示表结构desc 表名增长一个字段alter table 表名 add 字段名  类型删除一个字段alter table  表名 drop 字段名修改一个字段的属性alter table 表名modify 字段 新属性增加一个主键                                alter table 表名 add primary key(字段名)  删除一个主键alter table 表名  drop primary key(字段名)


三  有关于数据库的操作

show databases;              //显示所有数据库

use userpro_db;              //使用哪个数据库

show tables;                 //显示数据库下的表


四  表的操作

创建表:Create TABLE mytable (name VARCHAR(20), sex CHAR(1),birth DATE, birthaddr VARCHAR(20));

选择列:select * from table where 范围

插入数据:insert into table(field1,field2) values(value1,value2)

删除数据:delete from table where 范围

更新数据:update table set field=value1 where 范围

模糊查找:select * from table where field like '%value%' ---like的语法很精妙,查资料!

数据排序:select * from table order by field1,field2 [desc]

数据总数:select count as totalcount from table

数据求和:select sum(field) as sumvalue from table

数据平均:select avg(field) as avgvalue from table

最大数据:select max(field) as maxvalue from table

最小数据:select min(field) as minvalue from table


五  用户的操作

新建用户

(1)、创建用户(会使用到mysql数据库)

     mysql> insert into mysql.user(Host,User,Password)values('localhost','nine',password('mirchd'));
(2)、刷新系统权限表

     mysql> flush privileges;
(3)、修改用户密码

     mysql> update mysql.user setpassword=password('123') where User='nine' andHost='localhost';
(4)、授权nine用户拥有tree数据库的所有权限

     mysql> grant all privileges on tree.* tonine@localhost identified by '123';

删除用户

     mysql> delete from user where User='nine' andHost='localhost';
最好每一步都刷新系统权限表


0 0
原创粉丝点击