写给自己

巴拉巴拉。

创作时间

81213 19:37:00

作者

gaox

正文

  1. -- ----------------Mysql-----------------
  2. -- 查询数据库编码格式
  3. show variables like '%char%';
  4. show variables like 'character_set_client';#查询字符集
  5. -- 显示所有数据库
  6. show database;
  7. -- 创建一个数据库
  8. create database if not exists gaox;
  9. -- 删除数据库
  10. drop database gaox;
  11. -- 进入数据库
  12. use gaox;
  13. --显示所有表
  14. show tables;
  15. show tables from gaox; -- 查看gaox数据库中所有的表
  16. -- 输出表结构
  17. desc tb_dept;
  18. -- 输出建表语句
  19. show create table tb_dept;
  20. --删除表数据
  21. delete from t_user;
  22. -- 删除表(结构和数据)
  23. drop table center_bank_account;
  24. -- --------------------------------------------------------
  25. --创建表
  26. create table user(
  27. ID int primary key auto_increment, --主键,自增
  28. Name varchar (6),
  29. Dept varchar (16)
  30. );
  31. --修改列属性
  32. --仅当字段只包含空值时才可以修改
  33. alert table user modify Name varchar(8);
  34. --增加列
  35. alert table user add tel varchar(11);
  36. --删除列
  37. alert table user drop Dept;
  38. alert table user drop (column)Dept;
  39. --列改名
  40. alert table user change Name tel varchar(8);
  41. --表改名
  42. alert table user rename re_table;
  43. rename table re_table to t_user;
  44. -- 数据操作
  45. -- 插入数据
  46. insert into dept_emp (Name,sex,age,address,email)
  47. values ('','','','',''),('','','','','');
  48. -- ------------------------ 约束 ---------------------------------------
  49. -- 是在表上强制执行地数据校验规则,主要用于保证数据库地完整性
  50. /*
  51. not null
  52. unique 唯一键tb_depttb_dept
  53. primary key
  54. foreign key 外键
  55. check 检查
  56. */
  57. create table tb_emp(
  58. id int primary key auto_increment,
  59. Name varchar(18),
  60. sex varchar(2) default'男' check(sex='男'or sex='女'), -- 表级写法check mysql中不起作用
  61. age int,
  62. address varchar(200),
  63. email varchar(100) unique,
  64. dept_id int,#references tb_dept(id) #表级写法外键不起作用
  65. constraint foreign key fk_emp(dept_id) references tb_dept(id)
  66. );
  67. -- 创建表之后在添加外键约束
  68. alter table tb_emp add constraint foreign key fk_emp(dept_id) references tb_dept(id);

结束语