二:MySQL系列之SQL基本操作(二)
本篇主要介绍SOL语句的基本操作,主要有分为 连接数据库,创建数据库、创建数据表、添加数据记录,基本的查询功能等操作。
一、针对数据库的操作
-- 1、连接数据库
mysql -uroot -p
mysql -uroot -ppassword
-- 2、退出数据库
exit/quit/ctrl+d
-- 3、显示数据库版本,显示时间
select version();
select now();
-- 4、查看当前RDBMS管理的所有数据库
show databases;
-- 5、创建数据库 --information
create database information;
create database information charset=utf8; --指定编码
-- 6、查看创建数据库的详细信息
show create database information;
-- 7、查看当前使用的数据库
select database();
-- 8、使用数据库
use information;
-- 9、删除数据库(慎用)
drop database heros;
二、针对数据表的操作
------ 创建表,和查看表的结构------ -- 1、查看当前数据库中所有的表 show tables; -- 2、创建表的字段 类型(主键等)限制等-- -- 创建一个存放英雄信息的数据表 create table heros( id int unsigned primary key auto_increment not null, name varchar(20), age tinyint unsigned default 0, height decimal(5,2), gender enum("男","女","其他") default "其他", prov_id int unsigned, is_delete bit default 0 ); -- 创建一个存放名著 create table famous_books( id int unsigned primary key not null auto_increment, name varchar(30) not null, author varchar(20) ); -- 3、查看表的结构 desc heros -- 4、查看创建表的详细信息 show create table heros; -- 5、创建表的值 insert into heros values(0,"曹操",35,178.35,"男",1,0); insert into heros values(0,"刘备",45,175.32,"男",1,0), (0,"孙权",28,182.63,"男",1,0), (0,"貂蝉",20,175.22,"女",1,0), (0,"孙夫人",45,168.11,"女",1,0), (0,"宋江",48,178.01,"男",2,0), (0,"吴用",36,170.12,"男",2,0), (0,"黑寡妇",25,172.01,"女",2,0), (0,"贾宝玉",21,177.23,"男",3,0), (0,"林黛玉",19,165.32,"女",3,0), (0,"薛宝钗",19,168.55,"女",3,0); insert into famous_books values(0,"《水浒传》","施耐庵"), (0,"《三国演义》","罗贯中"), (0,"《红楼梦》","曹雪芹"); -- 6、查看表的结构 select * from heros; select * from famous_books; -- 得出的结果为: +----+-----------+------+--------+--------+---------+-----------+ | id | name | age | height | gender | prov_id | is_delete | +----+-----------+------+--------+--------+---------+-----------+ | 1 | 曹操 | 35 | 178.35 | 男 | 1 | | | 2 | 刘备 | 45 | 175.32 | 男 | 1 | | | 3 | 孙权 | 28 | 182.63 | 男 | 1 | | | 4 | 貂蝉 | 20 | 175.22 | 女 | 1 | | | 5 | 孙夫人 | 45 | 168.11 | 女 | 1 | | | 6 | 宋江 | 48 | 178.01 | 男 | 2 | | | 7 | 吴用 | 36 | 170.12 | 男 | 2 | | | 8 | 黑寡妇 | 25 | 172.01 | 女 | 2 | | | 9 | 贾宝玉 | 21 | 177.23 | 男 | 3 | | | 10 | 林黛玉 | 18 | 165.32 | 女 | 3 | | | 11 | 薛宝钗 | 19 | 168.55 | 女 | 3 | | | 12 | 薛宝钗 | 20 | 168.23 | 女 | 3 | | | 13 | 武松 | 26 | NULL | 其他 | NULL | | | 14 | 孙权 | 21 | 182.22 | 男 | 1 | | +----+-----------+------+--------+--------+---------+-----------+ 14 rows in set (0.00 sec) -- 注:里面的值有所添加
三、针对数据表字段的操作
-------- 修改表的结构,即字段的操作 alter-------- -- 1、-添加字段-- alter table heros add birthday datatime; -- 2、修改字段 - 修改字段的约束 alter table heros modify brithday date; -- 3、修改字段,修改字段名--- alter table heros change brithday birth date default "1996-01-01"; --注后面必须写上限制 -- alter table heros change brithday birth; 错误 -- 4、删除字段 alter table heros drop birth; -- drop database information; --删除数据库 -- drop table heros; -- 删除数据表
注:针对表头(字段)的操作均为 alter table ;
四、针对值的操作(记录)
------------针对值;增删改查(curd)----------------- --增加-- --全列插入-- insert into heros values(0,"赵子龙",23,185.62,"男",1,0); --同时插入多个 insert into famous_books values(0,"《西游记》","吴承恩"),(0,"《老人与海》","海明威"); --主键字典key用0,null,default占用 insert into heros values(null,"赵子龙",23,185.62,"男",1,0); insert into heros values(default,"赵子龙",23,185.62,"男",1,0); --枚举中的 下标使用 insert into heros values(0,"赵子龙",23,185.62,1,1,0); --部分插入,其他的设置默认值,没有默认值设置系统默认值 insert into heros (name,age,gender) values("张飞",35,1); -- 若插入枚举中不存在的,则报错; --insert into heros (name,age,gender) values("张飞",35,"人妖"); --报错 --修改 -------------修改对应字段的值---------------- update famous_books set name="《小黄书》"; -- 全部修改为name为《小黄书》; update classes set name="《聊斋》" where id =3; --指定id=3的修改为《聊斋》; update hero set age=22,gender="男" where id=2; --可以同时修改两个值;
--查询 --查询所有列 --select * from 表名 select * from heros; --定条件查询 --where select * from hreos where name="张飞"; --只查看 名字为张飞的所有信息 select * from heros where id>3; --查看id>3的信息 --查询指定列 select name,age,gender form hreos; --使用as作为列或者表的别名 select name as 姓名,age as 年龄, gender as 性别 from heros; --删除 -- 物理删除 delete from XXX delete from heros; --整个 数据表中所有的数据均删除 delete from heros where name="关羽"; --删除所有名字为关羽的记录 -- 逻辑删除 即用一个字段来表示,这条信息是否已经不使用 alter table famous_books add is_delete varchar(1) default 0; update amous_books set is_delete=1 where id =2;
over~~~ 本篇主要为命令~~~~~需要理解的比较少,找到规律多练即可~~~
本文来自博客园,作者:Little_five,转载请注明原文链接:https://www.cnblogs.com/littlefivebolg/p/9368775.html