Skip to content

Commit

Permalink
update sql
Browse files Browse the repository at this point in the history
  • Loading branch information
snowme34 committed Jan 30, 2019
1 parent 813db2c commit 9c6ef48
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
5 changes: 4 additions & 1 deletion cspell_dict_bash.txt
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,7 @@ Goroutines
mysqladmin
devel
mariadb
SAVEPOINT
SAVEPOINT
smallint
tinyint
varchar
94 changes: 93 additions & 1 deletion docs/source/reference/database/sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ A standard of ANSI.

Almost all relational database management systems use SQL.

This reference focuses on MySQL.

## What Can SQL Do

* Query database
Expand Down Expand Up @@ -82,4 +84,94 @@ ROLLBACK

## Relational

Data are stored using tables (row and column) with specified relationships between them.
Data are stored using tables (row and column) with specified relationships between them.

## Database Management

Create

```sql
CREATE DATABASE aaa;
```

Delete

```sql
DROP DATABASE aaa;
```

## Table

Each column is an attribute

Each row is a record

Common Data Types

| Data Type | Usage |
| --------------------------------------------------------- | ---------------------------------- |
| integer(size) or int(size), smallint(size), tinyint(size) | store integers (exact value) |
| decimal(size,d), numeric(size,d) | fixed-point (exact value) |
| float, double | floating-point (approximate value) |
| char(size) | fixed-length |
| varchar(size) | variable-length |
| date | date |

[MySQL Data Types](https://dev.mysql.com/doc/refman/8.0/en/data-types.html)

`char(10)` means a string with length 10.

`varchar(10)` where 10 is the max length. Usually 100 or 255.

Must provide data type when creating the tables.

Create a table

* Usually there is a "ID" column.
* the last column does not need a comma
* [MySQL :: MySQL 8.0 Reference Manual :: 13.1.20 CREATE TABLE Syntax](https://dev.mysql.com/doc/refman/8.0/en/create-table.html)

```sql
CREATE TABLE some_table_name
(
some_col_name some_data_type,
some_col_name some_data_type,
some_col_name some_data_type,
...
);
```

Check table

```sql
DESCRIBE aaa;
DESC aaa;
```

In the output of `DESC`,

* Field means the column names, i.e. the attributes
* Type is type
* Null is if the cells in this column can be Null
* Key is whether this column will be used as a key column
* Default is the default value

Delete table

```sql
DROP aaa;
```

Modify table

```sql
ALTER TABLE aaa RENAME bbb;

ALTER TABLE aaa ADD ccc some_type;

ALTER TABLE aaa DROP COLUMN ccc;

ALTER TABLE aaa MODIFY ccc some_new_type;

ALTER TABLE aaa CHANGE COLUMN ccc some_new_name some_new_type;
```

0 comments on commit 9c6ef48

Please sign in to comment.