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)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 50个常用的sql语句Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname...
    哈哈海阅读 1,256评论 0 7
  • Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 S...
    忘了呼吸的那只猫阅读 2,930评论 0 8
  • 最近打算采用关系型数据库来理一下公司的运营数据,先拿点东西练手找感觉。下面是几个关于学生课业的表,需要建立一个数据...
    九天朱雀阅读 1,004评论 0 3
  • 说明:以下五十个语句都按照测试数据进行过测试,最好每次只单独运行一个语句。 问题及描述: --1.学生表 Stud...
    lijun_m阅读 1,329评论 0 1
  • 1).创建数据库 create database学生选课数据库 2).创建四张表 Create table Stu...
    blvftigd阅读 1,634评论 0 0