Mysql增删改查sql语句练习
Mysql增删改查sql语句练习
关于数据库的⼀些操作:
进⼊mysql 命令⾏: mysql -uroot –p
查看所有数据库: show databases;
创建数据库: create database wg charset utf8;
删除数据库: drop database wg;
选择数据库: use databases;
查看所有表: show tables;
查看创建数据库的语句:show create database databasename;
查看创建表的语句:show create table tablename;
查看表结构:desc tablename;
增:
mysql> use wg;好看的企业网站模板
mysql> create table students( id int auto_increment primary key,name varchar(10) not null,sex varchar(12),address varchar(50),phone int not null unique);
#⾃增长 auto_increment
#⾮空 not null
#默认值 default ‘xx’
轮博怎么做#唯⼀ unique
#指定字符集 charset
#主键 primary key网页素材大宝库
mysql> create table scores(id int auto_increment primary key,s_id int not null,grade float not null);
数据:
mysql> insert into student (id,name,sex,phone) values(122,’wg’,’男’,’110’);
mysql> insert into students values(111,’wg’,’121’,’dd’) ;
删:
mysql> drop table tablename;
mysql> truncate tablename; 快速删除表数据,⾃增长id从头在来,快速,从磁盘直接删除,不可恢复
接口性能测试怎么做mysql> delete from student; 删除整个表的数据,⾃增长继续
改:
mysql> alter table oldtable rename newtable; 改表名
mysql> alter table scores modify s_id varchar(20);
mysql> alter table scores change s_id scoresss varchar(16); 修改表结构
mysql> alter table scores add age float after id; 新增字段的位置(age放在了ID后⾯)
mysql> update student set age=18;不指定条件,修改所有
mysql> update student set age=18 where name=‘wg’;只修改⽹⼯
查:
mysql> show create table tablename; 查看新建表语句
mysql> desc tablename; 查看表结构
mysql> show tables; 查看所有表
mysql> select * from students limit 5;查询5条
mysql> select * from students limit 1,5;从第⼏条开始,下⾯的n条
mysql> SELECT * from students where sex=‘男’; #指定条件
mysql> SELECT * from students where sex=‘男’ and age>10; #多个条件,必须同时满⾜
mysql> SELECT * from students where sex=‘男’ or sex=‘未知’ ; #多个条件,有⼀个满⾜即可
bitcoin核心更新mysql> SELECT * from students where sex !=‘男’; #<>也是不等于
mysql> SELECT * FROM students where addr like ‘%东京%’;#模糊匹配,%代表的是通配符,必须得⽤like
mysql> SELECT * from students a where a.stu_name like ‘姚_’;#_通配符表⽰任意⼀个单字符,姚字后⾯只能跟⼀个字
mysql> SELECT a.stu_name ‘学⽣名称’,a.phone ‘学⽣电话’ from students as a where a.stu_name=‘姚远’;#给表起别名,as 可以省略
SELECT * from students a BETWEEN 1000 and 10000;#在什么什么之间的数据
SELECT * from students ORDER BY money desc; #order by xxx desc,根据哪个字段继续排序,默认是升序,降序是desc,升序asc
SELECT * from students a where a.addr = ‘’ or a.addr is null; #查询字段为空的数据
SELECT from students a ;#去重
SELECT COUNT(*) ‘学⽣⼈数’ from students where sex=‘⼥’; #统计⾏数
SELECT ) 钱最多 from students a; #最⼤值
SELECT min(money) 钱最少 from students;#最⼩值mysql菜鸟教程增删改查
SELECT ) 平均多少钱 from students a; #平均数
SELECT ) 总共多少钱 from students a;#总和
备份数据库:
mysqldump -uroot -p123456 db > db.sql mysqldump -uroot -p123456 -A > all.sql 恢复数据:
mysql -uroot -p123456 db < db.sql
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论