1.登录mysql:
mysql -h 主机名 -u 用户名 -p 密码mysql
2.建立数据库
create database 数据库名 //create database my_sql
3.选择所要操做的数据库
(1).在登陆数据库时指定, 命令: mysql -D 所选择的数据库名 -h 主机名 -u 用户名 -p
例如登陆时选择刚刚建立的数据库: mysql -D samp_db -u root -psql
(2). 在登陆后使用 use 语句指定, 命令: use 数据库名;
use 语句能够不加分号, 执行 use samp_db 来选择刚刚建立的数据库, 选择成功后会提示: Database changed数据库
4.建立数据库表
create table 表名称(列声明);
5.向表中插入数据
insert 语句能够用来将一行或多行数据插到数据库表中, 使用的通常形式以下:
insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);
insert into students values(NULL, "王刚", "男", 20, "13811371377");
6.查询表中的数据
select 列名称 from 表名称 [查询条件]
例如要查询 students 表中全部学生的名字和年龄, 输入语句 select name, age from students;
也能够使用通配符 * 查询表中全部的内容, 语句: select * from students;
7.按特定条件查询
where 关键词用于指定查询条件:select 列名称 from 表名称 where 条件
示例:
查询年龄在21岁以上的全部人信息: select * from students where age > 21;
查询名字中带有 "王" 字的全部人信息: select * from students where name like "%王%";
查询id小于5且年龄大于20的全部人信息: select * from students where id<5 and age>20;
8.更新表中的数据
update语句可用来修改表中的数据:update 表名称 set 列名称=新值 where 更新条件
示例:
将id为5的手机号改成默认的"-": update students set tel=default where id=5;
将全部人的年龄增长1: update students set age=age+1;
将手机号为 13288097888 的姓名改成 "张伟鹏", 年龄改成 19: update students set name="张伟鹏", age=19 where tel="13288097888";
9.删除表中的数据
delete 语句用于删除表中的数据:delete from 表名称 where 删除条件
示例:
删除id为2的行: delete from students where id=2;
删除全部年龄小于21岁的数据: delete from students where age<20;
删除表中的全部数据: delete from students;
建立后表的修改
alter table 语句用于建立后对表的修改, 基础用法以下:table
1.添加列:alter table 表名 add 列名 列数据类型 [after 插入位置]
实例:
在表的最后追加列 address: alter table students add address char(60);
在名为 age 的列后插入列 birthday: alter table students add birthday date after age;
2.修改列:alter table 表名 change 列名称 列新名称 新数据类型
实例:
将表 tel 列更名为 telphone: alter table students change tel telphone char(13) default "-";
将 name 列的数据类型改成 char(16): alter table students change name name char(16) not null;
3.删除列:alter table 表名 drop 列名称
示例:
删除 birthday 列: alter table students drop birthday;
4.重命名表:alter table 表名 rename 新表名
实例:重命名 students 表为 workmates: alter table students rename workmates;
5.删除整张表:drop table 表名称
实例:删除 workmates 表: drop table workmates;
6.删除整个数据库:drop database 数据库名;
示例: 删除 samp_db 数据库: drop database samp_db;
7.修改 root 用户密码
按照本文的安装方式, root 用户默认是没有密码的, 重设 root 密码的方式也较多, 这里仅介绍一种较经常使用的方式。
使用 mysqladmin 方式:
打开命令提示符界面, 执行命令: mysqladmin -u root -p password 新密码
执行后提示输入旧密码完成密码修改, 当旧密码为空时直接按回车键确认便可。
登录