MYSQL为关系型数据库,所谓关系,即可以理解为表格的概念,一个关系型数据库由一个或者数个表格组成
表头header
行row
列col
值value
键key
数据库database
连接数据库:
mysql -host(主机名) -u(用户名) -p(登录密码,为空则忽略此项)
创建一个数据库
create database +数据库名(samp_db) 【其他选项】
createdatabase samp_db character set gbk
创建一个叫sanmp_db的数据库并设置编码为gbk
创建数据库表
create table +表名称(列声明)
creattable students (
id int unsigned not null auto_incrementprimary key,
name char(8) not null,
sex char(4) not null,
age tinyint unsigned not null,
tel char(13) null default “-”
);
插入数据
insert [into] 表名 [(列名1,列名2,列名3,…)] values (值1,值2,值3,…);
insertinto students values(NULL,”王刚“,”男“,20,”17625612527“);
insertinto students (name,sex,age) values (“孙丽华”,“女“,21);
查找数据
select 列名称 from 表名称 [查询条件];
selectname,age from students;
按照特定的条件查找
select 列名称 from 表名称 where 条件;
select* from students where age > 21;
select* from students where name like “%王%”;
更新表中的数据
update 表名称 set 列名称=新值whiere 更新条件;
updatestudents set tel=default where id=5;
updatestudents set age=age+1;
删除表中的数据
deletefrom 表名称 where 删除条件;
delete f’rom students whereid=2;
创建后表的修改
添加列
alter table 表名 add 列名 列数据类型 【alert插入位置】
altertable students add birthday date after age;
修改列
alter table 表名 change列名称 列新名称 新数据类型;
altertable students change tel telphone char(13) default;
altertable students change name name char(16) not null;
删除列
alter table 表名 drop 列名称 ;
altertable students drop birthday;
重命名表
alter table 表名 rename 新表名;
alter table students rename workmates;
删除整张表
drop table 表名;
droptable workmates;
删除整个数据库
drop databases 数据库名称;
dropdatabases samp_db;