MySQL数据库 - 数据库创建及常用约束定义

1. 创建数据库

版本库代码路径:/data/workspace/myshixun/
root@evassh-20125843:~# mysql -uroot -p123123 -h127.0.0.1
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 40
Server version: 5.5.59-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

mysql> create database MyDb
    -> ;
Query OK, 1 row affected (0.00 sec)

mysql> 

2. 创建表

版本库代码路径:/data/workspace/myshixun/
root@evassh-20125843:~# mysql -uroot -p123123 -h127.0.0.1
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 47
Server version: 5.5.59-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database TestDb;
ERROR 1007 (HY000): Can't create database 'TestDb'; database exists
mysql> use TestDb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
mysql> create table t_student(
    -> sno varchar(20) not null,
    -> sname varchar(32) not null,
    -> dptno int,
    -> ssex varchar(2)
    -> );
ERROR 1050 (42S01): Table 't_student' already exists
mysql> 

3. 使用主键约束

版本库代码路径:/data/workspace/myshixun/
root@evassh-20125843:~# mysql -uroot -p123123 -h127.0.0.1
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 49
Server version: 5.5.59-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use TestDb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> drop table t_user1;
ERROR 1051 (42S02): Unknown table 't_user1'
mysql> create table t_user1(
    -> userid INT primary key,
    -> name varchar(32),
    -> password varchar(11),
    -> phone varchar(11),
    -> email varchar(32)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> create table t_user2(
    -> name varchar(32),
    -> phone varchar(11),
    -> email varchar(32),
    -> primary key (name,phone)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> ALTER TABLE t_user1 CHANGE userid useid int(11);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> drop table t_user1;
Query OK, 0 rows affected (0.00 sec)

mysql> create table t_user1(
    -> useid int,
    -> name varchar(32),
    -> password varchar(11),
    -> phone varchar(11),
    -> email varchar(32)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> alter table t_user1 add primary key useid;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
mysql> alter table t_user1 add primary key useid;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
mysql> alter table t_user1 add primary key(useid);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> drop table t_user1;
Query OK, 0 rows affected (0.01 sec)

mysql> create table t_user1(
    -> useid int primary key,
    -> name varchar(32),
    -> password varchar(11),
    -> phone varchar(11),
    -> email varchar(32)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> 

4. 外键约束

版本库代码路径:/data/workspace/myshixun/
root@evassh-20125843:~# mysql -uroot -p123123 -h127.0.0.1
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 60
Server version: 5.5.59-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use TestDb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> create table t_depart(
    -> dptno int primary key,
    -> dptname varchar(22));
Query OK, 0 rows affected (0.00 sec)

mysql> create table t_student(
    -> stid varchar(12) primary key,
    -> name varchar(22),
    -> dptno int,
    -> foreign key (dptno) references t_depart(dptno));
Query OK, 0 rows affected (0.01 sec)

mysql> 

5. 添加常用约束

版本库代码路径:/data/workspace/myshixun/
root@evassh-20125843:~# mysql -uroot -p123123 -h127.0.0.1
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 62
Server version: 5.5.59-0ubuntu0.14.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use TestDb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> create table t_user(
    -> id int primary key auto_increment,
    -> username varchar(32) not null unique,
    -> sex varchar(4) default 'm');
Query OK, 0 rows affected (0.00 sec)

mysql> 







  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小蒋的学习笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值