后端开发基础——SQL
SQL概述
数据库:好比物流企业的仓库。用户从Web服务器浏览网站,而服务器通过SQL语言指令从数据库系统返回数据,最终经过web包装后返回给用户。
数据库系统构成:软件,服务接口(数据库,数据表,数据),用户三部分。
搭建数据库
环境准备:
1. MySQL命令行(更常用)
2. phpMyAdmin(可视化)
数据库操作:
1. 创建数据库:CREATE DATABASE name_of_database;
2. 查看数据库:SHOW databases;
3. 切换数据库:USE name_of_database;
4. 删除数据库:DROP DATABASE name_of_database;
TIPS: SQL语句对大小写不敏感;句末应有分号。
数据表介绍:
数据表操作:
1. 创建数据表:CREATE TABLE name_of_database (name_of_element1 type(length) attribute, …);
2. 查看数据表:SHOW tables;
3. 插入数据表:INSERT INTO name_of_database(name_of_element1, …) VALUES (content1, …);
4. 查询数据表:SELECT * FROM name_of_database;
演示:
SQL语法
- where:select/update/delete information from tables where conditions
- order by: select information from tables order by 1/2/ASC/DESC
- UNION: select information from table1 union select information from table2
- 注释: #… , – … , /*…*/
- database(): 返回数据库名字
- current_user: 返回用户信息
- load_file(): 显示文件内容
- version(): 数据库版本