MySQL-基础操作之增删改查
1.增
(1)創(chuàng)建數(shù)據(jù)庫(kù)dks
create database dks;(2)創(chuàng)建名為t1的表,并指定引擎和字符集;
create table t1(id int,name varchar(20) not null,ages int) engine=innodb default charset=utf8;(3)插入數(shù)據(jù),字符類型需要使用雙引號(hào);
insert into t1(id,name,ages)values(1,"zhangsan",28);(4)插入多條數(shù)據(jù)
insert into t1(id,name,ages)values(5,"xiaohong",58),(5,"xiaoming",68);(5)在后面增加一列
alter table t1 add job varchar(20);(6)在id列后面增加一列city;
alter table t1 add city tinyint(2) after id;?
2.刪
(1)刪除數(shù)據(jù)庫(kù)dks
drop database dks;(2)刪除表t1
drop table t1;(3)清空表內(nèi)容
delete from t1;(4)刪除job列
alter table t1 drop column job;(5)刪除數(shù)據(jù),where 條件篩選ages=18 數(shù)據(jù),刪除
delete from t1 where ages=18;?
3.查
(1)查看所有數(shù)據(jù)庫(kù)
show databases;(2)進(jìn)入dks數(shù)據(jù)庫(kù)
use dks;(3)查看數(shù)據(jù)庫(kù)內(nèi)有多少?gòu)埍?/p> show tables;
(4)查看t1表內(nèi)數(shù)據(jù)內(nèi)容
select * from t1;(5)只查看id這一列
select id from t1;(6)查看id、name兩列
select id,name from t1;(7)查詢ages大于20和name等于“zhangsan”的數(shù)據(jù);
select * from t1 where ages>20 and name="zhangsan";(8)查詢ages大于20和name等于“zhangsan”和id不等于1的數(shù)據(jù);
select * from t1 where ages>20 and name="zhangsan" and id !=1;(9)使用in參數(shù)指定多行
select * from t1 where id in (2,3);(10)使用not in參數(shù)排除多行
select * from t1 where id not in (2,3);(11)模糊查詢,使用通配符%查詢;%相當(dāng)于Linux中的*號(hào),統(tǒng)配所有;
select * from t1 where name like "xiao%"; #查詢所有與xiao有關(guān)的字符(12)一個(gè)下劃線可以統(tǒng)配一個(gè)字符,多個(gè)下劃線可以匹配多個(gè)字符;
select * from t1 where name like "xiao_";(13)只查看前三行數(shù)據(jù)
select * from t1 limit 3;(14)查看第三行后面的兩行數(shù)據(jù)
select * from t1 limit 3,2;(15)將數(shù)據(jù)從大到小排序
select * from t1 order by ages desc;(16)將數(shù)據(jù)從小到大進(jìn)行排序
select * from t1 order by ages asc;(17)指定庫(kù)、表、字段,進(jìn)行t1的查詢
select dks.t1.name from t1;(18)查看數(shù)據(jù)庫(kù)字符集
show variables like '%char%';(19)查看MySQL數(shù)據(jù)庫(kù)存儲(chǔ)引擎
show engines;(20)查看MySQL默認(rèn)存儲(chǔ)引擎
show variables like ‘%storage_engine%’;?
4.改
(1)change和modify都可以修改表的定義,不同的時(shí)change后面需要寫兩次列名,不方便。changge的優(yōu)點(diǎn)時(shí)可以修改列名稱,modify則不能。
(2)修改列名
alter table t1 change age ages int; #將age 改為 ages(2)修改字段類型和長(zhǎng)度
alter table t1 modify column ages varchar(10);(3)判斷修改id=3的數(shù)據(jù)
update t1 set id = 2 where id=3; #將id=3的數(shù)據(jù)改為id=2;(4)修改name字段的內(nèi)容
update t1 set name='zhangsan' where id=1;(5)修改MySQL 中 t1表的存儲(chǔ)引擎
alter table t1 engine=innodb;?
轉(zhuǎn)載于:https://www.cnblogs.com/shitou-st/p/10965024.html
總結(jié)
以上是生活随笔為你收集整理的MySQL-基础操作之增删改查的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 复制表数据和结构的方法
- 下一篇: 【转载】Sqlserver限制最大可使用