MySQL语句大全

1select * from emp; #注释 2#--------------------------- 3#----命令行连接MySql--------- 4 5#启动mysql服务器 6net start mysql 7 8#关闭 9net stop mysql 10 11#进入 12mysql -h 主机地址 -u 用户名 -p 用户密码 13 14#退出 15exitstatus;显示当前mysql的version的各种信息。 16 17#--------------------------- 18#----MySql用户管理--------- 19 20#修改密码:首先在DOS 下进入mysql安装路径的bin目录下,然后键入以下命令: 21mysqladmin -uroot -p123 password 456; 22 23#增加用户 24#格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by '密码' 25/* 26如,增加一个用户user1密码为password1,让其可以在本机上登录, 并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入mysql,然后键入以下命令: 27grant select,insert,update,delete on *.* to user1@localhost Identified by "password1"; 28如果希望该用户能够在任何机器上登陆mysql,则将localhost改为"%"。 29如果你不想user1有密码,可以再打一个命令将密码去掉。 30grant select,insert,update,delete on mydb.* to user1@localhost identified by ""; 31*/ 32 33grant all privileges on wpj1105.* to sunxiao@localhost identified by '123'; #all privileges 所有权限 34 35#---------------------------- 36#-----MySql数据库操作基础----- 37 38#显示数据库 39show databases; 40 41#判断是否存在数据库wpj1105,有的话先删除 42drop database if exists wpj1105; 43 44#创建数据库 45create database wpj1105; 46 47#删除数据库 48drop database wpj1105; 49 50#使用该数据库 51use wpj1105; 52 53#显示数据库中的表 54show tables; 55 56#先判断表是否存在,存在先删除 57drop table if exists student; 58 59#创建表 60create table student( 61id int auto_increment primary key, 62name varchar(50), 63sex varchar(20), 64date varchar(50), 65content varchar(100) 66)default charset=utf8; 67 68#删除表 69drop table student; 70 71#查看表的结构 72describe student; #可以简写为desc student; 73 74#插入数据 75insert into student values(null,'aa','男','1988-10-2','......'); 76insert into student values(null,'bb','女','1889-03-6','......'); 77insert into student values(null,'cc','男','1889-08-8','......'); 78insert into student values(null,'dd','女','1889-12-8','......'); 79insert into student values(null,'ee','女','1889-09-6','......'); 80insert into student values(null,'ff','null','1889-09-6','......'); 81#查询表中的数据 82select * from student; 83select id,name from student; 84 85#修改某一条数据 86update student set sex='男' where id=4; 87 88#删除数据 89delete from student where id=5; 90 91# and 且 92select * from student where date>'1988-1-2' and date<'1988-12-1'; 93 94# or 或 95select * from student where date<'1988-11-2' or date>'1988-12-1'; 96 97#between 98select * from student where date between '1988-1-2' and '1988-12-1'; 99 100#in 查询制定集合内的数据 101select * from student where id in (1,3,5); 102 103#排序 asc 升序 desc 降序 104select * from student order by id asc; 105 106#分组查询 #聚合函数 107select max(id),name,sex from student group by sex; 108 109select min(date) from student; 110 111select avg(id) as '求平均' from student; 112 113select count(*) from student; #统计表中总数 114 115select count(sex) from student; #统计表中性别总数 若有一条数据中sex为空的话,就不予以统计~ 116 117select sum(id) from student; 118 119#查询第i条以后到第j条的数据(不包括第i条) 120select * from student limit 2,5; #显示3-5条数据 121 122#巩固练习 123create table c( 124 id int primary key auto_increment, 125 name varchar(10) not null, 126 sex varchar(50) , #DEFAULT '男' , 127 age int unsigned, #不能为负值(如为负值 则默认为0) 128 sno int unique #不可重复 129); 130 131drop table c; 132desc c; 133 134insert into c (id,name,sex,age,sno) values (null,'涛哥','男',68,1); 135insert into c (id,name,sex,age,sno) values (null,'aa','男',68,2); 136insert into c (id,name,sex,age,sno) values (null,'平平','男',35,3); 137... 138 139select * from c; 140 141#修改数据 142update c set age=66 where id=2; 143update c set name='花花',age=21,sex='女' where id=2 144delete from c where age=21; 145 146#常用查询语句 147select name,age ,id from c 148select * from c where age>40 and age<60; #and 149select * from c where age<40 or age<60; #or 150select * from c where age between 40 and 60 #between 151select * from c where age in (30,48,68,99); #in 查询指定集合内的数据 152select * from c order by age desc; #order by (asc升序 des降序) 153 154#分组查询 155select name,max(age) from c group by sex; #按性别分组查年龄最大值 156#聚合函数 157select min(age) from c; 158select avg(age) as '平均年龄 ' from c; 159select count(*) from c; #统计表中数据总数 160select sum(age) from c; 161 162#修改表的名字 163#格式:alter table tbl_name rename to new_name 164alter table c rename to a; 165 166#表结构修改 167create table test 168( 169id int not null auto_increment primary key, #设定主键 170name varchar(20) not null default 'NoName', #设定默认值 171department_id int not null, 172position_id int not null, 173unique (department_id,position_id) #设定唯一值 174); 175 176#修改表的名字 177#格式:alter table tbl_name rename to new_name 178alter table test rename to test_rename; 179 180#向表中增加一个字段() 181#格式:alter table tablename add columnname type;/alter table tablename add(columnname type); 182alter table test add columnname varchar(20); 183 184#修改表中某个字段的名字 185alter table tablename change columnname newcolumnname type; #修改一个表的字段名 186alter table test change name uname varchar(50); 187 188select * from test; 189 190#表position 增加列test 191alter table position add(test char(10)); 192#表position 修改列test 193alter table position modify test char(20) not null; 194#表position 修改列test 默认值 195alter table position alter test set default 'system'; 196#表position 去掉test 默认值 197alter table position alter test drop default; 198#表position 去掉列test 199alter table position drop column test; 200#表depart_pos 删除主键 201alter table depart_pos drop primary key; 202#表depart_pos 增加主键 203alter table depart_pos add primary key PK_depart_pos 204(department_id,position_id); 205 206#用文本方式将数据装入数据库表中(例如D:/mysql.txt) 207load data local infile "D:/mysql.txt" into table MYTABLE; 208 209#导入.sql文件命令(例如D:/mysql.sql) 210source d:/mysql.sql; #或者 /. d:/mysql.sql;
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Python3:sqlalchemy对mysql数据库操作,非sql语句

Python3:sqlalchemy对mysql数据库操作,非sql语句python3authorlizmdatetime2018020110:00:00coding:utf8'''