连接mysql
mysql -h host_name -p -u user_name
退出mysql
mysql> quit
当前mysql信息
mysql> Select Now(),User(),Version();
mysql> Select Now(),User(),Version()\g // ; \g 终止语句
mysql> Select Now(),User(),Version()\G //竖排显示,每行一个值
脚本执行
% mysql < myscript.sql
% mysql sampdb < create_member.sql
简化链接过程
1. ~/.my.cnf
[client]
host=server_host
user=your_name
password=your_pass
2. 命令历史
% !my //最近使用过的命令
3. alias sampdb ‘mysql -h host_name -p -u sampadm sampdb’
聚合函数,MIN(),MAX(),SUN(),AVG()
数据库操作
创建数据库
CREATE DATABASE [IF NOT EXISTS] db_name [CHARACTER SET charset][COLLATE collation];
删除数据库
DROP DATABASE db_name;
变更数据库
ALTER DATABASE [db_name][CHARACTER SET charset][COLLATE collation];
表基本操作
创建数据表
mysql> CREATE TABLE member
(
member_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY (member_id),
first name VARCHAR(20) NOT NULL,
last name VARCHAR(20) NOT NULL,
suffix VARCHAR(5) NOT NULL,
expiration DATE NULL
);
查看表结构
mysql> DESCRIBE member;
显示特定列
mysql> SHOW COLUMNS FROM member LIKE '%name';
列出表
mysql> SHOW TABLES;
% mysqlshow sampdb
列出数据库
mysql> SHOW DATABASE;
% mysql show
插入数据行
mysql> CREATE TABLE student
(
name VARCHAR(20) NOT NULL,
sex ENUM('F','M') NOT NULL,
student_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY (student_id)
)ENGINE = InnoDB
mysql> INSERT INTO student VALUES('Alex','M',NULL);
mysql> INSERT INTO student VALUES('Alex','M',NULL),('Bob','F',NULL); //括号内包含所有列
mysql> INSERT INTO student (name,sex) VALUES('Alex','M');
加载数据文件
mysql> LOAD DATA LOCAL INFILE 'member.txt' INTO TABLE member;
% mysqlimport --local samdb member.txt
检索信息
书定顺序,Select–From–Where–Group by–Having–Order by
执行顺序,From–Where–Group by–Having–Select–Order by
NULL值表示’无数据’,不能与’有数据’的值比较
LIMIT number,限制显示行
日期函数
YEAR(),MONTH(),DAYOFMONTH()
变量,@variable
mysql> SELECT @birth:=birth FROM student where name='Bob';
mysql> SELECT name FROM student WHERE birth<@birth ORDER BY birth;
mysql> SET @today=CURDATE();
mysql> SET @one_week_ago:=DATE_SUB(@today,INTERVAL 7 DAY);
统计信息
DISTINCT,清除重复信息
COUNT(*),计数所有行
COUNT(数据列),非NULL行
mysql> SELECT state,COUNT(*) FROM president GROUP BY status ORDER BY count DESC LIMIT 4;
多表联结
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录
right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录
inner join(等值连接) 只返回两个表中联结字段相等的行
exists 与 in 最大的区别在于 in引导的子句只能返回一个字段,比如:
select name from student where sex = ‘m’ and mark in (select 1,2,3 from grade where …),
in子句返回了三个字段,这是不正确的,exists子句是允许的,但in只允许有一个字段返回,在1,2,3中随便去了两个字段即可
删除数据行
DELETE FROM tbl_name WHERE which rows to delete;
更新数据行
UPDATE tbl_name SET which columns to change WHERE which rows to update;
获取数据库对象的元数据
1)show语句
show databases; –列出所有数据库
show create database db_name; –查看数据库的DDL
show tables; –列出默认数据库的所有表
show tables from db_name; –列出指定数据库的所有表
show table status; –查看表的描述性信息
show table status from db_name;
show create table tbl_name; –查看表的DDL
show columns from tbl_name; –查看列信息
show index from tbl_name; –查看索引信息
show columns from domaininfo like ‘s%’;
show columns from domaininfo where field=’sysdomain’;
2)从INFORMATION_SCHEMA数据库里查询相关表
SHOW TABLES IN INFORMATION_SCHEMA;
3)命令行程序,mysqlshow, mysqldump
mysqlshow –列出所有数据库
mysqlshow db_name –列出给定数据库的所有表
mysqlshow db_name tbl_name –列出给定数据库表的所有列
mysqlshow –keys db_name tbl_name –列出索引信息
mysqlshow –status db_name –列出数据库的描述性信息
mysqldump可以让你看到create table语句(就想show create table语句一样),如:
mysqldump –no-data db_name [tbl_name] …