SQL语句的DDL:操作数据库、表(增删改查)

  1. 操作数据库:CRUD
    1. C(Create):创建
    * 创建数据库:
    * create database 数据库名称;
    * 创建数据库,判断不存在,再创建:
    * create database if not exists 数据库名称;
    * 创建数据库,并指定字符集
    * create database 数据库名称 character set 字符集名;
    * 练习: 创建db4数据库,判断是否存在,并制定字符集为gbk
    * create database if not exists db4 character set gbk;
    2. R(Retrieve):查询
    * 查询所有数据库的名称:
    * show databases;
    * 查询某个数据库的字符集:查询某个数据库的创建语句
    * show create database 数据库名称;
    3. U(Update):修改
    * 修改数据库的字符集
    * alter database 数据库名称 character set 字符集名称;
    4. D(Delete):删除
    * 删除数据库
    * drop database 数据库名称;
    * 判断数据库存在,存在再删除
    * drop database if exists 数据库名称;
    5. 使用数据库
    * 查询当前正在使用的数据库名称
    * select database();
    * 使用数据库
    * use 数据库名称;

操作表
1. C(Create):创建
1. 语法:
create table 表名(
列名1 数据类型1,
列名2 数据类型2,

列名n 数据类型n
);
* 注意:最后一列,不需要加逗号(,)
* 数据库类型:
1. int:整数类型
* age int,
2. double:小数类型
* score double(5,2)
3. date:日期,只包含年月日,yyyy-MM-dd
4. datetime:日期,包含年月日时分秒 yyyy-MM-dd HH:mm:ss
5. timestamp:时间错类型 包含年月日时分秒 yyyy-MM-dd HH:mm:ss
* 如果将来不给这个字段赋值,或赋值为null,则默认使用当前的系统时间,来自动赋值
6. varchar:字符串
* name varchar(20):姓名最大20个字符
* zhangsan 8个字符 张三 2个字符
* * 创建表
create table student(
id int,
name varchar(32),
age int ,
score double(4,1),
birthday date,
insert_time timestamp
);
* 复制表:
* create table 表名 like 被复制的表名;
2. R(Retrieve):查询
* 查询某个数据库中所有的表名称
* show tables;
* 查询表结构
* desc 表名;
3. U(Update):修改
1. 修改表名
alter table 表名 rename to 新的表名;
2. 修改表的字符集
alter table 表名 character set 字符集名称;
3. 添加一列
alter table 表名 add 列名 数据类型;
4. 修改列名称 类型
alter table 表名 change 列名 新列别 新数据类型;
alter table 表名 modify 列名 新数据类型;
5. 删除列
alter table 表名 drop 列名;
4. D(Delete):删除
* drop table 表名;
* drop table if exists 表名 ;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要创建一个数据库,包含两个:部门(department)和员工(employee)。 建语句如下: ```sql CREATE DATABASE IF NOT EXISTS `company`; USE `company`; CREATE TABLE IF NOT EXISTS `department` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL, PRIMARY KEY (`id`) ); CREATE TABLE IF NOT EXISTS `employee` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL, `gender` char(1) NOT NULL, `birthday` date NOT NULL, `department_id` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `department_id` (`department_id`), CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`department_id`) REFERENCES `department` (`id`) ); ``` 然后,我们可以使用Spring Boot和Thymeleaf来实现部门和员工增删改查操作。 在Spring Boot中,我们可以使用JPA来操作数据库,同时使用Thymeleaf来实现页面的渲染。 首先,我们需要在pom.xml文件中添加以下依赖: ```xml <dependencies> <!-- Spring Boot Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Data JPA --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- MySQL JDBC Driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- Thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> ``` 然后,我们需要在application.properties文件中配置数据库连接信息: ``` spring.datasource.url=jdbc:mysql://localhost:3306/company spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update spring.thymeleaf.cache=false ``` 接下来,我们需要创建实体类Department和Employee,对应数据库中的department和employee: ```java @Entity @Table(name = "department") public class Department { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "name") private String name; // getter and setter } @Entity @Table(name = "employee") public class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "name") private String name; @Column(name = "gender") private String gender; @Column(name = "birthday") private Date birthday; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "department_id") private Department department; // getter and setter } ``` 然后,我们需要创建DepartmentRepository和EmployeeRepository接口,继承JpaRepository接口,用于操作数据库: ```java public interface DepartmentRepository extends JpaRepository<Department, Long> { } public interface EmployeeRepository extends JpaRepository<Employee, Long> { } ``` 接下来,我们需要创建DepartmentController和EmployeeController类,用于处理请求和响应: ```java @Controller @RequestMapping("/department") public class DepartmentController { @Autowired private DepartmentRepository departmentRepository; @GetMapping("/list") public String list(Model model) { List<Department> departments = departmentRepository.findAll(); model.addAttribute("departments", departments); return "department/list"; } @GetMapping("/add") public String add(Model model) { Department department = new Department(); model.addAttribute("department", department); return "department/form"; } @PostMapping("/save") public String save(@ModelAttribute("department") Department department) { departmentRepository.save(department); return "redirect:/department/list"; } @GetMapping("/edit/{id}") public String edit(@PathVariable("id") Long id, Model model) { Department department = departmentRepository.findById(id) .orElseThrow(() -> new IllegalArgumentException("Invalid department Id:" + id)); model.addAttribute("department", department); return "department/form"; } @GetMapping("/delete/{id}") public String delete(@PathVariable("id") Long id) { Department department = departmentRepository.findById(id) .orElseThrow(() -> new IllegalArgumentException("Invalid department Id:" + id)); departmentRepository.delete(department); return "redirect:/department/list"; } } @Controller @RequestMapping("/employee") public class EmployeeController { @Autowired private EmployeeRepository employeeRepository; @Autowired private DepartmentRepository departmentRepository; @GetMapping("/list") public String list(Model model) { List<Employee> employees = employeeRepository.findAll(); model.addAttribute("employees", employees); return "employee/list"; } @GetMapping("/add") public String add(Model model) { Employee employee = new Employee(); List<Department> departments = departmentRepository.findAll(); model.addAttribute("employee", employee); model.addAttribute("departments", departments); return "employee/form"; } @PostMapping("/save") public String save(@ModelAttribute("employee") Employee employee) { employeeRepository.save(employee); return "redirect:/employee/list"; } @GetMapping("/edit/{id}") public String edit(@PathVariable("id") Long id, Model model) { Employee employee = employeeRepository.findById(id) .orElseThrow(() -> new IllegalArgumentException("Invalid employee Id:" + id)); List<Department> departments = departmentRepository.findAll(); model.addAttribute("employee", employee); model.addAttribute("departments", departments); return "employee/form"; } @GetMapping("/delete/{id}") public String delete(@PathVariable("id") Long id) { Employee employee = employeeRepository.findById(id) .orElseThrow(() -> new IllegalArgumentException("Invalid employee Id:" + id)); employeeRepository.delete(employee); return "redirect:/employee/list"; } } ``` 最后,我们需要创建部门和员工的页面,使用Thymeleaf进行渲染: ```html <!-- department/list.html --> <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Department List</title> </head> <body> <h1>Department List</h1> <table> <tr> <th>ID</th> <th>Name</th> <th>Action</th> </tr> <tr th:each="department : ${departments}"> <td th:text="${department.id}"></td> <td th:text="${department.name}"></td> <td> <a th:href="@{/department/edit/{id}(id=${department.id})}">Edit</a> <a th:href="@{/department/delete/{id}(id=${department.id})}">Delete</a> </td> </tr> </table> <a th:href="@{/department/add}">Add Department</a> </body> </html> <!-- department/form.html --> <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Add Department</title> </head> <body> <h1>Add Department</h1> <form action="/department/save" method="post"> <input type="hidden" th:field="*{id}"> <label for="name">Name:</label> <input type="text" id="name" th:field="*{name}"> <input type="submit" value="Save"> </form> <a th:href="@{/department/list}">Back to List</a> </body> </html> <!-- employee/list.html --> <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Employee List</title> </head> <body> <h1>Employee List</h1> <table> <tr> <th>ID</th> <th>Name</th> <th>Gender</th> <th>Birthday</th> <th>Department</th> <th>Action</th> </tr> <tr th:each="employee : ${employees}"> <td th:text="${employee.id}"></td> <td th:text="${employee.name}"></td> <td th:text="${employee.gender}"></td> <td th:text="${#dates.format(employee.birthday, 'yyyy-MM-dd')}"></td> <td th:text="${employee.department.name}"></td> <td> <a th:href="@{/employee/edit/{id}(id=${employee.id})}">Edit</a> <a th:href="@{/employee/delete/{id}(id=${employee.id})}">Delete</a> </td> </tr> </table> <a th:href="@{/employee/add}">Add Employee</a> </body> </html> <!-- employee/form.html --> <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Add Employee</title> </head> <body> <h1>Add Employee</h1> <form action="/employee/save" method="post"> <input type="hidden" th:field="*{id}"> <label for="name">Name:</label> <input type="text" id="name" th:field="*{name}"> <br> <label for="gender">Gender:</label> <input type="text" id="gender" th:field="*{gender}"> <br> <label for="birthday">Birthday:</label> <input type="date" id="birthday" th:field="*{birthday}"> <br> <label for="department">Department:</label> <select id="department" th:field="*{department}"> <option th:each="department : ${departments}" th:value="${department}" th:text="${department.name}" th:selected="${employee.department == department}"></option> </select> <br> <input type="submit" value="Save"> </form> <a th:href="@{/employee/list}">Back to List</a> </body> </html> ``` 至此,我们就完成了使用Spring Boot和Thymeleaf实现部门和员工增删改查操作

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值