MySQL初阶
基本命令一览表
-启动mysql服务 (win: net start mysql ; Linux: sudo start mysql )
-登陆MySQL (mysql -u root -p)
-选择使用某一个数据库 (查看当前存在的数据库show databases; 创建一个新数据库 create database xxx; 选择 use xxx;)
-操作table (查看当前数据库中存在的表 show tables; 创建数据库表 create table 表名称(列表名); 查看某个表的详细信息 describe xxx;)
-向表中插入数据 insert into xxx values(..........);
-查询表中的数据 select 列名称 from 表名称 [查询条件]
-更新表中的数据 update 表名称 set 列名称=新值 where 更新条件;
-删除表中的数据 delete from 表名称 where 删除条件;
-表的修改 alter table 表名.........; 删除整个表 drop table xxx;
-删除某个数据库 drop database xxx;
-退出登陆 exit; quit;
-停止服务 (win: net stop mysql Linux: sudo stop mysql)
基本命令使用
1.创建一个数据库(database),然后选中这个数据库
create database test;
use test
2.目前该数据库中没有内容,现在在里边建一个表(table):students
ID | Name | Sex | Tel | Addr | Score |
---|---|---|---|---|---|
create table students(
ID int unsigned not null primary key,
Name char(8) not null,
Sex char(4) not null,
Tel char(13) null default("--"),
Addr char(20) null default("--"),
Score int unsigned
);
命令中not null
表示该列不能为空,primary key
表示该列为主键,即能通过该列的信息唯一确定一条记录;default
指名如果录入信息时没有指明该列,自动填充默认的内容。
使用describe students
可以查看表的结构。
3.现在来向表中添加记录
insert into students values(1,"Li","male","12345678","China",88);
如果只输入部分信息,需要确定属于哪列:
insert into students(ID,Name,Score) values(2,"female"Maaie",79)
4.查询表中的内容
select * form students
,*代表所有列
select Name,Male from students
,只查询表中Name和Male两列
select * from students where Sex="male"
,查询男性的所有信息
select ID,Name from students where Score>80
查询分数大于80的学生的学号和姓名
5.删除一条记录
delete from students where ID=1
,删除1号的记录
6.修改表alter table
在表中添加Age列:
alter table students add Age int unsigned after Name;
删除列:
alter table students drop Age;
7.删除整个表
drop table students
8.删除数据库
drop databases test