MySQL 命令行操作教程
1.登录mysql
[root@host]# mysql -u root -p
Enter password:******
2.管理MySQL的命令
(1)SHOW DATABASES:
列出 MySQL 数据库管理系统的数据库列表。
(2)USE 数据库名 :
选择要操作的Mysql数据库,使用该命令后所有Mysql命令都只针对该数据库。
(3)SHOW TABLES:
显示指定数据库的所有表,使用该命令前需要使用 use 命令来选择要操作的数据库。
(4)SHOW COLUMNS FROM 数据表:
显示数据表的属性,属性类型,主键信息 ,是否为 NULL,默认值等其他信息。
(5)SHOW INDEX FROM 数据表:
显示数据表的详细索引信息,包括PRIMARY KEY(主键)。
(6)SHOW TABLE STATUS LIKE [FROM db_name] [LIKE \’pattern\’] \G:
该命令将输出Mysql数据库管理系统的性能及统计信息。
mysql> SHOW TABLE STATUS FROM RUNOOB; # 显示数据库 RUNOOB 中所有表的信息
mysql> SHOW TABLE STATUS from RUNOOB LIKE \'runoob%\'; # 表名以runoob开头的表的信息
mysql> SHOW TABLE STATUS from RUNOOB LIKE \'runoob%\'\G; # 加上 \G,查询结果按列打印
3.DDL 操作表
(1)创建部门表
create table t_dept(dept_id int primary key auto_increment,dept_name varchar(20) not null);
(2)创建员工表
drop table if exists t_emp; 如果这个表存在删除后重新创建
create table t_emp(emp_id int primary key auto_increment,emp_name varchar(20) not null,emp_sex char(3) not null,dept_id int not null);
(3)添加外键约束
alter table t_emp add constraint FK_EMP_DEPT foreign key(dept_id) references t_dept(dept_id);
(4)给员工表添加薪资列
alter table t_emp add(emp_salary int(5) not null);
alter table t_emp add(emp_mail varchar(30) not null);
-- 如果增加一列,并且这一列有默认值,而且是非空列
alter table t_emp add (emp_mail varchar(30) not null default \'default@mail.com\');
(5)修改列 [modify只能修改列的类型和约束默认值,change可以修改列的名及类型和约束]
alter table t_emp modify emp_salary double(6,1) not null; // 99999.9
alter table t_emp change emp_salary salary double(5,1) not null;
(6)删除列
alter table t_emp drop emp_mail;
(6重命名表名
alter table t_emp rename to tb_emp;
4.DML(数据操作语言INSERT, DELETE, UPDATE)
主要对数据库中表的数据操作