mysql CRUD

Mysql SQL语句语法
[url]http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html[/url]
MySQL Command-Line Tool:
[url]http://dev.mysql.com/doc/refman/5.6/en/mysql-command-options.html[/url]

命令行下登陆某server:

$ mysql -u myusername -h hostname -pmypassword
-u 指定用户名,-h 指定 host,-P 指定端口(未指定则默认3306),-p 指定密码;需要注意的是,前三个的具体值和其参数名间可以有空格,也可以没有;但是 -p 和具体密码值之间不可以有空格!如果有空格的话,则其后的密码不能够被正确解析,回车后 mysql 会再度提示你输入密码。详见:
[url]http://dev.mysql.com/doc/refman/5.6/en/mysql-command-options.html#option_mysql_password[/url][quote] --password[=password], -p[password]
The password to use when connecting to the server. [b][color=red]If you use the short option form (-p), you cannot have a space between the option and the password.[/color] If you omit the password value following the --password or -p option on the command line, mysql prompts for one.[/b][/quote]


新用户创建 & grant:
[url]https://www.digitalocean.com/community/articles/how-to-create-a-new-user-and-grant-permissions-in-mysql[/url]

$ mysql -uroot -p
mysql> CREATE USER 'scott'@'localhost' IDENTIFIED BY 'tiger';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'scott'@'localhost';
FLUSH PRIVILEGES;


使用某库
use dbName;


查看所有数据库
show databases;


查看当前连接库的所有表
show tables;


查看当前连接的是哪个库
select database();


查看所有用户
select * from mysql.user;


查看当前连接的用户是谁
select user();
select current_user();


增加列
alter table tbl_name add col_name type


删除列
alter table tbl_name drop col_name 


改变列
alter table tbl_name modify col_name type


给表更名
alter table tbl_name rename new_tbl_name



[b]change[/b],既可以用来重命名列,也可以用来改变列类型。
[quote]您可以使用CHANGE old_col_name column_definition子句对列进行重命名。重命名时,需给定旧的和新的列名称和列当前的类型。例如:要把一个INTEGER列的名称从a变更到b,您需要如下操作:
mysql> ALTER TABLE t1 CHANGE a b INTEGER;
如果您想要更改列的类型而不是名称, CHANGE语法仍然要求旧的和新的列名称,即使旧的和新的列名称是一样的。例如:
mysql> ALTER TABLE t1 CHANGE b b BIGINT NOT NULL;[/quote]

[/code]

[code="sql"]


[/code]

[code="sql"]


[/code]

[code="sql"]


[/code]

[code="sql"]


[/code]


Mysql时间函数:
[url]http://www.taobaodba.com/html/234_mysql_date_func.html[/url]
now()函数以`yyyy-mm-dd hh:mm:ss返回当前的日期时间,可以直接存到datetime字段中。
curdate()以’yyyy-mm-dd’的格式返回今天的日期,可以直接存到date字段中。
curtime()以’hh:mm:ss’的格式返回当前的时间,可以直接存到time字段中。

group by range in mysql:
[url]http://stackoverflow.com/questions/6687534/group-by-range-in-mysql[/url][quote]区间为 0-9,10-19(10落在range的区间下限上),20-29,...[code="sql"]select concat(10*floor(rule_count/10), '-', 10*floor(rule_count/10) + 9) as 'range', rule_count from pymk_log_details order by rule_count;
区间为 1-10(10落在range的区间上限上),11-20,21-30,...
select concat(10*ceil(rule_count/10)-9, '-', 10*ceil(rule_count/10))  as 'range', rule_count from pymk_log_details order by rule_count;
[/quote]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值