--Created by mac on 2017/1/4. -- MySQL数据库 -- ****************** 一. 连接数据库服务器的基础命令 ******************* -- 1. 连接数据库服务器的基础命令: a. cd /Applications/XAMPP/bin -- b. ./mysql -u用户名 -p密码 -- 2. 查看所有数据库 : show databases; -- 3. 使用已有数据库: use 数据库名字; -- 4. 查看该数据库下的表: show tables; -- 5. 查看该表中的记录: select * from 表名; -- 6. 描述表的结构: describe 表名; -- 7. 退出数据库服务器: exit; -- 已创建好一个数据库名为(xg_info),表名(xg) -- ****************** 二. 创建数据库,表 ******************* -- 注意事项: sql语句的关键字全部都是大写,自己书写时可以小写,因为数据库服务器在执行时会把小写字母的关键字全部变成大写字母 -- 如果数据库名字或表名字当中出现了下划线以外的特殊字符,而无法被识别。需要用反向单引号把这个名字引起来。例如:`register-info` -- 1. 创建数据库: create database 数据库名字; -- 2. 删除数据库: drop database 数据库名字; -- 3. 创建表(primary key 表示主键; auto_increment 属性表示,自动加一; not null 表示该字段的值不能为空; default 表示默认值;) -- 一张完整的表: 表名(字段名 字段类型 主键) 引擎 字符集 -- CREATE TABLE 数据库名字.表名字( -- ID INT PRIMARY KEY AUTO_INCREMENT NOT NULL , -- NAME VARCHAR (18) NOT NULL , -- PASSWORD VARCHAR (18) NOT NULL , -- EMAIL VARCHAR (18), -- TEL CHAR (11), -- SEX CHAR (1) DEFAULT '男' -- )ENGINE=InnoDB CHARSET=UTF8; -- 4.描述表结构: describe 表名; -- ****************** 三. 对表的操作: 增删改查 ******************* -- 1. 增 (insert into) -- a. 增加全部字段的内容,必须一一对应 insert into 表名 values('$id','$username','$password','$email','$tel','$sex'); -- 例: insert into xg values(1,'张三','12345678','zhangsan@qq.com','13568689468','男'); -- b. 按照指定字段添加数据(如果没有指定 not null 修饰的字段名,则该字段的数据自动用空白填充) insert into 表名 (name,password) values('王二','1234567890'); -- 例: insert into xg (name,password) values('王二','1234567890'); -- 2. 删(delete) -- a. 指定条件删除: delete from 表名 where 删除的id -- 例: delete from xg where id=3; -- b. 删除所有记录,慎用(删除以后表还在) delete from 表名; -- 例: delete from xg; -- 3. 改(update set) -- a. 指定某个条件改变: update 表名 set 需要修改的内容 where 被修改的那一条id -- 例: update xg set name='李奏凯' where id=2; -- 4. 查(select) -- a. 查询所有记录 select * from 表名; -- 例: select * from xg; ----------- 01. 指定条件查询 -------------- -- b. 指定某个条件查询 select * from 表名 where 指定条件; -- 例: select * from xg where sex='男'; -- c. 指定多个条件查询(where and) select * from 表名 where 查询条件 and 查询条件; -- 例: select * from xg where name='张三' and sex='男'; -- d. 指定判断条件查询(where >,<,!=) select * from 表名 where 判断条件; -- 例: select * from xg where id>3; -- e. 指定多个或条件查询(where or) select * from 表名 where 查询条件 or 查询条件; -- 例: select * from xg where id=7 or name='张三'; -- f. 指定查询条件在某一个集合中(where in) select * from 表名 where id in (指定查询的id); -- 例: select * from xg where id in (1,2,3,7,8); -- g. 指定查询条件在某一个区间内(where between and [2,5](是一个闭区间,包含首尾的)) select * from 表名 where id between 开始查询的id and 查询结束的id; -- 例: select * from xg where id between 2 and 5; ----------- 02. 模糊查找(where like %) -------------- -- a. 查询电话号为 135 开始的所在记录 -- 例: select * from xg where tel like '135%'; -- b. 查询电话号为 90 结束的所在记录 -- 例: select * from xg where tel like '%90'; -- c. 查询电话号中有 37 的记录 -- 例: select * from xg where tel like '%37%'; -- d. 查询电话号 13 开始的,中间包含 33 的记录 -- 例: select * from xg where tel like '13%33%'; ----------- 03. 查询一列或多列字段下的数据 -------------- -- a. 查询一列数据,指定字段下的所有数据 select 指定查询的字段 from 表名; -- 例: select name from xg; -- b. 查询多列数据,指定字段下的所有数据 select 第一个被指定查询的字段,第二个被指定查询的字段,第三个被指定查询的字段 from 表名; -- ****************** 四. 对标的结构进行修改(alter) ******************* -- 1. 修改表的字段类型(modify),改变需要改的类型,不需要修改的地方也要照抄 alter table 表名 modify 需要修改的类型 字段类型 default 被修改为什么 是否能为空; -- 例: alter table xg modify sex char(1) default '女' not null; -- 2. 修改表的字段名字和类型(change),change可以同时修改,但是不能替代modify使用只修改字段类型 alter table 表名 change 被修改的字段 修改为什么字段 字段类型 是否能为空; -- 例: alter table xg change name username varchar(20) not null; -- 3. 添加字段(add) alter table 表名 add 需要添加的字段 字段类型; -- 例: alter table xg add age varchar(15); -- 添加字段配合使用(first 表示放在第一个字段前面, after 表示放在某个字段后面) -- a. after 的使用(after 表示放在某个字段后面) alter table 表名 add 需要添加的字段 字段类型 after 选择字段; -- 例: alter table xg add address varchar(25) after tel; -- b. first 的使用(first 表示放在第一个字段前面) alter table 表名 add 需要添加的字段 字段类型 first; -- 例: alter table xg add birthday varchar(10) first; -- 4. 删除字段(drop) -- a. 删除一个字段 alter table 表名 drop 被删除的字段; -- 例: alter table xg drop birthday; -- b. 同时删除多个字段 alter table 表名 drop 被删除的字段,drop 被删除的字段; -- 例: alter table xg drop address,drop tel; -- 5.删除表 drop table 表名; -- 例: drop table xg;
本文为头条号作者发布,不代表今日头条立场。