MySQL | 小白创建表

装饰风格:

  • 语句以;结尾
  • 数据是字符型添加''或者""

语法

show database;
show tables;

  • 查看表结构

describe tb_name
desc tb_name

  • 查询表数据

select * from tb_name where conditions

  • 创建表

create table 表名(列名 列类型,列名 列类型…);

  • 重命名表

rename table 旧表名 to/ as 新表名

  • 修改表名、列名或类型

alter table 表名 change 旧列名 新列名 新列名的类型
alter table 表名 modify 被修改的列名 修改后的新类型

  • 删除表中数据

delete from 表名 where 条件

  • 删除表结构及数据

drop table 表名

游戏规则:

表架构

student(s, sname, sage, ssex) 学生表
course(c, cname, t) 课程表
sc(s, c, score) 成绩表
teacher(t, tname) 教师表

mac

建表

mysql> create database li;
Query OK, 1 row affected (0.03 sec)

mysql> use li;
Database changed

mysql> create table student(
    -> s int,
    -> sname char(32),
    -> sage int,
    -> ssex char(8)
    -> );
Query OK, 0 rows affected (0.05 sec)

mysql> create table course(
    -> c int,
    -> cname char(32),
    -> t int
    -> );
Query OK, 0 rows affected (0.06 sec)

mysql> create table sc(
    -> s_1 int,
    -> c_1 int,
    -> score int
    -> );
Query OK, 0 rows affected (0.06 sec)

mysql> create table teacher(
    -> t int,
    -> tname char(32)
    -> );
Query OK, 0 rows affected (0.08 sec)

mysql> alter table sc change s_1 s int;  #更改表的列名s_1为s
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table sc change c_1 c int;  #更改表的列名c_1为c
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show tables;
+--------------+
| Tables_in_li |
+--------------+
| course       |
| sc           |
| student      |
| teacher      |
+--------------+
4 rows in set (0.00 sec)

mysql> describe student;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| s     | int(11)  | YES  |     | NULL    |       |
| sname | char(32) | YES  |     | NULL    |       |
| sage  | int(11)  | YES  |     | NULL    |       |
| ssex  | char(8)  | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> select * from student;
Empty set (0.00 sec)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值