一些自己常用的mysql命令

来源:互联网 发布:网络制式英文 编辑:程序博客网 时间:2024/05/16 23:40

mysql的安装:

我安装mysql不是支官网下载的安装包,而是使用的是

sudo apt-get install mysql-server

这样非常得方便。

mysql的启动,停止,重启:

/etc/init.d/mysql start

最后可以是stop,restart

创建新表,插入数据,删除数据,删除表:

创建新表   CREATE TABLE 表名(域名 数据类型 列选项[...]);

mysql>create table customer(c_id char(5) primary key, c_name varchar(20),c_birth date,c_sex char(1)

DEFAULT '0');

CREATE TABLE 命令中可以使用的主要选项

选项说明:

AUTO_INCREMENT  定义自增序列

DEFAULT ‘默认值’  定义列的默认值

INDEX  定义索引

[NOT]NULL  允许/禁止NULL值

PRIMARY KEY  定义列主键

UNIQUE  定义唯一性

CHECK 定义可以输入值的范围/选项

插入数据:
语法:INSERT [INTO] tbl_name [(col_name,...)] VALUES (pression,...),…

删除某一行数据:
mysql>delete from 表名 where id=1;

删除某个表:
drop table table_name;

对于列的操作:

如果想在一个已经建好的表中添加一列,可以用诸如:
alter table table_name add column column_name varchar(20) not null;

这条语句会向已有的表t1中加入一列addr,这一列在表的最后一列位置。如果我们希望添加在指定的一列,可以用:
alter table table_name add column column_name varchar(20) not null after user1;

注意,上面这个命令的意思是说添加addr列到user1这一列后面。如果想添加到第一列的话,可以用:
alter table table_name add column column_name varchar(20) not null first;


创建新用户并赋予权限:

用户可以在任何地方登录:

create user username identified by 'password';

用户在指定的ip登录:

create user username@ip identified by 'password';


grant all privileges on *.* to test@localhost identified by 'test' with grant option;
这句增加一个本地具有所有权限的test用户(超级用户),密码是test。ON子句中的*.*意味着"所有数据库、所有表"。with grant option表示它具有grant权限。

grant select,insert,update,delete,create,drop privileges on test.* to test1@'192.168.1.0/255.255.255.0' identified by 'test';
这句是增加了一个test1用户,口令是test,但是它只能从C类子网192.168.1连接,对test库有select,insert,update,delete,create,drop操作权限。
用grant语句创建权限是不需要再手工刷新授权表的,因为它已经自动刷新了。

0 0