CREATE TABLE student(
sid INT,
sname VARCHAR(20),
sage TINYINT,
sgender ENUM('f','m'),
cometime DATETIME)
CREATE TABLE stu(
sid INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
sname VARCHAR(20) NOT NULL,
sage TINYINT UNSIGNED,
sgender ENUM('f','m') NOT NULL,
cometime DATETIME NOT NULL) CHARSET utf8 ENGINE INNODB
-- 学生名(只招3个字的) 班级 学号(不可重复,可自增) 成绩总分(整数) 字符gbk 存储myisam
CREATE TABLE student_i (
name CHAR(9) NOT NULL COMMENT '名字',
class VARCHAR(20) NOT NULL COMMENT '班级',
num INT NOT NULL PRIMARY key AUTO_INCREMENT COMMENT '学号',
mark INT UNSIGNED NOT null COMMENT '成绩'
)CHARSET gbk ENGINE myisam;
CREATE TABLE crm (
lname VARCHAR (20) NOT NULL COMMENT '课程名称',
lcount INT PRIMARY KEY AUTO_INCREMENT COMMENT '课程节次',
tname VARCHAR (20) NOT NULL COMMENT '老师名字',
cdate DATETIME NOT NULL COMMENT'上课日期',
1title VARCHAR (20) NOT NULL COMMENT '课程标题',
hwtitle VARCHAR (20) COMMENT '作业标题',
mark ENUM('A','B','C','D') COMMENT '成绩'
)CHARSET utf8 ENGINE INNODB;
-- alter table 表名 drop 列名 删除指定列名
ALTER TABLE crm drop hwtitle;
DDL
-- 设计一个表 DDL定义语言, ddl 改 的是表属性
-- 插入新的属性
-- ALTER table 表名 ADD 列名 INT NOT NULL
ALTER TABLE crm ADD phone INT NOT NULL;
-- 一次添加多个属性
ALTER TABLE 表名ADD qq INT NULL, ADD wechat VARCHAR(10) NOT NULL;
ALTER TABLE crm ADD qq INT NULL, ADD wechat VARCHAR(10) NOT NULL;
-- 添加属性到第一列
ALTER TABLE 表名 ADD 添加的内容 INT FIRST;
ALTER TABLE crm ADD id INT FIRST;
-- 在指定的后面添加
ALTER TABLE 表名 ADD 要添加的东西 VARCHAR(50) AFTER 在谁后面加;
ALTER TABLE crm ADD address VARCHAR(50) AFTER tname;
desc crm;
修改表名 rename
ALTER TABLE 表名RENAME 改为什么名;
ALTER TABLE crm RENAME test;
改表属性 modfly
ALTER TABLE 表名MODIFY mark ENUM('A+','B+','C+','D+');
ALTER TABLE test MODIFY mark ENUM('A+','B+','C+','D+');
-- 修改列名 change
ALTER TABLE 表名CHANGE mark aaa ENUM('A','B','C','D');
ALTER TABLE test CHANGE mark aaa ENUM('A','B','C','D');
DCL
mysql> mysql> show grants for root@'10.0.0.%';
+---------------------------------------------------------------------------------------------------------------------+
| Grants for root@10.0.0.% |
+---------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'10.0.0.%' IDENTIFIED BY PASSWORD '*FE28814B4A8B3309DAC6ED7D3237ADED6DA1E515' |
+---------------------------------------------------------------------------------------------------------------------+
缩减权限
mysql> revoke select on *.* from root@'10.0.0.%';
Query OK, 0 rows affected (0.10 sec)
mysql> show grants for root@'10.0.0.%';
| GRANT INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES,
LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE ON *.* TO 'root'@'10.0.0.%' IDENTIFIED BY PASSWORD '*FE28814B4A8B3309DAC6ED7D3237ADED6DA1E515' |+-------------------------------------------------------------------------------------------------------------
DML
insert
mysql> desc stu;
不规范做法
mysql> insert into stu values(1,'zhang3',20,'m',NOW());
mysql> insert into stu values(2,'li4',21,'f',NOW());
这样插入不规范,造成结果就是sid不自增,仍然要手动指定.那么怎么办呢,哟啊values前加字段
语法
insert into 表名 value(1,2,3,4) values()
规范做法示例1插入1行
mysql> insert into stu(sname,sage,sgender,cometime) values('wang5',30,'m',NOW());
values前加字段,就自增了,这样做规范
示例2插入多行
mysql> insert into oldboy.stu(sname,sage,sgender,cometime) values('oldboy',80,'f',NOW()), ('alax',47,'m',NOW());
update 更新表内容
不规范做法
因为没限定条件,所以全给改了
mysql> update stu set sgender='f';
语法
update 表名 set 列名=什么 where sid=1
update环境
有内容
规范做法示例1
加入条件,这个条件最好是唯一的
mysql> update stu set sgender='m' where sid=1;
mysql> update stu set sgender='m' where sid=2
mysql> update stu set sgender='m' where 1=1;
update where or 或者指定条件
环境
表内容是
语法
update 表名 set 字段='m' where sid=1 or sid=4;
示例1
修改为id是1或者是4的 sgender为f
mysql> update stu set sgender='m' where sid=1 or sid=4;
update 之 where and并且指定条件
环境
表内容
添加alax
mysql> insert into stu(sname,sage,sgender,cometime) values('alax',47,'f',NOW());
示例1:
修改年龄为200 条件是 sname=alax和sid=8的
mysql> update stu set sage=200 where sname='alax' and sid=8;
示例2
要改年龄为100 条件是 时间是121212 并且名字是alax的
mysql> update stu set sage=100 where cometime='2019-02-27 12:12:12' and sname='alax';
delete
delete from 表名 where 指定行条件
环境
mysql> insert into stu(sname,sage,sgender,cometime) values('alax',111,'f',NOW());
mysql> insert into stu(sname,sage,sgender,cometime) values('alax',111,'f',NOW());
示例1 删除sid是10的
删除id是10的
mysql> delete from stu where sid=10;
清空表但是保留表结构
truncate table stu;
伪删除
mysql> select * from stu;
给表加个枚举属性,1和0 默认值是1
mysql> alter table stu add status enum('0','1') default '1';
现在不删除数据了把status状态改成0
update 修改表内容 status=0 suid是11的
mysql> update stu set status='0' where sid=11;
问题来了,开发怎么查呢 select 查询后面加where status=1的
把代码写到程序里, 用户查询到是where 条件过滤完的
mysql> select * from stu where status='1';
可以很清楚的看到没有status=0 的数据了,这就是伪删除
DQL
-- show tables from 那个库 == use + 库 show databases;
show tables from world;
-- 查看表是怎么建的
show create table city;
-- 查看库创建信息
show create database world;
-- 字符集
show charset;
-- 查看支持的所以存储引擎
show engines;
-- 模糊查看lock相关的状态信息
show status like '%lock%';
-- 查看mysql客户端连接情况
show collation;
show processlist;
查看主库状态
show master status G
show save status G
练习
city
-------------+
城市序号ID
城市名称Name
国家代号 CountryCode
市所在的省District
城市人口 Population
+-------------+
- 查看有什么库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| oldboy |
| performance_schema |
| world |
+--------------------+
5 rows in set (0.00 sec)
- 进入world库
mysql> use world;
Database changed
- 查看有什么表 ,发现有3张表
mysql> show tables;
+-----------------+
| Tables_in_world |
+-----------------+
| city |
| country |
| countrylanguage |
+-----------------+
3 rows in set (0.00 sec)
- 查看下表属性,发现有5列
mysql> desc city;
+-------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NULL | auto_increment |
| Name | char(35) | NO | | | |
| CountryCode | char(3) | NO | MUL | | |
| District | char(20) | NO | | | |
| Population | int(11) | NO | | 0 | |
+-------------+----------+------+-----+---------+----------------+
mysql> select * from city where id < 3;
+----+----------+-------------+----------+------------+
| ID | Name | CountryCode | District | Population |
+----+----------+-------------+----------+------------+
- 查询国家是中国河北省所以城市的信息
mysql> select * from city where countrycode='chn' and district='hebei';
+------+--------------+-------------+----------+------------+
| ID | Name | CountryCode | District | Population |
+------+--------------+-------------+----------+------------+
| 1907 | Shijiazhuang | CHN | Hebei | 2041500 |
| 1924 | Tangshan | CHN | Hebei | 1040000 |
| 1928 | Handan | CHN | Hebei | 840000 |
查询中国或者美国的国家
select * from city where countrycode in ('chn','usa');
like 模糊查询
查询 country 代码是 ch开头的
mysql> select * from city where countrycode like'ch%';
-- 查询以CH开头的国家城市信息 grep '^CH'
SELECT * from city WHERE countrycode LIKE 'CH%'
-- 查询以HA结尾的国家城市信息 grep 'HA$'
SELECT * from city WHERE countrycode LIKE '%HA'
SELECT * from city WHERE countrycode LIKE '%HA%'
-- 注意: CH%可以出现, %HA或者%H%尽量少出现
-- 取列
-- 获取city表中, name和population 两列的数据
SELECT name,Population FROM city;
-- 查询中国城市中人口数量大于1000w的城市
SELECT * FROM city WHERE countrycode='chn' AND population > 10000;
-- 查询中国城市中人口数量小于1000w的城市
SELECT * FROM city WHERE population < 100;
-- 世界上人口最多的城市 ORDER BY 基于 population的列 从到小排序
SELECT * FROM city ORDER BY Population desc;
-- 世界上人口数量前10名的城市信息 limit 10
SELECT * FROM city ORDER BY Population desc limit 10;
slelect 详解
语法:
select 列 from 表 where 条件
等值条件
例子:
select * from city where countrycode='chn' and district='hebei'
select * from city where countrycode='chn' unioq select * from city where countrycode='usa'
等值查询:查询数据比较精准, 结果集较小
范围查询
select * from city where countrycode='chn' or countrycode='usa';
select * from city where countrycode in ('chn','usa');
> , >= ,< ,<= ,<> !=
select * from city where population > 50000;
select * from city where population like 'CH%';
select * from city where population >10000 and population <50000; = select * from city where population between 10000 and 50000;
order by 和linmit 排序
从大到小排序并限制查询前10行
select * from city where countrycode='chn' order by population desc limit 10;
limit N,M 跳过N行显示M行
select * from city where countrycode='chn' order by population desc limit 10,20;
导库
mysql -uroot -poldboy123 test < /root/wordl.sql