针对常用命令直接实践掌握
一、基础命令
1.启动登录
1 2
| service mysqld start //启动mysql mysql -u root -p password //登陆root password
|
2.建立新用户(grant命令)
格式:
grant all privileges on 数据库.* to 用户名@登录主机 identified by “密码”;
1 2
| grant all privileges on shopex.* to test@localhost identified by "1234";
|
增加一个用户test密码为”1234”,让他只可以在localhost上登录,并可以对数据库Shopex进行所有的操作(localhost指本地主机,即MYSQL数据库所在的那台主机)
3.操作数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| create database student_management; //创建数据库 show databases; use student_management; show table;
create table student_info(stu_id int,name varchar(8),sex varchar(4),class varchar(16)); //创建student_info表
create table teacher_info(tea_id int primary key,name varchar(8),sex varchar(4) check(sex=’男’ or sex=’女’),join_time date,nationality varchar(8) default ’汉族’) //主键、约束、默认值
desc student_info; //查看表结构
|
二、对表数据操作
1.增删改
1 2 3 4 5 6 7 8 9
| insert into student_info values(20080201,’张帅’,’男’,’计算机软件’); //增加一行数据
update student_info set class=’计算机软件’ where name=’王青’; //修改一行数据
delete from student_info where name=’张玲’; //删除一行数据
|
2.查询
1 2 3 4 5 6 7 8 9 10 11 12
| select * from student_info order by stu_id; //order by子句:按顺序排序
select stu_id,class from student_info where name=’李飞’; //where子句:过滤记录
select count(*) from student_info; //count()函数:计数
select name from student_info where name like '张%'; //like操作符:模式(模糊)匹配,"%"符号用于在模式的前后定义通配符(缺省字母)
|
三、对表结构操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| alter table student_info add tel_num int(11); //添加字段
alter table student_info change name stu_name varchar(8); //修改字段名称
alter table student_info modify stu_id int primary key; //修改字段属性
alter tavle student_info modify tel_num int(22); //同上
alter table student_info drop column tel_num; //删除字段
drop table course_info; //删除表
drop database student_management; //删除数据库
|