MySQL基本用法 I

MySQL Frequently Used Command (I)

SQL is a structured query language, it is a database language used to operate RDBMS, current relational databases support the use of SQL language, that is, you can use SQL to operate oracle, sql server, mysql, sqlite and all other relational databases

SQL clause mainly includes DQL, DML, TPL, DCL, DDL, CCL[, in which DQL and DML are most important for web developer.

Data integrity

  • A database is a complete business unit and can contain multiple tables in which data is stored.
  • In order to store the data more accurately and to ensure that the data is correct and valid, some mandatory validation can be added to the table when it is created, including the type of data type and constraints.

Data Type

Commonly used data types are as follows:
  • Integer: int, bit
  • fractional: decimal
  • String: varchar, char
  • Date and time: date, time, datetime
  • Enumerated types (enum)
Specially described types are as follows:
  • decimal means a floating point number, e.g. decimal(5,2) means a total of 5 digits, with 2 decimal places.
  • char means a fixed-length string, e.g. char(3), with a space for 'ab ’ if ‘ab’ is filled in.
  • varchar means a variable length string, such as varchar(3), which stores ab when filled with ‘ab’.
  • The string text indicates that large text is stored, and is recommended when the characters are greater than 4000.
  • For files such as images, audio and video, they are not stored in the database but are uploaded to some server and the path where this file is stored is then stored in a table.

Constraints

  • primary key: the order in which the primary key is physically stored
  • not null: this field is not allowed to be filled with a null value
  • unique: the value of this field is not allowed to be duplicated default: the default value is used when the value is not filled in, if it is filled in, it will prevail
  • foreign key: constraint on the relationship field, when filling the value for the relationship field, it will check if the value exists in the related table, if it exists, the filling will be successful, if not, the filling will fail and an exception will be thrown

Numerical type

TypeByte SizeSigned RangeUnsigned Range
TINYINT1-128 ~ 1270 ~ 255
SMALLINT2-32768 ~ 327670 ~ 65535
MEDIUMINT3-8388608 ~ 83886070 ~ 16777215
INT/INTEGER4-2147483648 ~21474836470 ~ 4294967295
BIGINT8-9223372036854775808 ~ 92233720368547758070 ~18446744073709551615

String

TypeByte SizeExample
CHAR0-255Type:char(3) input ‘ab’, actually stored as 'ab ', input ‘abcd’ actually stored as ‘abc’
VARCHAR0-255Type: varchar(3) lose ‘ab’, actually stored as ‘ab’, enter ‘abcd’, actually stored as ‘abc’
TEXT0-65535Large text

Date and time type

TypeByte SizeExample
DATE4‘2020-01-01’
TIME3‘12:29:59’
DATETIME8‘2020-01-01 12:29:59’
YEAR1‘2017’
TIMESTAMP4‘1970-01-01 00:00:01’ UTC ~ ‘2038-01-01 00:00:01’ UTC

Database basic operation

1. Connect to Database

mysql -uroot -p <password> 

2. Quit Login

quit or exit

3. Check Out Version, Display Current Time

select version();
select now();

4. Basic Operation

# check out databases
show database;
# check out tables
show tables;
# create a table
create table if not exists <table_name> (
	<col1> <datatype> <costraint>,
  ...
)
  # deomo 1
  create table if not exists areas (
      area_id varchar(6) primary key  not null,
      area_name varchar(20) not null,
      province_id varchar(6)
  );
  # demo 2
  create table classes(
      id int unsigned auto_increment primary key not null,
      name varchar(10)
  );
  # demo 3
  create table students(
    id int unsigned primary key auto_increment not null, name varchar(20) default '',
    age tinyint unsigned default 0,
    height decimal(5,2),
    gender enum('男','女','人妖','保密'),
    cls_id int unsigned default 0
  )
# modify a table
	# add a field
	alter table <table_name> add <col_name> <data_type>;
	# modify a field in a table (rename)
	alter table <table_name> change <origin_col_name> <new_col_name> <data_type> <constraint>;
	# modify a field in a table (no rename)
	alter table <table_name> modify <col_name> <data_type> <constraint>;
	# remove a field
	alter table <table_name> drop <col_name>;
# remove a table 
drop table <table_name>;
# show the clause of creating a table
show create table <table_name>;
	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值