Win+R 运行,输入services.msc,查看MySql服务是否存在以及开启
Win+R 运行,输入cmd进入命令行(我的账户密码都是root)
create database db_user;//创建数据库
create database if not exists db_user//如果db_user不存在则创建,否则不创建
create database db_user character set gbk;//创建编码为GBK格式的数据库
show databases;//查看数据库
show create database db_user;//查询某个数据库的创建语句
show create database db_user//查看当前数据库编码(字符集)
alter database db_user character set = gbk;//修改数据库编码(类同)
drop database db_user;//删除数据库
drop database if exists db_user;//如果db_user不存在则不删除,否则删除
use db_user;//打开使用数据库
select database();//查询当前所在数据库
create table tb_user (id int primary Key auto_increment,username varchar(32),pwd varchar(32));//创建表,主键id自动生成,用户名及密码两列为字符串类型
create table if not exists tb_mysql(name varchar(20),age tinyint,salary decimal(7,2));//如果不存在则创建tb_sql表(salary decimal(7,2)-->一共有七位,保留小数点后两位)
alter table tb_user add age int;//添加一列
alter table tb_user change age newage int;//修改列名称
show tables;//查询表
show tables from db_name;//查看其它数据库中所有表
alter table tb_user rename to newtb_user;//修改表名
create table tb_user like newtb_user;//复制表
drop table tb_user;//删除表
drop table if exists tb_user;//如果存在则删除tb_user表
alter table tb_user drop age;//删除列
delete table tb_user;//删除表中所有记录
delete from tb_user where id = 1;//删除一条信息(记录)
update tb_user set age= 23 where id = 1;//修改数据
desc tb_user;//查询表字段(查看数据表结构)
insert [into] tb_user(username,pwd) values('Samson','123456' not null);//添加用户,pwd是非空字段
insert into tb_user values('LJT','5555522');//添加用户
select * from tb_user where username='Samson' and pwd='123456';//查询用户
select * from tb_user;//查看用户
select id,username from tb_user;//查看用户某字段信息
select distinct address from tb_user;//查询并去除重复字段
SELECT NAME math, english, math+IFNULL(english,0) AS 总分 FROM tb_test;//字段计算
1.not null;//非空约束(保证这个字段不能有空值)
2.primary Key auto_increment;//主键约束(保证数据的唯一性和一致性)
id INT UNSIGNED PRIMARY KRY AUTO_INCREMENT;//id无符号int主键约束自动增长
3.unique Key;//唯一约束(非主键唯一值约束)
alter table user add unique(username)//给表user中username添加唯一约束
4.default;//默认约束
//创建外键约束:父表和子表必须是相同的引擎,数据表的存储引擎为InnoDB
//查看表结构语句:
show create table F_table/S_table
//父表:
create table F_table(id int unsigned primary key auto_increment,name varchar(32));
//子表:
create table S_table(id int unsigned primary key auto_increment,name varchar(32),pid int unsigned,foreign key (pid) references F_table(id));//子表创建外键约束,pid与id相近的数据类型
ALTER TABLE tb01 ADD password VARCHAR(32);//添加password列
ALTER TABLE tb01 ADD age TINYINT AFTER name;//添加age列到name后面
alter table tb01 add (sex tinyint,height varchar(32));//同时添加sex和height两列
alter table tb01 drop height;//删除height列;
alter table tb01 drop sex,drop password;//同时删除sex和password列
alter table tb02 add primary key (id);//给id添加主键约束
alter table tb03_stu add foreign key (pid) references tb03_major(id);//添加外键约束
alter table tb03_stu alter sex set default 0;//添加默认约束
alter table tb03_stu alter sex drop default;//删除默认约束
alter table tb04 drop primary key;//删除主键索引
alter table tb04 drop index name;//删除唯一索引
(show create table tb03_stu;)alter table tb03_stu drop foreign key tb03_stu_ibfk_1;//删除外键索引
alter table tb04 modify name varchar(20);//修改数据表列定义
alter table tb04 change name username varchar(20);//修改数据表列名称
alter table tb04 rename as tb04_01;//修改表名
rename table tb04_01 to tb_04;//另一种方式修改表名
insert into tb05(id, name,password) values(null,'Samson','000000');//单条插入
insert into tb05 values(null,'L','111111');
insert into tb05(id, name) values(null,'T');
insert into tb05 values(null,'L','000000'),(null,'J','111111'),(null,'T','222222');//多条插入
update tb05 set password='000000';//更新表中所有密码值
update tb05 set password='111111' where id = 1;//更新id为1的密码值
delete from tb05 where id = 1;//删除第一行数据
select id as '编号',name as '名字',password as '密码'from tb06;//字段别名
select distinct(name),id from test group by(name);//可以查看那个id字段名name去重并分组
select distinct(name) from test;//字段名name去重
select distinct(name),id from test;//可以查看那个id字段名name去重
select version();//查询版本(8.0.11版本)
select 3*7+5+8;//表达式——简单的运算
select * from test order by id(asc);//根据id正向排序
select * from test order by id desc;//根据id反向(倒叙)排序
select * from test order by id limit 0,3;//限制结果,从0开始取三个
select * from test order by id desc limit 0,3;//限制结果,倒叙从0开始取三个
select count(*) from test;//查询表中的记录统计
select sum(id) from test;//对字段id的数字进行求和
select avg(id) from test;//对字段id的数字进行平均
select max(id) from test;//对字段id的数字进行求最大值
select min(id) from test;//对字段id的数字进行求最小值
select * from test where sex = 1 and age = 19;
select * from test where sex = 1 && age = 19;
select * from test where sex = 2 ;
select * from test where sex = 1 or age = 19;
select * from test where sex = 1 || age = 19;
select * from test where sex != 1;
select * from test where not sex = 1;
select * from test where id = 4;
select * from test where age = 19;
select * from test where age < 20;
select * from test where age <= 20;
select * from test where age > 20;
select * from test where age >= 20;
select * from test where age is null;
select * from test where age is not null;
select * from test where age between 19 and 21;
select * from test where age not between 19 and 21;
select * from test where age like "%1%";//查询在表test中匹配年龄字段值中带有1的记录(%1%:意思是1的左边不看,右边不看,不管第几位,只要带有1就可以)
select * from test where age like "2%";//(只查找开头是2的记录)
select * from test where age like "%2";//(只查找结尾是2的记录)
select * from test where age like "%2%" and sex like "%1%";//查询在表test中匹配age字段值中带有2和sex字段值中带有1的记录
select * from test where id in(1,3,5,7,9);//查找id包含1,3,5,7,9的记录
select * from test where id = 1 or id = 3 or id = 5 or id = 7 or id = 9;//(代码效率低,不推荐)
select * from test where id not in(1,3,5,7,9);//查找id不包含1,3,5,7,9的记录
select * from test where age > (select avg(age) from test);
select avg (age) from test;//平均值为20.222
select * from test where age > 20.222;
select * from test where age = (select avg(age) from test);
select * from test where age < (select avg(age) from test);
insert into test_price(price) select price from test group by price;//建表后插入多条记录
select price from test group by price;
create table test_price(id int primary key auto_increment,price varchar(32));
update test inner join test_price on test.price = test_price.price set test.price = test_price.id;//建表后更新多条记录
create table test_brand (id int primary key auto_increment,brand varchar(32)) select brand from test group by brand;//建表后插入多条记录
update test inner join test_price on test.price = test_price.price set test.price = test_price.id;//建表后更新多条记录
alter table test change brand brand_id int,change price price_id int;//更新原来字段类型和字段名
select * from test left join test_brand on test.brand_id = test_brand.id;//左表记录全部提取,右表只提取相匹配(符合)的记录,左表是test表
select * from test_brand left join test on test.brand_id = test_brand.id;//左表是test_brand表
select * from test right join test_brand on test.brand_id = test_brand.id;//右表记录全部提取,左表只提取相匹配(符合)的记录,右表是test表
select * from test_brand right join test on test.brand_id = test_brand.id;//右表是test_brand表
select * from test inner join test_brand on test.brand_id = test_brand.id;//左右表只提取相同匹配(符合)的记录
set autocommit = 0 // 关闭自动提交
start transaction //开启事务
rollback //回滚事务
commint //提交事务